Skip to content

Commit

Permalink
Merge pull request #89 from Setono/66-handle-empty-image
Browse files Browse the repository at this point in the history
Handle the empty background image
  • Loading branch information
Roshyo authored Aug 7, 2020
2 parents 7db76f2 + 789a53c commit cebcb0c
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<import resource="services/order_processor.xml"/>
<import resource="services/provider.xml"/>
<import resource="services/resolver.xml"/>
<import resource="services/validator.xml"/>
<import resource="services/voter.xml"/>
</imports>
</container>
11 changes: 11 additions & 0 deletions src/Resources/config/services/validator.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8" ?>

<container xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://symfony.com/schema/dic/services"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="Setono\SyliusGiftCardPlugin\Validator\Constraints\HasBackgroundImage">
<tag name="validator.constraint_validator" />
</service>
</services>
</container>
6 changes: 6 additions & 0 deletions src/Resources/config/validation/GiftCardConfiguration.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
<option name="fields">code</option>
<option name="groups">setono_sylius_gift_card</option>
</constraint>
<constraint name="Setono\SyliusGiftCardPlugin\Validator\Constraints\HasBackgroundImage">
<option name="groups">
<value>setono_sylius_gift_card</value>
</option>
</constraint>

<property name="code">
<constraint name="NotBlank">
<option name="groups">setono_sylius_gift_card</option>
Expand Down
2 changes: 2 additions & 0 deletions src/Resources/translations/validators.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ setono_sylius_gift_card:
integer: Amount must be an integer
min: Amount cannot be lower than 1
greater_than_or_equal: The amount must be greater than or equal to 1
gift_card_configuration:
background_image_required: The background image is required
gift_card_search_command:
gift_card:
does_not_exist: The gift card with this code does not exist
Expand Down
2 changes: 2 additions & 0 deletions src/Resources/translations/validators.fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ setono_sylius_gift_card:
not_blank: Veuillez renseigner un montant
integer: Le montant doit être un entier
min: Le montant ne dois pas être en-dessous de 1
gift_card_configuration:
background_image_required: L'image de fond est requise
gift_card_search_command:
gift_card:
does_not_exist: Ce code de chèque-cadeau n'existe pas
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{% form_theme form '@SyliusAdmin/Form/imagesTheme.html.twig' %}

<div class="ui two column stackable grid">
{{ form_errors(form) }}
<div class="column">
<div class="ui segment">
{{ form_row(form.code) }}
Expand Down
4 changes: 1 addition & 3 deletions src/Resources/views/Shop/GiftCard/pdf.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
<body>
{% if configuration.backgroundImage is not empty %}
{% set path = configuration.backgroundImage.path|imagine_filter('setono_sylius_gift_card_background') %}
{% else %}
{% set path = '//placehold.it/200x200' %}
<img src="{{ path }}" alt="">
{% endif %}
<img src="{{ path }}" alt="">
<span class="code-block" style="position: absolute; top: 515px; left: 55px; font-size: 3.2rem; width: 1100px; text-align: center;">
{{- giftCard.code -}}
</span>
Expand Down
18 changes: 18 additions & 0 deletions src/Validator/Constraints/HasBackgroundImage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusGiftCardPlugin\Validator\Constraints;

use Symfony\Component\Validator\Constraint;

final class HasBackgroundImage extends Constraint
{
/** @var string */
public $message = 'setono_sylius_gift_card.gift_card_configuration.background_image_required';

public function getTargets(): array
{
return [self::CLASS_CONSTRAINT];
}
}
30 changes: 30 additions & 0 deletions src/Validator/Constraints/HasBackgroundImageValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusGiftCardPlugin\Validator\Constraints;

use Setono\SyliusGiftCardPlugin\Model\GiftCardConfigurationInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;

final class HasBackgroundImageValidator extends ConstraintValidator
{
public function validate($value, Constraint $constraint): void
{
if (!$constraint instanceof HasBackgroundImage) {
throw new UnexpectedTypeException($constraint, HasBackgroundImage::class);
}

if (!$value instanceof GiftCardConfigurationInterface) {
return;
}

$backgroundImage = $value->getBackgroundImage();
if (null === $backgroundImage || null === $backgroundImage->getPath()) {
$this->context->buildViolation($constraint->message)
->addViolation();
}
}
}

0 comments on commit cebcb0c

Please sign in to comment.