@@ 13-52 (lines=40) @@ | ||
10 | /** |
|
11 | * @deprecated 2.0-beta use EmbedOneFieldNormalizer or ReferenceOneFieldNormalizer |
|
12 | */ |
|
13 | final class ReferenceFieldNormalizer implements FieldNormalizerInterface |
|
14 | { |
|
15 | /** |
|
16 | * @var AccessorInterface |
|
17 | */ |
|
18 | private $accessor; |
|
19 | ||
20 | /** |
|
21 | * @param AccessorInterface $accessor |
|
22 | */ |
|
23 | public function __construct(AccessorInterface $accessor) |
|
24 | { |
|
25 | $this->accessor = $accessor; |
|
26 | } |
|
27 | ||
28 | /** |
|
29 | * @param string $path |
|
30 | * @param object $object |
|
31 | * @param NormalizerContextInterface $context |
|
32 | * @param NormalizerInterface|null $normalizer |
|
33 | * |
|
34 | * @return array |
|
35 | */ |
|
36 | public function normalizeField( |
|
37 | string $path, |
|
38 | $object, |
|
39 | NormalizerContextInterface $context, |
|
40 | NormalizerInterface $normalizer = null |
|
41 | ) { |
|
42 | if (null === $normalizer) { |
|
43 | throw SerializerLogicException::createMissingNormalizer($path); |
|
44 | } |
|
45 | ||
46 | if (null === $value = $this->accessor->getValue($object)) { |
|
47 | return null; |
|
48 | } |
|
49 | ||
50 | return $normalizer->normalize($value, $context, $path); |
|
51 | } |
|
52 | } |
|
53 |
@@ 13-54 (lines=42) @@ | ||
10 | use Chubbyphp\Serialization\Normalizer\FieldNormalizerInterface; |
|
11 | use Chubbyphp\Serialization\SerializerLogicException; |
|
12 | ||
13 | final class EmbedOneFieldNormalizer implements FieldNormalizerInterface |
|
14 | { |
|
15 | /** |
|
16 | * @var AccessorInterface |
|
17 | */ |
|
18 | private $accessor; |
|
19 | ||
20 | /** |
|
21 | * @param AccessorInterface $accessor |
|
22 | */ |
|
23 | public function __construct(AccessorInterface $accessor) |
|
24 | { |
|
25 | $this->accessor = $accessor; |
|
26 | } |
|
27 | ||
28 | /** |
|
29 | * @param string $path |
|
30 | * @param object $object |
|
31 | * @param NormalizerContextInterface $context |
|
32 | * @param NormalizerInterface|null $normalizer |
|
33 | * |
|
34 | * @return mixed |
|
35 | * |
|
36 | * @throws SerializerLogicException |
|
37 | */ |
|
38 | public function normalizeField( |
|
39 | string $path, |
|
40 | $object, |
|
41 | NormalizerContextInterface $context, |
|
42 | NormalizerInterface $normalizer = null |
|
43 | ) { |
|
44 | if (null === $normalizer) { |
|
45 | throw SerializerLogicException::createMissingNormalizer($path); |
|
46 | } |
|
47 | ||
48 | if (null === $childObject = $this->accessor->getValue($object)) { |
|
49 | return null; |
|
50 | } |
|
51 | ||
52 | return $normalizer->normalize($childObject, $context, $path); |
|
53 | } |
|
54 | } |
|
55 |