forked from Sylius/Sylius
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bring back the transformer from ProductBundle and create a new one fo…
…r Admin
- Loading branch information
Showing
5 changed files
with
257 additions
and
32 deletions.
There are no files selected for viewing
129 changes: 129 additions & 0 deletions
129
...lius/Bundle/AdminBundle/Form/DataTransformer/ProductsToProductAssociationsTransformer.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,129 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Sylius Sp. z o.o. | ||
* | ||
* 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\AdminBundle\Form\DataTransformer; | ||
|
||
use Doctrine\Common\Collections\ArrayCollection; | ||
use Doctrine\Common\Collections\Collection; | ||
use Sylius\Component\Product\Model\ProductAssociationInterface; | ||
use Sylius\Component\Product\Model\ProductAssociationTypeInterface; | ||
use Sylius\Component\Product\Model\ProductInterface; | ||
use Sylius\Component\Resource\Factory\FactoryInterface; | ||
use Sylius\Component\Resource\Repository\RepositoryInterface; | ||
use Symfony\Component\Form\DataTransformerInterface; | ||
use Webmozart\Assert\Assert; | ||
|
||
/** | ||
* @implements DataTransformerInterface<Collection<array-key, ProductAssociationInterface>, array<string, Collection<array-key, ProductInterface>>> | ||
*/ | ||
final class ProductsToProductAssociationsTransformer implements DataTransformerInterface | ||
{ | ||
/** @var Collection<array-key, ProductAssociationInterface>|null */ | ||
private ?Collection $productAssociations = null; | ||
|
||
/** | ||
* @param FactoryInterface<ProductAssociationInterface> $productAssociationFactory | ||
* @param RepositoryInterface<ProductAssociationTypeInterface> $productAssociationTypeRepository | ||
*/ | ||
public function __construct( | ||
private readonly FactoryInterface $productAssociationFactory, | ||
private readonly RepositoryInterface $productAssociationTypeRepository, | ||
) { | ||
} | ||
|
||
/** | ||
* @return array<string, Collection<array-key, ProductInterface>> | ||
*/ | ||
public function transform(mixed $value): array | ||
{ | ||
$this->setProductAssociations($value); | ||
|
||
if ($value->isEmpty()) { | ||
return []; | ||
} | ||
|
||
$values = []; | ||
|
||
/** @var ProductAssociationInterface $productAssociation */ | ||
foreach ($value as $productAssociation) { | ||
$values[$productAssociation->getType()->getCode()] = clone $productAssociation->getAssociatedProducts(); | ||
} | ||
|
||
return $values; | ||
} | ||
|
||
public function reverseTransform(mixed $value): ?Collection | ||
{ | ||
if (!is_array($value)) { | ||
return null; | ||
} | ||
|
||
/** @var Collection<array-key, ProductAssociationInterface> $productAssociations */ | ||
$productAssociations = new ArrayCollection(); | ||
foreach ($value as $productAssociationTypeCode => $products) { | ||
if ($products->isEmpty()) { | ||
continue; | ||
} | ||
|
||
$productAssociation = $this->getProductAssociationByTypeCode((string) $productAssociationTypeCode); | ||
$this->linkProductsToAssociation($productAssociation, $products); | ||
$productAssociations->add($productAssociation); | ||
} | ||
|
||
$this->setProductAssociations(null); | ||
|
||
return $productAssociations; | ||
} | ||
|
||
private function getProductAssociationByTypeCode(string $productAssociationTypeCode): ProductAssociationInterface | ||
{ | ||
foreach ($this->productAssociations as $productAssociation) { | ||
if ($productAssociationTypeCode === $productAssociation->getType()->getCode()) { | ||
return $productAssociation; | ||
} | ||
} | ||
|
||
/** @var ProductAssociationTypeInterface $productAssociationType */ | ||
$productAssociationType = $this->productAssociationTypeRepository->findOneBy([ | ||
'code' => $productAssociationTypeCode, | ||
]); | ||
|
||
/** @var ProductAssociationInterface $productAssociation */ | ||
$productAssociation = $this->productAssociationFactory->createNew(); | ||
$productAssociation->setType($productAssociationType); | ||
|
||
return $productAssociation; | ||
} | ||
|
||
/** | ||
* @param Collection<array-key, ProductInterface> $products | ||
*/ | ||
private function linkProductsToAssociation( | ||
ProductAssociationInterface $productAssociation, | ||
Collection $products, | ||
): void { | ||
$productAssociation->clearAssociatedProducts(); | ||
foreach ($products as $product) { | ||
Assert::isInstanceOf($product, ProductInterface::class); | ||
$productAssociation->addAssociatedProduct($product); | ||
} | ||
} | ||
|
||
/** | ||
* @param Collection<array-key, ProductAssociationInterface>|null $productAssociations | ||
*/ | ||
private function setProductAssociations(?Collection $productAssociations): void | ||
{ | ||
$this->productAssociations = $productAssociations instanceof Collection ? $productAssociations : new ArrayCollection(); | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
...le/AdminBundle/spec/Form/DataTransformer/ProductsToProductAssociationsTransformerSpec.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,83 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Sylius Sp. z o.o. | ||
* | ||
* 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\AdminBundle\Form\DataTransformer; | ||
|
||
use Doctrine\Common\Collections\ArrayCollection; | ||
use PhpSpec\ObjectBehavior; | ||
use Sylius\Component\Product\Model\ProductAssociationInterface; | ||
use Sylius\Component\Product\Model\ProductAssociationTypeInterface; | ||
use Sylius\Component\Product\Model\ProductInterface; | ||
use Sylius\Component\Resource\Factory\FactoryInterface; | ||
use Sylius\Component\Resource\Repository\RepositoryInterface; | ||
use Symfony\Component\Form\DataTransformerInterface; | ||
|
||
final class ProductsToProductAssociationsTransformerSpec extends ObjectBehavior | ||
{ | ||
function let( | ||
FactoryInterface $productAssociationFactory, | ||
RepositoryInterface $productAssociationTypeRepository, | ||
): void { | ||
$this->beConstructedWith( | ||
$productAssociationFactory, | ||
$productAssociationTypeRepository, | ||
); | ||
} | ||
|
||
function it_is_a_data_transformer(): void | ||
{ | ||
$this->shouldImplement(DataTransformerInterface::class); | ||
} | ||
|
||
function it_transforms_an_empty_collection_to_an_empty_array(): void | ||
{ | ||
$this->transform(new ArrayCollection())->shouldReturn([]); | ||
} | ||
|
||
function it_transforms_product_associations_to_array( | ||
ProductAssociationInterface $productAssociation, | ||
ProductAssociationTypeInterface $productAssociationType, | ||
ProductInterface $firstAssociatedProduct, | ||
ProductInterface $secondAssociatedProduct, | ||
): void { | ||
$productAssociation->getType()->willReturn($productAssociationType); | ||
$productAssociation->getAssociatedProducts()->willReturn( | ||
new ArrayCollection([ | ||
$firstAssociatedProduct->getWrappedObject(), | ||
$secondAssociatedProduct->getWrappedObject(), | ||
]), | ||
); | ||
|
||
$firstAssociatedProduct->getCode()->willReturn('FIRST'); | ||
$secondAssociatedProduct->getCode()->willReturn('SECOND'); | ||
|
||
$productAssociationType->getCode()->willReturn('accessories'); | ||
|
||
$this->transform(new ArrayCollection([$productAssociation->getWrappedObject()]))->shouldBeLike([ | ||
'accessories' => new ArrayCollection([ | ||
$firstAssociatedProduct->getWrappedObject(), | ||
$secondAssociatedProduct->getWrappedObject(), | ||
]), | ||
]); | ||
} | ||
|
||
function it_reverse_transforms_null_into_null(): void | ||
{ | ||
$this->reverseTransform(null)->shouldReturn(null); | ||
} | ||
|
||
function it_reverse_transforms_empty_string_into_null(): void | ||
{ | ||
$this->reverseTransform('')->shouldReturn(null); | ||
} | ||
} |
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