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
![]() |
|||||||
13 | use const Cerbero\Dto\MUTABLE; |
||||||
0 ignored issues
–
show
|
|||||||
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
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
![]() |
|||||||
30 | |||||||
31 | 26 | foreach ($this->getPropertiesMap() as $name => $property) { |
|||||
0 ignored issues
–
show
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
![]() |
|||||||
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
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
![]() |
|||||||
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
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
![]() |
|||||||
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
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
![]() |
|||||||
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
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
![]() |
|||||||
108 | 1 | } |
|||||
109 | } |
||||||
110 |