JsonApiSerializer::getTransformer()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * Author: Nil Portugués Calderó <[email protected]>
4
 * Date: 11/21/15
5
 * Time: 3:27 PM.
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace NilPortugues\Api\JsonApi;
11
12
use NilPortugues\Api\JsonApi\Http\Request\Parameters\Fields;
13
use NilPortugues\Api\JsonApi\Http\Request\Parameters\Included;
14
use NilPortugues\Serializer\DeepCopySerializer;
15
16
/**
17
 * Class JsonApiSerializer.
18
 */
19
class JsonApiSerializer extends DeepCopySerializer
20
{
21
    /**
22
     * @var JsonApiTransformer
23
     */
24
    protected $serializationStrategy;
25
26
    /**
27
     * @param JsonApiTransformer $strategy
28
     */
29
    public function __construct(JsonApiTransformer $strategy)
30
    {
31
        parent::__construct($strategy);
32
    }
33
34
    /**
35
     * @return JsonApiTransformer
36
     */
37
    public function getTransformer()
38
    {
39
        return $this->serializationStrategy;
40
    }
41
42
    /**
43
     * @param mixed                            $value
44
     * @param Http\Request\Parameters\Fields   $fields
45
     * @param Http\Request\Parameters\Included $included
46
     *
47
     * @return string
48
     */
49
    public function serialize($value, Fields $fields = null, Included $included = null)
50
    {
51
        if (null !== $fields) {
52
            $this->filterOutResourceFields($fields);
53
        }
54
55
        if (null !== $included) {
56
            $this->filterOutIncludedResources($included);
57
        }
58
59
        return parent::serialize($value);
60
    }
61
62
    /**
63
     * @param Http\Request\Parameters\Included $included
64
     */
65
    protected function filterOutIncludedResources(Included $included)
66
    {
67
        if (false === $included->isEmpty()) {
68
            foreach ($included->get() as $resource => $includeData) {
69
                foreach ($this->serializationStrategy->getMappings() as $mapping) {
70
                    $mapping->filteringIncludedResources(true);
71
                    if (is_array($includeData)) {
72
                        foreach ($includeData as $subResource) {
73
                            $this->serializationStrategy->getMappingByAlias($subResource)->addIncludedResource(
74
                                $this->serializationStrategy->getMappingByAlias($resource)->getClassName()
75
                            );
76
                        }
77
                        break;
78
                    }
79
80
                    $mapping->addIncludedResource(
81
                        $this->serializationStrategy->getMappingByAlias($resource)->getClassName()
82
                    );
83
                }
84
            }
85
        }
86
    }
87
88
    /**
89
     * @param Http\Request\Parameters\Fields $fields
90
     */
91
    protected function filterOutResourceFields(Fields $fields)
92
    {
93
        if (false === $fields->isEmpty()) {
94
            foreach ($fields->get() as $type => $properties) {
95
                foreach ($this->serializationStrategy->getMappings() as $mapping) {
96
                    if ($mapping->getClassAlias() === $type) {
97
                        $mapping->setFilterKeys($properties);
98
                    }
99
                }
100
            }
101
        }
102
    }
103
}
104