Passed
Push — master ( d32d3d...0995a0 )
by Andrea Marco
02:44 queued 12s
created

TurnsIntoArray::offsetSet()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
1
<?php
2
3
namespace Cerbero\Dto\Traits;
4
5
use ArrayIterator;
6
use Cerbero\Dto\Exceptions\ImmutableDtoException;
7
use Cerbero\Dto\Exceptions\UnknownDtoPropertyException;
8
use Cerbero\Dto\Exceptions\UnsetDtoPropertyException;
9
use Cerbero\Dto\Manipulators\ArrayConverter;
10
use Traversable;
11
12
use const Cerbero\Dto\MUTABLE;
0 ignored issues
show
Bug introduced by
The constant Cerbero\Dto\MUTABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
13
14
/**
15
 * Trait to turn a DTO into an array.
16
 *
17
 */
18
trait TurnsIntoArray
19
{
20
    /**
21
     * The array converter.
22
     *
23
     * @var ArrayConverter
24
     */
25
    protected static $arrayConverter;
26
27
    /**
28
     * Retrieve the array converter
29
     *
30
     * @return ArrayConverter
31
     */
32 39
    public static function getArrayConverter(): ArrayConverter
33
    {
34 39
        return static::$arrayConverter ?: ArrayConverter::instance();
35
    }
36
37
    /**
38
     * Set the given array converter
39
     *
40
     * @param ArrayConverter
41
     * @return void
42
     */
43 3
    public static function setArrayConverter(ArrayConverter $converter): void
44
    {
45 3
        static::$arrayConverter = $converter;
46 3
    }
47
48
    /**
49
     * Retrieve the DTO as an array
50
     *
51
     * @return array
52
     */
53 36
    public function toArray(): array
54
    {
55 36
        $data = [];
56
57 36
        foreach ($this->getPropertiesMap() as $name => $property) {
0 ignored issues
show
Bug introduced by
It seems like getPropertiesMap() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

57
        foreach ($this->/** @scrutinizer ignore-call */ getPropertiesMap() as $name => $property) {
Loading history...
58 36
            $data[$name] = static::getArrayConverter()->convert($property->value());
59
        }
60
61 36
        return $data;
62
    }
63
64
    /**
65
     * Retrieve the DTO as an iterator
66
     *
67
     * @return Traversable
68
     */
69 12
    public function getIterator(): Traversable
70
    {
71 12
        return new ArrayIterator($this->toArray(), ArrayIterator::ARRAY_AS_PROPS);
72
    }
73
74
    /**
75
     * Determine whether a given property has a value
76
     *
77
     * @param mixed $property
78
     * @return bool
79
     */
80 18
    public function offsetExists($property): bool
81
    {
82 18
        return $this->has($property);
0 ignored issues
show
Bug introduced by
It seems like has() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

82
        return $this->/** @scrutinizer ignore-call */ has($property);
Loading history...
83
    }
84
85
    /**
86
     * Retrieve the given property value
87
     *
88
     * @param mixed $property
89
     * @return mixed
90
     * @throws UnknownDtoPropertyException
91
     */
92 33
    public function &offsetGet($property)
93
    {
94 33
        $value = $this->get($property);
0 ignored issues
show
Bug introduced by
It seems like get() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

94
        /** @scrutinizer ignore-call */ 
95
        $value = $this->get($property);
Loading history...
95
96 27
        return $value;
97
    }
98
99
    /**
100
     * Set the given property to the provided value
101
     *
102
     * @param mixed $property
103
     * @param mixed $value
104
     * @return void
105
     * @throws ImmutableDtoException
106
     * @throws UnknownDtoPropertyException
107
     */
108 12
    public function offsetSet($property, $value): void
109
    {
110 12
        if (!($this->getFlags() & MUTABLE)) {
0 ignored issues
show
Bug introduced by
It seems like getFlags() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

110
        if (!($this->/** @scrutinizer ignore-call */ getFlags() & MUTABLE)) {
Loading history...
111 6
            throw new ImmutableDtoException(static::class);
112
        }
113
114 6
        $this->set($property, $value);
0 ignored issues
show
Bug introduced by
It seems like set() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

114
        $this->/** @scrutinizer ignore-call */ 
115
               set($property, $value);
Loading history...
115 6
    }
116
117
    /**
118
     * Set the given property to the provided value
119
     *
120
     * @param mixed $property
121
     * @return void
122
     * @throws ImmutableDtoException
123
     * @throws UnsetDtoPropertyException
124
     * @throws UnknownDtoPropertyException
125
     */
126 9
    public function offsetUnset($property): void
127
    {
128 9
        if (!($this->getFlags() & MUTABLE)) {
129 3
            throw new ImmutableDtoException(static::class);
130
        }
131
132 6
        $this->unset($property);
0 ignored issues
show
Bug introduced by
It seems like unset() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

132
        $this->/** @scrutinizer ignore-call */ 
133
               unset($property);
Loading history...
133 3
    }
134
}
135