1 | <?php |
||||||
2 | /** |
||||||
3 | * |
||||||
4 | * This file is part of the Aura project for PHP. |
||||||
5 | * |
||||||
6 | * @package Aura.Marshal |
||||||
7 | * |
||||||
8 | * @license https://opensource.org/licenses/mit-license.php MIT |
||||||
9 | * |
||||||
10 | */ |
||||||
11 | namespace Aura\Marshal\Entity; |
||||||
12 | |||||||
13 | use Aura\Marshal\Lazy\LazyInterface; |
||||||
14 | |||||||
15 | /** |
||||||
16 | * |
||||||
17 | * Use this trait for magic __get(), __set(), __isset(), and __unset() on any |
||||||
18 | * entity class that implements ArrayAccess for its fields. |
||||||
19 | * |
||||||
20 | * @package Aura.Marshal |
||||||
21 | * |
||||||
22 | */ |
||||||
23 | trait MagicArrayAccessTrait |
||||||
24 | { |
||||||
25 | /** |
||||||
26 | * |
||||||
27 | * Calls ArrayAccess::offsetGet() to get a field value; converts Lazy |
||||||
28 | * objects for related entities and collections in place. |
||||||
29 | * |
||||||
30 | * @param string $field The field name to get. |
||||||
31 | * |
||||||
32 | * @return mixed The field value. |
||||||
33 | * |
||||||
34 | */ |
||||||
35 | public function __get($field) |
||||||
36 | { |
||||||
37 | // get the field value |
||||||
38 | $value = $this->offsetGet($field); |
||||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||||
39 | |||||||
40 | // is it a Lazy placeholder? |
||||||
41 | if ($value instanceof LazyInterface) { |
||||||
42 | // replace the Lazy placeholder with the real object |
||||||
43 | $value = $value->get($this); |
||||||
44 | $this->offsetSet($field, $value); |
||||||
0 ignored issues
–
show
It seems like
offsetSet() 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
![]() |
|||||||
45 | } |
||||||
46 | |||||||
47 | // done! |
||||||
48 | return $value; |
||||||
49 | } |
||||||
50 | |||||||
51 | /** |
||||||
52 | * |
||||||
53 | * Calls ArrayAccess::offsetSet() to set a field value. |
||||||
54 | * |
||||||
55 | * @param string $field The field name to get. |
||||||
56 | * |
||||||
57 | * @param mixed $value Set the field to this value. |
||||||
58 | * |
||||||
59 | * @return void |
||||||
60 | * |
||||||
61 | */ |
||||||
62 | public function __set($field, $value) |
||||||
63 | { |
||||||
64 | $this->offsetSet($field, $value); |
||||||
65 | } |
||||||
66 | |||||||
67 | /** |
||||||
68 | * |
||||||
69 | * Calls ArrayAccess::offsetExists() to see if a field is set. |
||||||
70 | * |
||||||
71 | * @param string $field The field name to check. |
||||||
72 | * |
||||||
73 | * @return bool |
||||||
74 | * |
||||||
75 | */ |
||||||
76 | public function __isset($field) |
||||||
77 | { |
||||||
78 | return $this->offsetExists($field); |
||||||
0 ignored issues
–
show
It seems like
offsetExists() 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
![]() |
|||||||
79 | } |
||||||
80 | |||||||
81 | /** |
||||||
82 | * |
||||||
83 | * Calls ArrayAccess::offsetUnset() to unset a field. |
||||||
84 | * |
||||||
85 | * @param string $field The field name to unset. |
||||||
86 | * |
||||||
87 | * @return void |
||||||
88 | * |
||||||
89 | */ |
||||||
90 | public function __unset($field) |
||||||
91 | { |
||||||
92 | $this->offsetUnset($field); |
||||||
0 ignored issues
–
show
It seems like
offsetUnset() 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
![]() |
|||||||
93 | } |
||||||
94 | } |
||||||
95 |