|
12 | 12 |
|
13 | 13 | class RepeaterConverter extends BaseConverter |
14 | 14 | { |
15 | | - public function toDisplayValue(mixed $sourceValue, ?string $locale, ?string $fallbackLocale) |
16 | | - { |
17 | | - |
18 | | - $fieldTypeConfig = $this->fieldTypeConfig; |
19 | | - |
20 | | - if (! $fieldTypeConfig instanceof Repeater) { |
21 | | - return []; |
22 | | - } |
23 | | - |
24 | | - if (! is_array($sourceValue)) { |
25 | | - $sourceValue = []; |
26 | | - } |
27 | | - |
28 | | - $newValue = []; |
29 | | - |
30 | | - foreach ($sourceValue as $i => $data) { |
31 | | - |
32 | | - $propData = PropertyDataCollection::make(); |
33 | | - |
34 | | - foreach ($data as $key => $value) { |
35 | | - |
36 | | - $field = collect($fieldTypeConfig->fields)->firstWhere('name', $key); |
37 | | - |
38 | | - if (is_null($field) || ! isset($field['field']) || blank($field['field'])) { |
39 | | - continue; |
40 | | - } |
41 | | - |
42 | | - $innerFieldTypeName = $field['field']; |
43 | | - |
44 | | - $innerPropertyType = FieldTypeHelper::getFieldTypeConfig($innerFieldTypeName, $field['fieldConfig'] ?? []); |
45 | | - if (is_null($innerPropertyType)) { |
46 | | - continue; |
47 | | - } |
48 | | - |
49 | | - $innerFieldConverter = $this->tryGetConverterForInnerField($innerPropertyType); |
50 | | - if (is_null($innerFieldConverter)) { |
51 | | - continue; |
52 | | - } |
53 | | - |
54 | | - $finalValue = null; |
55 | | - $attempt = $this->tryGetDisplayValueForInnerField($innerFieldConverter, $value, $locale, $fallbackLocale, $finalValue); |
56 | | - |
57 | | - $innerPropTypeDto = PropertyTypeDto::fromArray([ |
58 | | - 'key' => $key, |
59 | | - 'group' => $i, |
60 | | - 'config' => $innerPropertyType, |
61 | | - ]); |
62 | | - $newInnerValue = PropertyDataDto::fromArray([ |
63 | | - 'key' => $key, |
64 | | - 'value' => $finalValue, |
65 | | - 'propertyType' => $innerPropTypeDto, |
66 | | - ]) |
67 | | - ->setFallbackLocale($fallbackLocale); |
68 | | - |
69 | | - $propData->push($newInnerValue); |
70 | | - |
71 | | - } |
72 | | - |
73 | | - $newValue[$i] = PropertyDataGroupDto::fromArray([ |
74 | | - 'key' => $i, |
75 | | - 'data' => $propData, |
76 | | - 'propertyTypes' => collect($propData) |
77 | | - ->mapWithKeys(fn ($p) => [ |
78 | | - $p->key => $p->propertyType, |
79 | | - ]), |
80 | | - ]); |
81 | | - } |
82 | | - |
83 | | - return $newValue; |
84 | | - } |
85 | | - |
86 | | - /** |
87 | | - * @param BaseConverter $convert |
88 | | - * @param mixed $sourceValue |
89 | | - * @param ?string $locale |
90 | | - * @param ?string $fallbackLocale |
91 | | - * @param mixed $finalValue |
92 | | - * @return bool |
93 | | - */ |
94 | | - private function tryGetDisplayValueForInnerField($convert, $sourceValue, $locale, $fallbackLocale, &$finalValue = null) |
95 | | - { |
96 | | - try { |
97 | | - |
98 | | - $finalValue = $convert->toDisplayValue($sourceValue, $locale, $fallbackLocale); |
99 | | - |
100 | | - return true; |
101 | | - |
102 | | - } catch (\Throwable $th) { |
103 | | - |
104 | | - return false; |
105 | | - |
106 | | - } |
107 | | - } |
108 | | - |
109 | | - private function tryGetConverterForInnerField($fieldTypeConfig): ?BaseConverter |
110 | | - { |
111 | | - try { |
112 | | - |
113 | | - $transformer = app(PropertyValueTransformerInterface::class); |
114 | | - |
115 | | - return $transformer->getConverter($fieldTypeConfig); |
116 | | - |
117 | | - } catch (\Throwable $th) { |
118 | | - |
119 | | - return null; |
120 | | - |
121 | | - } |
122 | | - } |
| 15 | + public function toDisplayValue(mixed $sourceValue, ?string $locale, ?string $fallbackLocale) |
| 16 | + { |
| 17 | + |
| 18 | + $fieldTypeConfig = $this->fieldTypeConfig; |
| 19 | + |
| 20 | + if (! $fieldTypeConfig instanceof Repeater) { |
| 21 | + return []; |
| 22 | + } |
| 23 | + |
| 24 | + if (! is_array($sourceValue)) { |
| 25 | + $sourceValue = []; |
| 26 | + } |
| 27 | + |
| 28 | + $newValue = []; |
| 29 | + |
| 30 | + foreach ($sourceValue as $i => $data) { |
| 31 | + |
| 32 | + $propData = PropertyDataCollection::make(); |
| 33 | + |
| 34 | + foreach ($data as $key => $value) { |
| 35 | + |
| 36 | + $field = collect($fieldTypeConfig->fields)->firstWhere('name', $key); |
| 37 | + |
| 38 | + if (is_null($field) || ! isset($field['field']) || blank($field['field'])) { |
| 39 | + continue; |
| 40 | + } |
| 41 | + |
| 42 | + $innerFieldTypeName = $field['field']; |
| 43 | + |
| 44 | + $innerPropertyType = FieldTypeHelper::getFieldTypeConfig($innerFieldTypeName, $field['fieldConfig'] ?? []); |
| 45 | + if (is_null($innerPropertyType)) { |
| 46 | + continue; |
| 47 | + } |
| 48 | + |
| 49 | + $innerFieldConverter = $this->tryGetConverterForInnerField($innerPropertyType); |
| 50 | + if (is_null($innerFieldConverter)) { |
| 51 | + continue; |
| 52 | + } |
| 53 | + |
| 54 | + $finalValue = null; |
| 55 | + $attempt = $this->tryGetDisplayValueForInnerField($innerFieldConverter, $value, $locale, $fallbackLocale, $finalValue); |
| 56 | + |
| 57 | + $innerPropTypeDto = PropertyTypeDto::fromArray([ |
| 58 | + 'key' => $key, |
| 59 | + 'group' => $i, |
| 60 | + 'config' => $innerPropertyType, |
| 61 | + ]); |
| 62 | + $newInnerValue = PropertyDataDto::fromArray([ |
| 63 | + 'key' => $key, |
| 64 | + 'value' => $finalValue, |
| 65 | + 'propertyType' => $innerPropTypeDto, |
| 66 | + ]) |
| 67 | + ->setFallbackLocale($fallbackLocale); |
| 68 | + |
| 69 | + $propData->push($newInnerValue); |
| 70 | + |
| 71 | + } |
| 72 | + |
| 73 | + $newValue[$i] = PropertyDataGroupDto::fromArray([ |
| 74 | + 'key' => $i, |
| 75 | + 'data' => $propData, |
| 76 | + 'propertyTypes' => collect($propData) |
| 77 | + ->mapWithKeys(fn ($p) => [ |
| 78 | + $p->key => $p->propertyType, |
| 79 | + ]), |
| 80 | + ]); |
| 81 | + } |
| 82 | + |
| 83 | + return $newValue; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * @param BaseConverter $convert |
| 88 | + * @param mixed $sourceValue |
| 89 | + * @param ?string $locale |
| 90 | + * @param ?string $fallbackLocale |
| 91 | + * @param mixed $finalValue |
| 92 | + * @return bool |
| 93 | + */ |
| 94 | + private function tryGetDisplayValueForInnerField($convert, $sourceValue, $locale, $fallbackLocale, &$finalValue = null) |
| 95 | + { |
| 96 | + try { |
| 97 | + |
| 98 | + $finalValue = $convert->toDisplayValue($sourceValue, $locale, $fallbackLocale); |
| 99 | + |
| 100 | + return true; |
| 101 | + |
| 102 | + } catch (\Throwable $th) { |
| 103 | + |
| 104 | + return false; |
| 105 | + |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + private function tryGetConverterForInnerField($fieldTypeConfig): ?BaseConverter |
| 110 | + { |
| 111 | + try { |
| 112 | + |
| 113 | + $transformer = app(PropertyValueTransformerInterface::class); |
| 114 | + |
| 115 | + return $transformer->getConverter($fieldTypeConfig); |
| 116 | + |
| 117 | + } catch (\Throwable $th) { |
| 118 | + |
| 119 | + return null; |
| 120 | + |
| 121 | + } |
| 122 | + } |
123 | 123 | } |
0 commit comments