TurnsIntoArray::offsetExists()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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\CAMEL_CASE_ARRAY;
0 ignored issues
show
Bug introduced by
The constant Cerbero\Dto\CAMEL_CASE_ARRAY was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
13
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...
14
15
/**
16
 * Trait to turn a DTO into an array.
17
 *
18
 */
19
trait TurnsIntoArray
20
{
21
    /**
22
     * Retrieve the DTO as an array
23
     *
24
     * @return array
25
     */
26 26
    public function toArray(): array
27
    {
28 26
        $data = [];
29 26
        $snakeCase = !($this->getFlags() & CAMEL_CASE_ARRAY);
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

29
        $snakeCase = !($this->/** @scrutinizer ignore-call */ getFlags() & CAMEL_CASE_ARRAY);
Loading history...
30
31 26
        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

31
        foreach ($this->/** @scrutinizer ignore-call */ getPropertiesMap() as $name => $property) {
Loading history...
32 23
            $key = ArrayConverter::instance()->formatKey($name, $snakeCase);
33 23
            $data[$key] = ArrayConverter::instance()->convert($property->value(), $snakeCase);
34
        }
35
36 26
        return $data;
37
    }
38
39
    /**
40
     * Retrieve the DTO as an iterator
41
     *
42
     * @return Traversable
43
     */
44 5
    public function getIterator(): Traversable
45
    {
46 5
        return new ArrayIterator($this->toArray(), ArrayIterator::ARRAY_AS_PROPS);
47
    }
48
49
    /**
50
     * Determine whether a given property has a value
51
     *
52
     * @param mixed $property
53
     * @return bool
54
     */
55 6
    public function offsetExists($property): bool
56
    {
57 6
        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

57
        return $this->/** @scrutinizer ignore-call */ has($property);
Loading history...
58
    }
59
60
    /**
61
     * Retrieve the given property value
62
     *
63
     * @param mixed $property
64
     * @return mixed
65
     * @throws UnknownDtoPropertyException
66
     */
67 11
    public function &offsetGet($property)
68
    {
69 11
        $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

69
        /** @scrutinizer ignore-call */ 
70
        $value = $this->get($property);
Loading history...
70
71 9
        return $value;
72
    }
73
74
    /**
75
     * Set the given property to the provided value
76
     *
77
     * @param mixed $property
78
     * @param mixed $value
79
     * @return void
80
     * @throws ImmutableDtoException
81
     * @throws UnknownDtoPropertyException
82
     */
83 6
    public function offsetSet($property, $value): void
84
    {
85 6
        if (!($this->getFlags() & MUTABLE)) {
86 2
            throw new ImmutableDtoException(static::class);
87
        }
88
89 4
        $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

89
        $this->/** @scrutinizer ignore-call */ 
90
               set($property, $value);
Loading history...
90 4
    }
91
92
    /**
93
     * Set the given property to the provided value
94
     *
95
     * @param mixed $property
96
     * @return void
97
     * @throws ImmutableDtoException
98
     * @throws UnsetDtoPropertyException
99
     * @throws UnknownDtoPropertyException
100
     */
101 3
    public function offsetUnset($property): void
102
    {
103 3
        if (!($this->getFlags() & MUTABLE)) {
104 1
            throw new ImmutableDtoException(static::class);
105
        }
106
107 2
        $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

107
        $this->/** @scrutinizer ignore-call */ 
108
               unset($property);
Loading history...
108 1
    }
109
}
110