Skip to content
This repository was archived by the owner on Jan 27, 2025. It is now read-only.

Commit 443651e

Browse files
Merge pull request #62 from aerni/feature/synthesizers
Add more synthesizers
2 parents 36ad6fb + e37e63c commit 443651e

File tree

4 files changed

+129
-0
lines changed

4 files changed

+129
-0
lines changed

config/config.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
'classes' => [
2828
\Jonassiewertsen\Livewire\Synthesizers\EntryCollectionSynthesizer::class,
2929
\Jonassiewertsen\Livewire\Synthesizers\EntrySynthesizer::class,
30+
\Jonassiewertsen\Livewire\Synthesizers\FieldSynthesizer::class,
31+
\Jonassiewertsen\Livewire\Synthesizers\FieldtypeSynthesizer::class,
32+
\Jonassiewertsen\Livewire\Synthesizers\ValueSynthesizer::class,
3033
],
3134
],
3235

src/Synthesizers/FieldSynthesizer.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Jonassiewertsen\Livewire\Synthesizers;
4+
5+
use Livewire\Mechanisms\HandleComponents\Synthesizers\Synth;
6+
use Statamic\Fields\Field;
7+
8+
class FieldSynthesizer extends Synth
9+
{
10+
public static $key = 'statamic-field';
11+
12+
public static function match($target)
13+
{
14+
return $target instanceof Field;
15+
}
16+
17+
public function dehydrate($target, $dehydrateChild)
18+
{
19+
$data = [
20+
'handle' => $target->handle(),
21+
'config' => $target->config(),
22+
];
23+
24+
foreach ($data as $key => $child) {
25+
$data[$key] = $dehydrateChild($key, $child);
26+
}
27+
28+
return [$data, []];
29+
}
30+
31+
public function hydrate($value, $meta, $hydrateChild)
32+
{
33+
foreach ($value as $key => $child) {
34+
$value[$key] = $hydrateChild($key, $child);
35+
}
36+
37+
return new Field(...$value);
38+
}
39+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Jonassiewertsen\Livewire\Synthesizers;
4+
5+
use Livewire\Mechanisms\HandleComponents\Synthesizers\Synth;
6+
use Statamic\Fields\Fieldtype;
7+
8+
class FieldtypeSynthesizer extends Synth
9+
{
10+
public static $key = 'statamic-fieldtype';
11+
12+
public static function match($target)
13+
{
14+
return $target instanceof Fieldtype;
15+
}
16+
17+
public function dehydrate($target, $dehydrateChild)
18+
{
19+
$data = [
20+
'field' => $target->field(),
21+
];
22+
23+
foreach ($data as $key => $child) {
24+
$data[$key] = $dehydrateChild($key, $child);
25+
}
26+
27+
return [
28+
$data,
29+
['class' => get_class($target)],
30+
];
31+
}
32+
33+
public function hydrate($value, $meta, $hydrateChild)
34+
{
35+
foreach ($value as $key => $child) {
36+
$value[$key] = $hydrateChild($key, $child);
37+
}
38+
39+
return app($meta['class'])->setField($value['field']);
40+
}
41+
}

src/Synthesizers/ValueSynthesizer.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Jonassiewertsen\Livewire\Synthesizers;
4+
5+
use Livewire\Mechanisms\HandleComponents\Synthesizers\Synth;
6+
use Statamic\Fields\Value;
7+
8+
use function Livewire\invade;
9+
10+
class ValueSynthesizer extends Synth
11+
{
12+
public static $key = 'statamic-value';
13+
14+
public static function match($target)
15+
{
16+
return $target instanceof Value;
17+
}
18+
19+
public function dehydrate($target, $dehydrateChild)
20+
{
21+
$value = invade($target);
22+
23+
$data = [
24+
'value' => $value->raw,
25+
'handle' => $value->handle,
26+
'fieldtype' => $value->fieldtype,
27+
'augmentable' => $value->augmentable,
28+
'shallow' => $value->shallow,
29+
];
30+
31+
foreach ($data as $key => $child) {
32+
$data[$key] = $dehydrateChild($key, $child);
33+
}
34+
35+
return [$data, []];
36+
}
37+
38+
public function hydrate($value, $meta, $hydrateChild)
39+
{
40+
foreach ($value as $key => $child) {
41+
$value[$key] = $hydrateChild($key, $child);
42+
}
43+
44+
return new Value(...$value);
45+
}
46+
}

0 commit comments

Comments
 (0)