|
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 PimCatalogIdentifier |
|
10
|
|
|
* |
|
11
|
|
|
* @package Actualys\Bundle\DrupalCommerceConnectorBundle\Normalizer |
|
12
|
|
|
*/ |
|
13
|
|
|
class PimCatalogIdentifierNormalizer 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
|
|
|
$normalizedAttribute['required'] = $attribute->isRequired(); |
|
32
|
|
|
$normalizedAttribute['type'] = $attribute->getAttributeType(); |
|
33
|
|
|
$normalizedAttribute['code'] = $attribute->getCode(); |
|
34
|
|
|
$normalizedAttribute['parameters'] = [ |
|
35
|
|
|
'scope' => $attribute->isScopable(), |
|
36
|
|
|
'unique_value' => $attribute->isUnique(), |
|
37
|
|
|
'max_characters' => $attribute->getMaxCharacters(), |
|
38
|
|
|
'validation_rule' => $attribute->getValidationRule(), |
|
39
|
|
|
]; |
|
40
|
|
|
|
|
41
|
|
|
if ($attribute->isLocalizable()) { |
|
42
|
|
|
foreach ($attribute->getTranslations() as $trans) { |
|
43
|
|
|
$normalizedAttribute['labels'][$trans->getLocale( |
|
44
|
|
|
)] = $trans->getLabel(); |
|
45
|
|
|
} |
|
46
|
|
|
} else { |
|
47
|
|
|
$normalizedAttribute['labels'][LANGUAGE_NONE] = $attribute->getLabel( |
|
48
|
|
|
); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
return $normalizedAttribute; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Checks whether the given class is supported for normalization by this normalizer |
|
56
|
|
|
* |
|
57
|
|
|
* @param mixed $data Data to normalize. |
|
58
|
|
|
* @param string $format The format being (de-)serialized from or into. |
|
59
|
|
|
* |
|
60
|
|
|
* @return boolean |
|
61
|
|
|
*/ |
|
62
|
|
|
public function supportsNormalization($data, $format = null) |
|
63
|
|
|
{ |
|
64
|
|
|
return $data instanceof Attribute; |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|