Skip to content

Commit c533452

Browse files
committed
Merge remote-tracking branch 'origin/develop' into develop
# Conflicts: # src/Fields/Mixins/FieldTypeConverter.php
2 parents a1ebfd0 + efbcac3 commit c533452

11 files changed

Lines changed: 157 additions & 157 deletions

src/Fields/Configs/Attributes/Converter.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Attribute;
66
use SolutionForest\InspireCms\Fields\Converters\BaseConverter;
7+
78
/**
89
* @property class-string<BaseConvert> $converter The converter class to use for this field type.
910
*/

src/Fields/Converters/BaseConverter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ public function __construct(FieldTypeConfig $fieldTypeConfig)
1313
{
1414
$this->fieldTypeConfig = $fieldTypeConfig;
1515
}
16-
17-
public abstract function toDisplayValue(mixed $sourceValue, ?string $locale, ?string $fallbackLocale);
16+
17+
abstract public function toDisplayValue(mixed $sourceValue, ?string $locale, ?string $fallbackLocale);
1818

1919
protected function applyLocaleConversion(mixed $sourceValue, ?string $locale, ?string $fallbackLocale)
2020
{

src/Fields/Converters/ContentPickerConverter.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
class ContentPickerConverter extends BaseConverter
88
{
9-
public function toDisplayValue(mixed $sourceValue, string|null $locale, string|null $fallbackLocale)
10-
{
9+
public function toDisplayValue(mixed $sourceValue, ?string $locale, ?string $fallbackLocale)
10+
{
1111
// todo: improve performance
1212
$content = inspirecms_content()->findPublishedContentByIds($sourceValue)
1313
->filter(fn ($c) => in_array($c->getKey(), $sourceValue))
@@ -25,5 +25,5 @@ public function toDisplayValue(mixed $sourceValue, string|null $locale, string|n
2525
}
2626

2727
return $content;
28-
}
28+
}
2929
}

src/Fields/Converters/DateTimeConverter.php

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,24 @@
44

55
class DateTimeConverter extends BaseConverter
66
{
7-
public function toDisplayValue(mixed $sourceValue, string|null $locale, string|null $fallbackLocale)
8-
{
9-
if (is_null($sourceValue)) {
10-
return null;
11-
}
12-
13-
if (is_string($sourceValue) && filled($sourceValue)) {
14-
15-
return \Carbon\Carbon::parse($sourceValue);
16-
17-
} elseif ($sourceValue instanceof \DateTimeInterface) {
18-
19-
return $sourceValue;
20-
21-
} else {
22-
23-
return null;
24-
25-
}
26-
}
7+
public function toDisplayValue(mixed $sourceValue, ?string $locale, ?string $fallbackLocale)
8+
{
9+
if (is_null($sourceValue)) {
10+
return null;
11+
}
12+
13+
if (is_string($sourceValue) && filled($sourceValue)) {
14+
15+
return \Carbon\Carbon::parse($sourceValue);
16+
17+
} elseif ($sourceValue instanceof \DateTimeInterface) {
18+
19+
return $sourceValue;
20+
21+
} else {
22+
23+
return null;
24+
25+
}
26+
}
2727
}

src/Fields/Converters/FileConverter.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
class FileConverter extends BaseConverter
88
{
9-
public function toDisplayValue(mixed $sourceValue, string|null $locale, string|null $fallbackLocale)
10-
{
9+
public function toDisplayValue(mixed $sourceValue, ?string $locale, ?string $fallbackLocale)
10+
{
1111
$disk = $this->fieldTypeConfig->disk ?? config('filesystems.default');
1212
$directory = $this->fieldTypeConfig->directory;
1313

@@ -23,5 +23,5 @@ public function toDisplayValue(mixed $sourceValue, string|null $locale, string|n
2323
]))
2424
->values()
2525
->all();
26-
}
26+
}
2727
}

src/Fields/Converters/MarkdownConverter.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function toDisplayValue(mixed $sourceValue, ?string $locale, ?string $fal
1414
return $value;
1515
}
1616

17-
if (!$this->isFieldTypeTranslatable() && is_array($value)) {
17+
if (! $this->isFieldTypeTranslatable() && is_array($value)) {
1818
$value = Arr::first($value);
1919
}
2020

@@ -29,9 +29,10 @@ public function toDisplayValue(mixed $sourceValue, ?string $locale, ?string $fal
2929

3030
private function convertMarkdown($value)
3131
{
32-
if (!is_string($value)) {
32+
if (! is_string($value)) {
3333
return $value;
3434
}
35+
3536
return str($value)->markdown()->toHtmlString();
3637
}
3738
}

src/Fields/Converters/MediaPickerConverter.php

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,33 @@
88

99
class MediaPickerConverter extends BaseConverter
1010
{
11-
public function toDisplayValue(mixed $sourceValue, string|null $locale, string|null $fallbackLocale)
12-
{
13-
//todo: improve performance
11+
public function toDisplayValue(mixed $sourceValue, ?string $locale, ?string $fallbackLocale)
12+
{
13+
// todo: improve performance
1414
$result = [];
1515

1616
$keysToFind = collect($sourceValue)->where(fn ($v) => is_string($v));
17-
$mediaAssets = $keysToFind->isNotEmpty()
17+
$mediaAssets = $keysToFind->isNotEmpty()
1818
? inspirecms_asset()->findByKeys($keysToFind->all())->mapWithKeys(fn (Model $record) => [$record->getKey() => $record])
1919
: collect();
2020

2121
foreach ($sourceValue as $index => $item) {
22-
22+
2323
if ($item instanceof MediaAsset) {
2424
$result[] = $item->toDto($locale);
25-
}
26-
elseif ($item instanceof BaseDto) {
25+
} elseif ($item instanceof BaseDto) {
2726
$result[] = $item;
28-
}
29-
elseif (is_string($item)) {
27+
} elseif (is_string($item)) {
3028
if (($mediaAsset = $mediaAssets->get($item)) instanceof MediaAsset) {
3129
$result[] = $mediaAsset->toDto($locale);
3230
} else {
3331
$result[] = null;
3432
}
35-
}
36-
else {
33+
} else {
3734
$result[] = null;
3835
}
3936
}
4037

4138
return $result;
42-
}
39+
}
4340
}

src/Fields/Converters/RepeaterConverter.php

Lines changed: 108 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -12,112 +12,112 @@
1212

1313
class RepeaterConverter extends BaseConverter
1414
{
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+
}
123123
}

0 commit comments

Comments
 (0)