Completed
Push — master ( f2042d...10cdad )
by Kamil
29:04 queued 13:16
created

ProductVariantToProductOptionsTransformer.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Sylius\Bundle\ProductBundle\Form\DataTransformer;
15
16
use Sylius\Component\Product\Model\ProductInterface;
17
use Sylius\Component\Product\Model\ProductOptionValueInterface;
18
use Sylius\Component\Product\Model\ProductVariantInterface;
19
use Symfony\Component\Form\DataTransformerInterface;
20
use Symfony\Component\Form\Exception\TransformationFailedException;
21
use Symfony\Component\Form\Exception\UnexpectedTypeException;
22
23
final class ProductVariantToProductOptionsTransformer implements DataTransformerInterface
24
{
25
    /**
26
     * @var ProductInterface
27
     */
28
    private $product;
29
30
    /**
31
     * @param ProductInterface $product
32
     */
33
    public function __construct(ProductInterface $product)
34
    {
35
        $this->product = $product;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     *
41
     * @throws UnexpectedTypeException
42
     */
43
    public function transform($value): array
44
    {
45
        if (null === $value) {
46
            return [];
47
        }
48
49
        if (!$value instanceof ProductVariantInterface) {
50
            throw new UnexpectedTypeException($value, ProductVariantInterface::class);
51
        }
52
53
        return array_combine(
54
            array_map(function (ProductOptionValueInterface $productOptionValue) {
55
                return $productOptionValue->getOptionCode();
56
            }, $value->getOptionValues()->toArray()),
57
            $value->getOptionValues()->toArray()
58
        );
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function reverseTransform($value): ?ProductVariantInterface
65
    {
66
        if (null === $value || '' === $value) {
67
            return null;
68
        }
69
70
        if (!is_array($value) && !$value instanceof \Traversable && !$value instanceof \ArrayAccess) {
71
            throw new UnexpectedTypeException($value, '\Traversable or \ArrayAccess');
72
        }
73
74
        return $this->matches($value);
0 ignored issues
show
$value is of type array|object<Traversable>|object<ArrayAccess>, but the function expects a array<integer,object<Syl...tOptionValueInterface>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
75
    }
76
77
    /**
78
     * @param ProductOptionValueInterface[] $optionValues
79
     *
80
     * @return ProductVariantInterface|null
81
     *
82
     * @throws TransformationFailedException
83
     */
84
    private function matches(array $optionValues): ?ProductVariantInterface
85
    {
86
        foreach ($this->product->getVariants() as $variant) {
87
            foreach ($optionValues as $optionValue) {
88
                if (null === $optionValue || !$variant->hasOptionValue($optionValue)) {
89
                    continue 2;
90
                }
91
            }
92
93
            return $variant;
94
        }
95
96
        throw new TransformationFailedException(sprintf(
97
            'Variant "%s" not found for product %s',
98
            !empty($optionValues[0]) ? $optionValues[0]->getCode() : '',
99
            $this->product->getCode()
100
        ));
101
    }
102
}
103