1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Actualys\Bundle\DrupalCommerceConnectorBundle\Normalizer\Attribute; |
4
|
|
|
|
5
|
|
|
use Pim\Bundle\CatalogBundle\Entity\Attribute; |
6
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class PimCatalogBoolean |
10
|
|
|
* |
11
|
|
|
* @package Actualys\Bundle\DrupalCommerceConnectorBundle\Normalizer |
12
|
|
|
*/ |
13
|
|
View Code Duplication |
class PimCatalogBooleanNormalizer implements NormalizerInterface |
|
|
|
|
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @param object $attribute |
18
|
|
|
* @param null $format |
19
|
|
|
* @param array $context |
20
|
|
|
* @return array|\Symfony\Component\Serializer\Normalizer\scalar |
21
|
|
|
*/ |
22
|
|
|
public function normalize($attribute, $format = null, array $context = []) |
23
|
|
|
{ |
24
|
|
|
/**@var Attribute $attribute * */ |
25
|
|
|
$normalizedAttribute = [ |
26
|
|
|
'code' => null, |
27
|
|
|
'type' => null, |
28
|
|
|
'required' => null, |
29
|
|
|
'labels' => null, |
30
|
|
|
'parameters' => null, |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
$availableLocales = []; |
34
|
|
|
$attributeAvailableLocales = $attribute->getAvailableLocales(); |
35
|
|
|
if (!is_null($attributeAvailableLocales)) { |
36
|
|
|
foreach ($attribute->getAvailableLocales() as $availableLocale) { |
37
|
|
|
$availableLocales [] = $availableLocale; |
|
|
|
|
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$normalizedAttribute['required'] = $attribute->isRequired(); |
42
|
|
|
$normalizedAttribute['type'] = $attribute->getAttributeType(); |
43
|
|
|
$normalizedAttribute['code'] = $attribute->getCode(); |
44
|
|
|
$normalizedAttribute['parameters'] = [ |
45
|
|
|
'scope' => $attribute->isScopable(), |
46
|
|
|
'localizable' => $attribute->isLocalizable(), |
47
|
|
|
'available_locales' => $availableLocales, |
48
|
|
|
'default_value' => null, |
49
|
|
|
]; |
50
|
|
|
|
51
|
|
|
if ($attribute->isLocalizable()) { |
52
|
|
|
foreach ($attribute->getTranslations() as $trans) { |
53
|
|
|
$normalizedAttribute['labels'][$trans->getLocale( |
54
|
|
|
)] = $trans->getLabel(); |
55
|
|
|
} |
56
|
|
|
} else { |
57
|
|
|
$normalizedAttribute['labels'][LANGUAGE_NONE] = $attribute->getLabel( |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return $normalizedAttribute; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param mixed $data |
66
|
|
|
* @param null $format |
67
|
|
|
* @return bool |
68
|
|
|
*/ |
69
|
|
|
public function supportsNormalization($data, $format = null) |
70
|
|
|
{ |
71
|
|
|
return $data instanceof Attribute; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
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.