Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add callout colors #113

Open
wants to merge 1 commit into
base: 0.5.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Setono\SyliusCalloutPlugin\CssClassBuilder;
namespace Setono\SyliusCalloutPlugin\CssBuilder;

use Setono\SyliusCalloutPlugin\Model\CalloutInterface;

Expand Down
23 changes: 23 additions & 0 deletions src/CssBuilder/CssStyleBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusCalloutPlugin\CssBuilder;

use Setono\SyliusCalloutPlugin\Model\CalloutInterface;

class CssStyleBuilder implements CssStyleBuilderInterface
{
public function build(CalloutInterface $callout): string
{
$style = 'z-index: 999;';
if ($callout->getColor()) {
$style .= sprintf(' color: %s;', $callout->getColor());
}
if ($callout->getBackgroundColor()) {
$style .= sprintf(' background-color: %s;', $callout->getBackgroundColor());
}

return $style;
}
}
12 changes: 12 additions & 0 deletions src/CssBuilder/CssStyleBuilderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusCalloutPlugin\CssBuilder;

use Setono\SyliusCalloutPlugin\Model\CalloutInterface;

interface CssStyleBuilderInterface
{
public function build(CalloutInterface $callout): string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Setono\SyliusCalloutPlugin\CssClassBuilder;
namespace Setono\SyliusCalloutPlugin\CssBuilder;

use Setono\SyliusCalloutPlugin\Model\CalloutInterface;
use Webmozart\Assert\Assert;
Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<import resource="services/command.xml"/>
<import resource="services/controller.xml"/>
<import resource="services/css_class_builder.xml"/>
<import resource="services/css_style_builder.xml"/>
<import resource="services/event_subscriber.xml"/>
<import resource="services/factory.xml"/>
<import resource="services/fixture.xml"/>
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/config/services/css_class_builder.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="setono_sylius_callout.css_class_builder.semantic_ui"
class="Setono\SyliusCalloutPlugin\CssClassBuilder\SemanticUiCssClassBuilder">
class="Setono\SyliusCalloutPlugin\CssBuilder\SemanticUiCssClassBuilder">
</service>

<service id="setono_sylius_callout.css_class_builder.default"
Expand Down
12 changes: 12 additions & 0 deletions src/Resources/config/services/css_style_builder.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="setono_sylius_callout.css_style_builder.default"
class="Setono\SyliusCalloutPlugin\CssBuilder\CssStyleBuilder">
</service>

</services>
</container>
1 change: 1 addition & 0 deletions src/Resources/config/services/twig.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<argument type="service" id="setono_sylius_callout.css_class_builder.default"/>
<argument type="service" id="setono_sylius_callout.provider.rendering_callout"/>
<argument type="service" id="setono_sylius_callout.renderer.callout"/>
<argument type="service" id="setono_sylius_callout.css_style_builder.default"/>

<tag name="twig.runtime"/>
</service>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
{# @var \Setono\SyliusCalloutPlugin\Model\CalloutInterface callout #}
<div class="{{ render_callout_class_attribute(callout) }}" style="z-index: 999">{{ render_callout(callout) }}</div>
<div class="{{ render_callout_class_attribute(callout) }}" style="{{ render_callout_style(callout) }}">{{ render_callout(callout) }}</div>
1 change: 1 addition & 0 deletions src/Twig/CalloutExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function getFunctions(): array
new TwigFunction('get_callout_assignment_delay', $this->getDelay(...)),
new TwigFunction('render_callout_class_attribute', [CalloutRuntime::class, 'renderCalloutClassAttribute']),
new TwigFunction('render_callout', [CalloutRuntime::class, 'renderCallout'], ['is_safe' => ['html']]),
new TwigFunction('render_callout_style', [CalloutRuntime::class, 'renderCalloutStyle']),
];
}

Expand Down
9 changes: 8 additions & 1 deletion src/Twig/CalloutRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

use Setono\SyliusCalloutPlugin\Checker\Eligibility\CalloutEligibilityCheckerInterface;
use Setono\SyliusCalloutPlugin\Checker\RenderingEligibility\CalloutRenderingEligibilityCheckerInterface;
use Setono\SyliusCalloutPlugin\CssClassBuilder\CssClassBuilderInterface;
use Setono\SyliusCalloutPlugin\CssBuilder\CssClassBuilderInterface;
use Setono\SyliusCalloutPlugin\CssBuilder\CssStyleBuilderInterface;
use Setono\SyliusCalloutPlugin\Model\CalloutInterface;
use Setono\SyliusCalloutPlugin\Model\ProductInterface;
use Setono\SyliusCalloutPlugin\Provider\RenderingCalloutProviderInterface;
Expand All @@ -21,6 +22,7 @@ public function __construct(
private readonly CssClassBuilderInterface $cssClassBuilder,
private readonly RenderingCalloutProviderInterface $renderingCalloutProvider,
private readonly CalloutRendererInterface $calloutRenderer,
private readonly CssStyleBuilderInterface $cssStyleBuilder,
) {
}

Expand Down Expand Up @@ -76,4 +78,9 @@ public function renderCallout(CalloutInterface $callout): string
{
return (string) $this->calloutRenderer->render($callout);
}

public function renderCalloutStyle(CalloutInterface $callout): string
{
return $this->cssStyleBuilder->build($callout);
}
}
5 changes: 4 additions & 1 deletion tests/Twig/CalloutRuntimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
use Prophecy\PhpUnit\ProphecyTrait;
use Setono\SyliusCalloutPlugin\Checker\Eligibility\CalloutEligibilityCheckerInterface;
use Setono\SyliusCalloutPlugin\Checker\RenderingEligibility\CalloutRenderingEligibilityCheckerInterface;
use Setono\SyliusCalloutPlugin\CssClassBuilder\CssClassBuilderInterface;
use Setono\SyliusCalloutPlugin\CssBuilder\CssClassBuilderInterface;
use Setono\SyliusCalloutPlugin\CssBuilder\CssStyleBuilderInterface;
use Setono\SyliusCalloutPlugin\Model\Callout;
use Setono\SyliusCalloutPlugin\Model\CalloutInterface;
use Setono\SyliusCalloutPlugin\Model\ProductInterface;
Expand Down Expand Up @@ -76,13 +77,15 @@ private function getCalloutRuntime(): CalloutRuntime
;

$calloutRenderer = $this->prophesize(CalloutRendererInterface::class);
$cssStyleBuilder = $this->prophesize(CssStyleBuilderInterface::class);

return new CalloutRuntime(
$calloutRenderingEligibilityChecker->reveal(),
$calloutEligibilityChecker->reveal(),
$cssClassBuilder->reveal(),
$renderCalloutProvider->reveal(),
$calloutRenderer->reveal(),
$cssStyleBuilder->reveal(),
);
}

Expand Down