AbstractProperties::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
ccs 4
cts 4
cp 1
crap 1
1
<?php
2
3
/**
4
 * apparat-object
5
 *
6
 * @category    Apparat
7
 * @package     Apparat\Object
8
 * @subpackage  Apparat\Object\Application
9
 * @author      Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright   Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
11
 * @license     http://opensource.org/licenses/MIT The MIT License (MIT)
12
 */
13
14
/***********************************************************************************
15
 *  The MIT License (MIT)
16
 *
17
 *  Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
18
 *
19
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of
20
 *  this software and associated documentation files (the "Software"), to deal in
21
 *  the Software without restriction, including without limitation the rights to
22
 *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
23
 *  the Software, and to permit persons to whom the Software is furnished to do so,
24
 *  subject to the following conditions:
25
 *
26
 *  The above copyright notice and this permission notice shall be included in all
27
 *  copies or substantial portions of the Software.
28
 *
29
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
31
 *  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
32
 *  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
33
 *  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
 ***********************************************************************************/
36
37
namespace Apparat\Object\Domain\Model\Properties;
38
39
use Apparat\Object\Domain\Contract\SerializablePropertyInterface;
40
use Apparat\Object\Domain\Model\Object\ObjectInterface;
41
42
/**
43
 * Abstract object properties collection
44
 *
45
 * @package Apparat\Object
46
 * @subpackage Apparat\Object\Domain
47
 */
48
abstract class AbstractProperties implements PropertiesInterface
49
{
50
    /**
51
     * Absolute URL property
52
     *
53
     * @var string
54
     */
55
    const PROPERTY_ABSOLUTE_URL = 'absoluteUrl';
56
    /**
57
     * Canonical URL property
58
     *
59
     * @var string
60
     */
61
    const PROPERTY_CANONICAL_URL = 'canonicalUrl';
62
    /**
63
     * Property data
64
     *
65
     * @var array
66
     */
67
    protected $data = [];
68
    /**
69
     * Owner object
70
     *
71
     * @var ObjectInterface
72
     */
73
    protected $object = null;
74
75
    /**
76
     * Meta properties constructor
77
     *
78
     * @param array $data Property data
79
     * @param ObjectInterface $object Owner object
80
     */
81 48
    public function __construct(array $data, ObjectInterface $object)
82
    {
83 48
        $this->data = $data;
84 48
        $this->object = $object;
85 48
    }
86
87
    /**
88
     * Return the owner object
89
     *
90
     * @return ObjectInterface Owner object
91
     */
92 3
    public function getObject()
93
    {
94 3
        return $this->object;
95
    }
96
97
    /**
98
     * Return the property values as array
99
     *
100
     * @param bool $serialize Serialize property objects
101
     * @return array Property values
102
     */
103 10
    public function toArray($serialize = true)
104
    {
105 10
        return $this->toSerializedArray($serialize, $this->data);
106
    }
107
108
    /**
109
     * Return the potentially serialized property values
110
     *
111
     * @param boolean $serialize Serialize objects
112
     * @param array $data Property values
113
     * @return array Serialized property values
114
     */
115 10
    protected function toSerializedArray($serialize, array $data)
116
    {
117
        // Filter all empty values
118 10
        $data = array_filter($data);
119
120
        // If the values should be serialized
121 10
        if ($serialize) {
122
            // Run through all properties
123 10
            while (list($property, $value) = each($data)) {
124
                // If the value is an array itself: Recurse
125 10
                if (is_array($value)) {
126 4
                    $data[$property] = $this->toSerializedArray($serialize, $value);
127
                    // Else if the value is serializable
128 10
                } elseif (is_object($value) &&
129 10
                    (new \ReflectionClass($value))->implementsInterface(SerializablePropertyInterface::class)
130
                ) {
131
                    /** @var $value SerializablePropertyInterface */
132 1
                    $data[$property] = $value->serialize();
133
                }
134
            }
135
        }
136
137 10
        return $data;
138
    }
139
140
    /**
141
     * Normalize and sort a property value list
142
     *
143
     * @param array $values Property values
144
     * @return array Normalized and sorted property values
145
     */
146 38
    protected function normalizeSortedPropertyValues(array $values)
147
    {
148 38
        $values = array_unique($values);
149 38
        sort($values, SORT_NATURAL);
150 38
        return $values;
151
    }
152
153
    /**
154
     * Mutate a string property
155
     *
156
     * @param string $property Property name
157
     * @param string $value New value
158
     * @return $this|AbstractProperties Self reference or clone
159
     */
160 4
    protected function mutateStringProperty($property, $value)
161
    {
162
163
        // If the new value differs from the current: Return clone
164 4
        if (strval($this->$property) !== strval($value)) {
165 4
            $collection = clone $this;
166 4
            $collection->$property = strval($value);
167 4
            return $collection;
168
        }
169
170
        // Else: return self reference
171 1
        return $this;
172
    }
173
174
    /**
175
     * Mutate a float property
176
     *
177
     * @param string $property Property name
178
     * @param float $value New value
179
     * @return $this|AbstractProperties Self reference or clone
180
     */
181 1
    protected function mutateFloatProperty($property, $value)
182
    {
183
184
        // If the new value differs from the current: Return clone
185 1
        if ($this->$property !== floatval($value)) {
186 1
            $collection = clone $this;
187 1
            $collection->$property = floatval($value);
188 1
            return $collection;
189
        }
190
191
        // Else: return self reference
192 1
        return $this;
193
    }
194
195
    /**
196
     * Mutate a list property
197
     *
198
     * @param string $property Property name
199
     * @param array $values New values
200
     * @return $this|AbstractProperties Self reference or clone
201
     */
202 1
    protected function mutateListProperty($property, array $values)
203
    {
204
        // If the new values differ from the current ones: Return clone
205 1
        if (array_diff($this->$property, $values) || array_diff($values, $this->$property)) {
206 1
            $collection = clone $this;
207 1
            $collection->$property = $values;
208 1
            return $collection;
209
        }
210
211
        // Else: return self reference
212 1
        return $this;
213
    }
214
215
    /**
216
     * Mutate a nested properties property
217
     *
218
     * @param string $property Property name
219
     * @param PropertiesInterface $value Nested properties
220
     * @return $this|AbstractProperties Self reference or clone
221
     */
222 1
    protected function mutatePropertiesProperty($property, PropertiesInterface $value)
223
    {
224
        // If the new value differs from the current one: Return clone
225 1
        if (spl_object_hash($this->$property) !== spl_object_hash($value)) {
226 1
            $collection = clone $this;
227 1
            $collection->$property = $value;
228 1
            return $collection;
229
        }
230
231
        // Else: return self reference
232 1
        return $this;
233
    }
234
}
235