Skip to content

Commit 1e56ffe

Browse files
committed
refactor: enhance BaseConverter and related classes with key and group parameters
1 parent 678454d commit 1e56ffe

4 files changed

Lines changed: 71 additions & 9 deletions

File tree

src/Fields/Converters/BaseConverter.php

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,38 @@ abstract class BaseConverter
1111
{
1212
use Macroable;
1313

14-
protected FieldTypeConfig $fieldTypeConfig;
14+
/**
15+
* @var FieldTypeConfig
16+
*/
17+
protected $fieldTypeConfig;
18+
19+
/**
20+
* @var string|null
21+
*/
22+
protected $fieldKey;
23+
24+
/**
25+
* @var string|null
26+
*/
27+
protected $fieldGroupKey;
1528

1629
/**
1730
* @var array<class-string<BaseConverter>, array<Closure>>
1831
*/
1932
protected static $configurations = [];
2033

21-
public function __construct(FieldTypeConfig $fieldTypeConfig)
34+
/**
35+
* @param \SolutionForest\FilamentFieldGroup\FieldTypes\Configs\Contracts\FieldTypeConfig $fieldTypeConfig
36+
* @param ?string $group
37+
* @param ?string $key
38+
*/
39+
public function __construct(FieldTypeConfig $fieldTypeConfig, $group, $key)
2240
{
2341
$this->fieldTypeConfig = $fieldTypeConfig;
2442

43+
$this->fieldKey = $key;
44+
$this->fieldGroupKey = $group;
45+
2546
$this->configure();
2647
}
2748

@@ -42,7 +63,35 @@ protected function applyLocaleConversion(mixed $sourceValue, ?string $locale, ?s
4263

4364
protected function isFieldTypeTranslatable(): bool
4465
{
45-
return $this->fieldTypeConfig->isTranslatable();
66+
return $this->getFieldTypeConfig()->isTranslatable();
67+
}
68+
69+
public function getFieldTypeConfig(): FieldTypeConfig
70+
{
71+
return $this->fieldTypeConfig;
72+
}
73+
74+
/**
75+
* @return string|null
76+
*/
77+
public function getKey()
78+
{
79+
return $this->fieldKey;
80+
}
81+
82+
/**
83+
* @return string|null
84+
*/
85+
public function getGroup()
86+
{
87+
return $this->fieldGroupKey;
88+
}
89+
90+
public function getFieldIdentifier(): string
91+
{
92+
return collect([$this->getGroup(), $this->getKey()])
93+
->filter()
94+
->implode('.');
4695
}
4796

4897
public static function configureUsing(Closure $modifyUsing)

src/Fields/Converters/RepeaterConverter.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,12 @@ public function toDisplayValue(mixed $sourceValue, ?string $locale, ?string $fal
4646
continue;
4747
}
4848

49-
$innerFieldConverter = $this->tryGetConverterForInnerField($innerPropertyType);
49+
$innerFieldConverter = $this->tryGetConverterForInnerField(
50+
$innerPropertyType,
51+
implode('.', [$this->getFieldIdentifier(), $i]),
52+
$key
53+
);
54+
5055
if (is_null($innerFieldConverter)) {
5156
continue;
5257
}
@@ -106,13 +111,14 @@ private function tryGetDisplayValueForInnerField($convert, $sourceValue, $locale
106111
}
107112
}
108113

109-
private function tryGetConverterForInnerField($fieldTypeConfig): ?BaseConverter
114+
private function tryGetConverterForInnerField($fieldTypeConfig, $group, $key): ?BaseConverter
110115
{
111116
try {
112117

113118
$transformer = app(PropertyValueTransformerInterface::class);
119+
114120

115-
return $transformer->getConverter($fieldTypeConfig);
121+
return $transformer->getConverter($fieldTypeConfig, $key, $group);
116122

117123
} catch (\Throwable $th) {
118124

src/Fields/PropertyValueTransformer.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ class PropertyValueTransformer implements PropertyValueTransformerInterface
1414
{
1515
public function transform(PropertyDataDto $propertyDataDto, ?string $locale, ?string $fallbackLocale)
1616
{
17-
$converter = $this->getConverter($propertyDataDto?->propertyType?->config);
17+
$propType = $propertyDataDto->propertyType;
18+
19+
$converter = $this->getConverter($propType?->config, $propType?->key, $propType?->group);
1820

1921
return $converter->toDisplayValue($propertyDataDto->getSourceValue(), $locale, $fallbackLocale);
2022
}
@@ -34,7 +36,7 @@ public function attemptTransform(PropertyDataDto $propertyDataDto, ?string $loca
3436
}
3537
}
3638

37-
public function getConverter($fieldType)
39+
public function getConverter($fieldType, $key, $group)
3840
{
3941
if ($fieldType === null) {
4042
throw new \InvalidArgumentException('No field type specified.');
@@ -57,6 +59,8 @@ public function getConverter($fieldType)
5759

5860
return app($converter, [
5961
'fieldTypeConfig' => $fieldType,
62+
'key' => $key,
63+
'group' => $group,
6064
]);
6165
}
6266
}

src/Fields/PropertyValueTransformerInterface.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@ public function attemptTransform(PropertyDataDto $propertyDataDto, ?string $loca
1616
* Get the converter for the specified field type.
1717
*
1818
* @param ?FieldTypeConfig $fieldType The type of the field for which the converter is needed.
19+
* @param ?string $key The key of the field.
20+
* @param ?string $group The group of the field.
21+
*
1922
* @return BaseConverter The converter for the specified field type.
2023
*
2124
* @throws \InvalidArgumentException If no field type is specified, or if no converter is found for the specified field type.
2225
*/
23-
public function getConverter($fieldType);
26+
public function getConverter($fieldType, $key, $group);
2427
}

0 commit comments

Comments
 (0)