ObjectProperty::hydrate()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 21
ccs 12
cts 12
cp 1
rs 9.0534
cc 4
eloc 10
nc 4
nop 2
crap 4
1
<?php
2
3
namespace Marek\Toggable\Hydrator;
4
5
use ReflectionClass;
6
use ReflectionProperty;
7
use ArrayObject;
8
use BadMethodCallException;
9
10
/**
11
 * Class ObjectProperty
12
 * @package Marek\Toggable\Hydrator
13
 */
14
class ObjectProperty implements HydratorInterface, StrategyEnabledInterface
15
{
16
    /**
17
    * The list with strategies that this hydrator has.
18
    *
19
    * @var \ArrayObject
20
    */
21
    protected $strategies;
22
23
    /**
24
     * ObjectProperty constructor.
25
     */
26 71
    public function __construct()
27
    {
28 71
        $this->strategies = new ArrayObject();
29 71
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 14
    public function extract($object)
35
    {
36 14
        if (!is_object($object)) {
37 1
            throw new BadMethodCallException(
38 1
                sprintf('%s expects the provided $object to be a PHP object)', __METHOD__)
39 1
            );
40
        }
41
42 13
        $properties = $this->getObjectProperties($object);
43
44 13
        $data = array();
45 13
        foreach ($properties as $name => $value) {
46
47 13
            if (!empty($object->$name) || $object->$name === false) {
48 13
                $data[$name] = $this->extractValue($name, $object->$name);
49 13
            }
50
51 13
        }
52
53 13
        return $data;
54
    }
55
56
    /**
57
     * @inheritDoc
58
     */
59 14
    public function hydrate(array $data, $object)
60
    {
61 14
        if (!is_object($object)) {
62 1
            throw new BadMethodCallException(
63 1
                sprintf('%s expects the provided $object to be a PHP object)', __METHOD__)
64 1
            );
65
        }
66
67 13
        $props = $this->getObjectProperties($object);
68
69 13
        foreach ($data as $name => $value) {
70
71 13
            if (!isset($props[$name])) {
72 1
                continue;
73
            }
74
75 13
            $object->$name = $this->hydrateValue($name, $value);
76 13
        }
77
78 13
        return $object;
79
    }
80
81
    /**
82
     * @inheritDoc
83
     */
84 1
    public function canHydrate($object)
85
    {
86 1
        return true;
87
    }
88
89
    /**
90
     * @inheritDoc
91
     */
92 66
    public function addStrategy($name, StrategyInterface $strategy)
93
    {
94 66
        $this->strategies[$name] = $strategy;
95
96 66
        return $this;
97
    }
98
99
    /**
100
     * @inheritDoc
101
     */
102 21
    public function getStrategy($name)
103
    {
104 21
        return $this->strategies[$name];
105
    }
106
107
    /**
108
     * @inheritDoc
109
     */
110 29
    public function hasStrategy($name)
111
    {
112 29
        return array_key_exists($name, $this->strategies);
113
    }
114
115
    /**
116
     * @inheritDoc
117
     */
118 1
    public function removeStrategy($name)
119
    {
120 1
        unset($this->strategies[$name]);
121
122 1
        return $this;
123
    }
124
125
    /**
126
     * Converts a value for extraction. If no strategy exists the plain value is returned.
127
     *
128
     * @param string $name
129
     * @param mixed $value
130
     *
131
     * @return mixed
132
     */
133 13 View Code Duplication
    public function extractValue($name, $value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
134
    {
135 13
        if ($this->hasStrategy($name)) {
136 10
            $strategy = $this->getStrategy($name);
137 10
            $value = $strategy->extract($value);
138 10
        }
139
140 13
        return $value;
141
    }
142
143
    /**
144
     * Converts a value for hydration. If no strategy exists the plain value is returned.
145
     *
146
     * @param string $name
147
     * @param mixed $value
148
     *
149
     * @return mixed
150
     */
151 13 View Code Duplication
    public function hydrateValue($name, $value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
152
    {
153 13
        if ($this->hasStrategy($name)) {
154 10
            $strategy = $this->getStrategy($name);
155 10
            $value = $strategy->hydrate($value);
156 10
        }
157
158 13
        return $value;
159
    }
160
161
    /**
162
     * Return list of object properties
163
     *
164
     * @param object $object
165
     *
166
     * @return array
167
     */
168 26
    protected function getObjectProperties($object)
169
    {
170 26
        $reflection = new ReflectionClass($object);
171
172 26
        $properties = $reflection->getProperties(
173
            ReflectionProperty::IS_PRIVATE
174 26
            + ReflectionProperty::IS_PROTECTED
175 26
        );
176
177 26
        $props = array();
178
        /** @var ReflectionProperty $property */
179 26
        foreach ($properties as $property) {
180 26
            $props[$property->getName()] = true;
181 26
        }
182
183 26
        return $props;
184
    }
185
}
186