Passed
Push — master ( acd513...675cc6 )
by Andrea Marco
02:53 queued 11s
created

TurnsIntoArray::toSnakeCaseArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 10
ccs 6
cts 6
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
     * Retrieve the DTO as an array
22
     *
23
     * @return array
24
     */
25 54
    public function toArray(): array
26
    {
27 54
        $data = [];
28
29 54
        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

29
        foreach ($this->/** @scrutinizer ignore-call */ getPropertiesMap() as $name => $property) {
Loading history...
30 54
            $data[$name] = ArrayConverter::instance()->convert($property->value());
31
        }
32
33 54
        return $data;
34
    }
35
36
    /**
37
     * Retrieve the DTO as an array with snake-case keys
38
     *
39
     * @return array
40
     */
41 3
    public function toSnakeCaseArray(): array
42
    {
43 3
        $data = [];
44
45 3
        foreach ($this->getPropertiesMap() as $name => $property) {
46 3
            $key = strtolower(preg_replace(ArrayConverter::RE_SNAKE_CASE, '_', $name));
47 3
            $data[$key] = ArrayConverter::instance()->convert($property->value(), true);
48
        }
49
50 3
        return $data;
51
    }
52
53
    /**
54
     * Retrieve the DTO as an iterator
55
     *
56
     * @return Traversable
57
     */
58 15
    public function getIterator(): Traversable
59
    {
60 15
        return new ArrayIterator($this->toArray(), ArrayIterator::ARRAY_AS_PROPS);
61
    }
62
63
    /**
64
     * Determine whether a given property has a value
65
     *
66
     * @param mixed $property
67
     * @return bool
68
     */
69 18
    public function offsetExists($property): bool
70
    {
71 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

71
        return $this->/** @scrutinizer ignore-call */ has($property);
Loading history...
72
    }
73
74
    /**
75
     * Retrieve the given property value
76
     *
77
     * @param mixed $property
78
     * @return mixed
79
     * @throws UnknownDtoPropertyException
80
     */
81 33
    public function &offsetGet($property)
82
    {
83 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

83
        /** @scrutinizer ignore-call */ 
84
        $value = $this->get($property);
Loading history...
84
85 27
        return $value;
86
    }
87
88
    /**
89
     * Set the given property to the provided value
90
     *
91
     * @param mixed $property
92
     * @param mixed $value
93
     * @return void
94
     * @throws ImmutableDtoException
95
     * @throws UnknownDtoPropertyException
96
     */
97 12
    public function offsetSet($property, $value): void
98
    {
99 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

99
        if (!($this->/** @scrutinizer ignore-call */ getFlags() & MUTABLE)) {
Loading history...
100 6
            throw new ImmutableDtoException(static::class);
101
        }
102
103 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

103
        $this->/** @scrutinizer ignore-call */ 
104
               set($property, $value);
Loading history...
104 6
    }
105
106
    /**
107
     * Set the given property to the provided value
108
     *
109
     * @param mixed $property
110
     * @return void
111
     * @throws ImmutableDtoException
112
     * @throws UnsetDtoPropertyException
113
     * @throws UnknownDtoPropertyException
114
     */
115 9
    public function offsetUnset($property): void
116
    {
117 9
        if (!($this->getFlags() & MUTABLE)) {
118 3
            throw new ImmutableDtoException(static::class);
119
        }
120
121 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

121
        $this->/** @scrutinizer ignore-call */ 
122
               unset($property);
Loading history...
122 3
    }
123
}
124