1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Actualys\Bundle\DrupalCommerceConnectorBundle\Normalizer\Attribute; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
6
|
|
|
use Pim\Bundle\CatalogBundle\Entity\Attribute; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class PimCatalogMetric |
10
|
|
|
* |
11
|
|
|
* @package Actualys\Bundle\DrupalCommerceConnectorBundle\Normalizer |
12
|
|
|
*/ |
13
|
|
View Code Duplication |
class PimCatalogMetricNormalizer implements NormalizerInterface |
|
|
|
|
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @param object $attribute |
17
|
|
|
* @param null $format |
18
|
|
|
* @param array $context |
19
|
|
|
* @return array|\Symfony\Component\Serializer\Normalizer\scalar |
20
|
|
|
*/ |
21
|
|
|
public function normalize($attribute, $format = null, array $context = []) |
22
|
|
|
{ |
23
|
|
|
/**@var Attribute $attribute * */ |
24
|
|
|
$normalizedAttribute = [ |
25
|
|
|
'code' => null, |
26
|
|
|
'type' => null, |
27
|
|
|
'required' => null, |
28
|
|
|
'labels' => null, |
29
|
|
|
'parameters' => null, |
30
|
|
|
]; |
31
|
|
|
$availableLocales = []; |
32
|
|
|
$attributeAvailableLocales = $attribute->getAvailableLocales(); |
33
|
|
|
if (!is_null($attributeAvailableLocales)) { |
34
|
|
|
foreach ($attribute->getAvailableLocales() as $availableLocale) { |
35
|
|
|
$availableLocales [] = $availableLocale; |
|
|
|
|
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$normalizedAttribute['required'] = $attribute->isRequired(); |
40
|
|
|
$normalizedAttribute['type'] = $attribute->getAttributeType(); |
41
|
|
|
$normalizedAttribute['code'] = $attribute->getCode(); |
42
|
|
|
$normalizedAttribute['parameters'] = [ |
43
|
|
|
'scope' => $attribute->isScopable(), |
44
|
|
|
'localizable' => $attribute->isLocalizable(), |
45
|
|
|
'available_locales' => $availableLocales, |
46
|
|
|
'default_value' => null, |
47
|
|
|
'negative_allowed' => (bool) $attribute->isNegativeAllowed(), |
48
|
|
|
'decimale_allowed' => (bool) $attribute->isDecimalsAllowed(), |
49
|
|
|
'number_min' => (bool) $attribute->getNumberMin(), |
50
|
|
|
'number_max' => (bool) $attribute->getNumberMax(), |
51
|
|
|
'metric_family' => $attribute->getMetricFamily(), |
52
|
|
|
'default_metric_unit' => $attribute->getDefaultMetricUnit(), |
53
|
|
|
]; |
54
|
|
|
|
55
|
|
|
if ($attribute->isLocalizable()) { |
56
|
|
|
foreach ($attribute->getTranslations() as $trans) { |
57
|
|
|
$normalizedAttribute['labels'][$trans->getLocale( |
58
|
|
|
)] = $trans->getLabel(); |
59
|
|
|
} |
60
|
|
|
} else { |
61
|
|
|
$normalizedAttribute['labels'][LANGUAGE_NONE] = $attribute->getLabel( |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $normalizedAttribute; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Checks whether the given class is supported for normalization by this normalizer |
70
|
|
|
* |
71
|
|
|
* @param mixed $data Data to normalize. |
72
|
|
|
* @param string $format The format being (de-)serialized from or into. |
73
|
|
|
* |
74
|
|
|
* @return boolean |
75
|
|
|
*/ |
76
|
|
|
public function supportsNormalization($data, $format = null) |
77
|
|
|
{ |
78
|
|
|
return $data instanceof Attribute; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.