-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
[API]Product image handling #12847
Merged
Merged
[API]Product image handling #12847
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 | ||
---|---|---|---|---|
|
@@ -33,6 +33,11 @@ public function getConfigTreeBuilder(): TreeBuilder | |||
->defaultFalse() | ||||
->end() | ||||
->end() | ||||
->children() | ||||
->variableNode('product_image_prefix') | ||||
->defaultValue('media/image') | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm fine with defining this value as a parameter, however we should at least use another parameter, that will be used here as well:
But once we will introduce the parameter there, I'm not sure we need it. Up to you. |
||||
->end() | ||||
->end() | ||||
; | ||||
|
||||
return $treeBuilder; | ||||
|
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
72 changes: 72 additions & 0 deletions
72
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,72 @@ | ||
<?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 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, $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']; | ||
arti0090 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return $data; | ||
} | ||
|
||
public function supportsNormalization($data, $format = null, $context = []): bool | ||
{ | ||
if (isset($context[self::ALREADY_CALLED])) { | ||
return false; | ||
} | ||
|
||
return $data instanceof ProductImageInterface; | ||
} | ||
|
||
private function validatePrefix(string $prefix): string | ||
{ | ||
if ('/' !== substr($prefix, 0, 1)) { | ||
arti0090 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$prefix = '/'.$prefix; | ||
} | ||
|
||
if ('/' === substr($prefix, -1)) { | ||
return $prefix; | ||
} | ||
|
||
return $prefix . '/'; | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
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,50 @@ | ||
<?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'); | ||
} | ||
arti0090 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I see correctly, there is no more
media_path
, you are updatingpath
fieldThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have changed it a little now :)