Completed
Push — master ( 68c6f3...91ba10 )
by Joschi
02:27
created

AbstractProperties::mutateStringProperty()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 12
ccs 5
cts 6
cp 0.8333
rs 9.4285
cc 2
eloc 6
nc 2
nop 2
crap 2.0185
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 object properties collection
43
 *
44
 * @package Apparat\Object
45
 * @subpackage Apparat\Object\Application
46
 */
47
abstract class AbstractProperties implements PropertiesInterface
48
{
49
    /**
50
     * Property data
51
     *
52
     * @var array
53
     */
54
    protected $data = [];
55
    /**
56
     * Owner object
57
     *
58
     * @var ObjectInterface
59
     */
60
    protected $object = null;
61
62
    /**
63
     * Meta properties constructor
64
     *
65
     * @param array $data Property data
66
     * @param ObjectInterface $object Owner object
67
     */
68 20
    public function __construct(array $data, ObjectInterface $object)
69
    {
70 20
        $this->data = $data;
71 20
        $this->object = $object;
72 20
    }
73
74
    /**
75
     * Return the owner object
76
     *
77
     * @return ObjectInterface Owner object
78
     */
79 15
    public function getObject()
80
    {
81 15
        return $this->object;
82
    }
83
84
    /**
85
     * Normalize and sort a property value list
86
     *
87
     * @param array $values Property values
88
     * @return array Normalized and sorted property values
89
     */
90 15
    protected function normalizeSortedPropertyValues(array $values) {
91 15
        $values = array_unique($values);
92 15
        sort($values, SORT_NATURAL);
93 15
        return $values;
94
    }
95
96
    /**
97
     * Mutate a string property
98
     *
99
     * @param string $property Property name
100
     * @param string $value New value
101
     * @return $this|AbstractProperties Self reference or clone
102
     */
103 1
    protected function mutateStringProperty($property, $value) {
104
105
        // If the new value differs from the current: Return clone
106 1
        if (strval($this->$property) !== strval($value)) {
107 1
            $collection = clone $this;
108 1
            $collection->$property = strval($value);
109 1
            return $collection;
110
        }
111
112
        // Else: return self reference
113
        return $this;
114
    }
115
116
    /**
117
     * Mutate a list property
118
     *
119
     * @param string $property Property name
120
     * @param array $values New values
121
     * @return $this|AbstractProperties Self reference or clone
122
     */
123
    protected function mutateListProperty($property, array $values) {
124
125
        // If the new values differ from the current ones: Return clone
126
        if (array_diff($this->$property, $values) || array_diff($values, $this->$property)) {
127
            $collection = clone $this;
128
            $collection->$property = $values;
129
            return $collection;
130
        }
131
132
        // Else: return self reference
133
        return $this;
134
    }
135
}
136