-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add doc
- Loading branch information
Showing
8 changed files
with
147 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
70 changes: 70 additions & 0 deletions
70
src/Sylius/Bundle/ApiBundle/Serializer/ProductImageNormalizer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Paweł Jędrzejewski | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Sylius\Bundle\ApiBundle\Serializer; | ||
|
||
use Sylius\Component\Core\Model\ProductImageInterface; | ||
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface; | ||
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface; | ||
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait; | ||
use Webmozart\Assert\Assert; | ||
|
||
/** @experimental */ | ||
class ProductImageNormalizer implements ContextAwareNormalizerInterface, NormalizerAwareInterface | ||
{ | ||
use NormalizerAwareTrait; | ||
|
||
private const ALREADY_CALLED = 'product_image_normalizer_already_called'; | ||
|
||
/** @var string */ | ||
private $prefix; | ||
|
||
public function __construct(string $prefix) | ||
{ | ||
$this->prefix = $this->validatePrefix($prefix); | ||
} | ||
|
||
public function normalize($object, string $format = null, array $context = []) | ||
{ | ||
Assert::isInstanceOf($object, ProductImageInterface::class); | ||
Assert::keyNotExists($context, self::ALREADY_CALLED); | ||
|
||
$context[self::ALREADY_CALLED] = true; | ||
|
||
$data = $this->normalizer->normalize($object, $format, $context); | ||
|
||
$data['media_path'] = $this->prefix . $data['path']; | ||
|
||
return $data; | ||
} | ||
|
||
public function supportsNormalization($data, string $format = null, array $context = []): bool | ||
{ | ||
if (isset($context[self::ALREADY_CALLED])) { | ||
return false; | ||
} | ||
|
||
return $data instanceof ProductImageInterface; | ||
} | ||
|
||
private function validatePrefix(string $prefix): string | ||
{ | ||
if ('/' !== substr($prefix, 0)) { | ||
$prefix = '/'.$prefix; | ||
} | ||
|
||
if ('/' === substr($prefix, -1)) { | ||
return $prefix; | ||
} | ||
|
||
return $prefix . '/'; | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
src/Sylius/Bundle/ApiBundle/spec/Serializer/ProductImageNormalizerSpec.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Paweł Jędrzejewski | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace spec\Sylius\Bundle\ApiBundle\Serializer; | ||
|
||
use PhpSpec\ObjectBehavior; | ||
use Sylius\Component\Core\Model\OrderInterface; | ||
use Sylius\Component\Core\Model\ProductImageInterface; | ||
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface; | ||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; | ||
|
||
final class ProductImageNormalizerSpec extends ObjectBehavior | ||
{ | ||
function let(): void { | ||
$this->beConstructedWith('prefix'); | ||
} | ||
|
||
function it_implements_context_aware_normalizer_interface(): void | ||
{ | ||
$this->shouldImplement(ContextAwareNormalizerInterface::class); | ||
} | ||
|
||
function it_supports_only_product_image_interface(ProductImageInterface $productImage, OrderInterface $order): void | ||
{ | ||
$this->supportsNormalization($productImage)->shouldReturn(true); | ||
$this->supportsNormalization($order)->shouldReturn(false); | ||
} | ||
|
||
function it_serializes_product_image_with_proper_prefix( | ||
NormalizerInterface $normalizer, | ||
ProductImageInterface $productImage | ||
): void { | ||
$this->setNormalizer($normalizer); | ||
|
||
$normalizer->normalize($productImage, null, ['product_image_normalizer_already_called' => true])->willReturn(['path' => 'some_path']); | ||
|
||
$this->normalize($productImage, null, [])->shouldReturn(['path' => 'some_path', 'media_path' => '/prefix/some_path']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters