Completed
Push — master ( 2ca52b...7d8595 )
by Joschi
02:51
created

AbstractGenericProperties::setProperty()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5.0061

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 5
eloc 16
nc 6
nop 2
dl 0
loc 29
rs 8.439
c 1
b 0
f 1
ccs 15
cts 16
cp 0.9375
crap 5.0061
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\Model\Object\ObjectInterface;
40
41
/**
42
 * Abstract generic object properties collection
43
 *
44
 * @package Apparat\Object
45
 * @subpackage Apparat\Object\Application
46
 */
47
abstract class AbstractGenericProperties extends AbstractProperties implements GenericPropertiesInterface
48
{
49
50
    /**
51
     * Property collection constructor
52
     *
53
     * @param array $data Property data
54
     * @param ObjectInterface $object Owner object
55
     */
56 18
    public function __construct(array $data, ObjectInterface $object)
57
    {
58 18
        parent::__construct($data, $object);
59 18
    }
60
61
    /**
62
     * Get a property value
63
     *
64
     * Multi-level properties might be traversed by property name paths separated with colons (":").
65
     *
66
     * @param string $property Property name
67
     * @return mixed Property value
68
     * @throws InvalidArgumentException If the property name is invalid
69
     */
70 2
    public function getProperty($property)
71
    {
72 2
        $propertyPath = $this->buildPropertyPath($property);
73
74
        // Traverse the property tree
75 1
        $propertyPathSteps = [];
76 1
        $data =& $this->data;
77 1
        foreach ($propertyPath as $property) {
78 1
            $propertyPathSteps[] = $property;
79
80
            // If the property name step is invalid
81 1
            if (!array_key_exists($property, $data)) {
82 1
                throw new InvalidArgumentException(
83
                    sprintf(
84 1
                        'Invalid property name "%s"',
85 1
                        implode(self::PROPERTY_TRAVERSAL_SEPARATOR, $propertyPathSteps)
86
                    ),
87 1
                    InvalidArgumentException::INVALID_PROPERTY_NAME
88
                );
89
            }
90
91 1
            $data =& $data[$property];
92
        }
93
94 1
        return $data;
95
    }
96
97
    /**
98
     * Translate a property name to a property path segments
99
     *
100
     * @param string $property Property name
101
     * @return array Property path
102
     * @throws InvalidArgumentException If the property name is empty
103
     */
104 4
    protected function buildPropertyPath($property)
105
    {
106 4
        $propertyPath = array_filter(array_map('trim', explode(self::PROPERTY_TRAVERSAL_SEPARATOR, $property)));
107
108
        // If the property traversal path is empty
109 4
        if (!count($propertyPath)) {
110 1
            throw new InvalidArgumentException('Empty property name', InvalidArgumentException::EMPTY_PROPERTY_NAME);
111
        }
112
113 3
        return $propertyPath;
114
    }
115
116
    /**
117
     * Set a property value
118
     *
119
     * Multi-level properties might be traversed by property name paths separated with colons (":").
120
     *
121
     * @param string $property Property name
122
     * @param mixed $value Property value
123
     * @return GenericPropertiesInterface Self reference
124
     */
125 2
    public function setProperty($property, $value)
126
    {
127 2
        $propertyPath = $this->buildPropertyPath($property);
128
129
        // Traverse the property tree
130 2
        $mutated = false;
131 2
        $propertyPathSteps = [];
132 2
        $dataRoot = $this->data;
133 2
        $data =& $dataRoot;
134 2
        foreach ($propertyPath as $property) {
135 2
            $propertyPathSteps[] = $property;
136
137
            // If the property name step is invalid
138 2
            if (!array_key_exists($property, $data)) {
139 1
                $data[$property] = [];
140 1
                $mutated = true;
141
            }
142
143 2
            $data =& $data[$property];
144
        }
145
146
        // If a new property is created with a non-empty value or an existing property is altered: Mutate
147 2
        if ($mutated ? !empty($value) : ($value !== $data)) {
148 2
            $data = $value;
149 2
            return new static($dataRoot, $this->object);
150
        }
151
152
        return $this;
153
    }
154
}
155