1 | <?php |
||||||
2 | |||||||
3 | namespace NovaFlexibleContent\Layouts\LayoutTraits; |
||||||
4 | |||||||
5 | use NovaFlexibleContent\Flexible; |
||||||
6 | use NovaFlexibleContent\Layouts\Layout; |
||||||
7 | |||||||
8 | trait HasFlexibleFieldInLayout |
||||||
9 | { |
||||||
10 | /** |
||||||
11 | * If layout contains sub flexible groups, find group by key recursive. |
||||||
12 | * |
||||||
13 | * @param string $groupKey |
||||||
14 | * @return Layout|null |
||||||
15 | */ |
||||||
16 | 4 | public function findFlexibleGroupRecursive(string $groupKey): ?Layout |
|||||
17 | { |
||||||
18 | 4 | if ($this->isUseKey($groupKey)) { |
|||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||||
19 | 1 | return $this; |
|||||
20 | } |
||||||
21 | |||||||
22 | 4 | foreach ($this->fieldsCollection() as $field) { |
|||||
0 ignored issues
–
show
It seems like
fieldsCollection() 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
![]() |
|||||||
23 | 4 | if ($field instanceof Flexible) { |
|||||
24 | 4 | if ($group = $field->findGroupRecursive($groupKey)) { |
|||||
25 | 3 | return $group; |
|||||
26 | } |
||||||
27 | } |
||||||
28 | } |
||||||
29 | |||||||
30 | 2 | return null; |
|||||
31 | } |
||||||
32 | |||||||
33 | /** |
||||||
34 | * TODO: rebuild |
||||||
35 | */ |
||||||
36 | 2 | public function findGroupRecursiveAndSetAttribute($groupKey, $fieldKey, $newValue): bool |
|||||
37 | { |
||||||
38 | 2 | $data = $this->getAttributes(); |
|||||
0 ignored issues
–
show
It seems like
getAttributes() 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
![]() |
|||||||
39 | |||||||
40 | 2 | if ($this->isUseKey($groupKey)) { |
|||||
41 | 1 | if (array_key_exists($fieldKey, $data)) { |
|||||
42 | 1 | $this->setAttribute($fieldKey, $newValue); |
|||||
0 ignored issues
–
show
The method
setAttribute() does not exist on NovaFlexibleContent\Layo...asFlexibleFieldInLayout . Did you maybe mean setAttributeValue() ?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||||
43 | |||||||
44 | 1 | return true; |
|||||
45 | } |
||||||
46 | |||||||
47 | 1 | return false; |
|||||
48 | } |
||||||
49 | |||||||
50 | 2 | $result = $this->setAttributeValue($data, $groupKey, $fieldKey, $newValue); |
|||||
51 | |||||||
52 | 2 | $this->attributes = $data; |
|||||
0 ignored issues
–
show
|
|||||||
53 | |||||||
54 | 2 | return $result; |
|||||
55 | } |
||||||
56 | |||||||
57 | 2 | public function setAttributeValue(array &$array, string $groupKey, string $fieldKey, mixed $newValue): bool |
|||||
58 | { |
||||||
59 | 2 | foreach ($array as $key => $value) { |
|||||
60 | 2 | if (is_object($value) |
|||||
61 | 2 | && property_exists($value, 'key') |
|||||
62 | 2 | && property_exists($value, 'attributes') |
|||||
63 | 2 | && $value->key === $groupKey |
|||||
64 | 2 | && is_object($value->attributes)) { |
|||||
65 | 2 | foreach ($value->attributes as $attribute => $attrValue) { |
|||||
66 | 2 | if ($attribute === $fieldKey) { |
|||||
67 | 2 | $value->attributes->$attribute = $newValue; |
|||||
68 | |||||||
69 | 2 | return true; |
|||||
70 | } |
||||||
71 | } |
||||||
72 | } |
||||||
73 | 2 | if (is_array($value)) { |
|||||
74 | 2 | if ($this->setAttributeValue($array[$key], $groupKey, $fieldKey, $newValue)) { |
|||||
75 | 2 | return true; |
|||||
76 | } |
||||||
77 | } |
||||||
78 | } |
||||||
79 | |||||||
80 | 1 | return false; |
|||||
81 | } |
||||||
82 | } |
||||||
83 |