Skip to content

Commit

Permalink
Merge pull request #62 from aerni/feature/synthesizers
Browse files Browse the repository at this point in the history
Add more synthesizers
  • Loading branch information
jonassiewertsen authored Jun 10, 2024
2 parents 36ad6fb + e37e63c commit 443651e
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 0 deletions.
3 changes: 3 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
'classes' => [
\Jonassiewertsen\Livewire\Synthesizers\EntryCollectionSynthesizer::class,
\Jonassiewertsen\Livewire\Synthesizers\EntrySynthesizer::class,
\Jonassiewertsen\Livewire\Synthesizers\FieldSynthesizer::class,
\Jonassiewertsen\Livewire\Synthesizers\FieldtypeSynthesizer::class,
\Jonassiewertsen\Livewire\Synthesizers\ValueSynthesizer::class,
],
],

Expand Down
39 changes: 39 additions & 0 deletions src/Synthesizers/FieldSynthesizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Jonassiewertsen\Livewire\Synthesizers;

use Livewire\Mechanisms\HandleComponents\Synthesizers\Synth;
use Statamic\Fields\Field;

class FieldSynthesizer extends Synth
{
public static $key = 'statamic-field';

public static function match($target)
{
return $target instanceof Field;
}

public function dehydrate($target, $dehydrateChild)
{
$data = [
'handle' => $target->handle(),
'config' => $target->config(),
];

foreach ($data as $key => $child) {
$data[$key] = $dehydrateChild($key, $child);
}

return [$data, []];
}

public function hydrate($value, $meta, $hydrateChild)
{
foreach ($value as $key => $child) {
$value[$key] = $hydrateChild($key, $child);
}

return new Field(...$value);
}
}
41 changes: 41 additions & 0 deletions src/Synthesizers/FieldtypeSynthesizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Jonassiewertsen\Livewire\Synthesizers;

use Livewire\Mechanisms\HandleComponents\Synthesizers\Synth;
use Statamic\Fields\Fieldtype;

class FieldtypeSynthesizer extends Synth
{
public static $key = 'statamic-fieldtype';

public static function match($target)
{
return $target instanceof Fieldtype;
}

public function dehydrate($target, $dehydrateChild)
{
$data = [
'field' => $target->field(),
];

foreach ($data as $key => $child) {
$data[$key] = $dehydrateChild($key, $child);
}

return [
$data,
['class' => get_class($target)],
];
}

public function hydrate($value, $meta, $hydrateChild)
{
foreach ($value as $key => $child) {
$value[$key] = $hydrateChild($key, $child);
}

return app($meta['class'])->setField($value['field']);
}
}
46 changes: 46 additions & 0 deletions src/Synthesizers/ValueSynthesizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Jonassiewertsen\Livewire\Synthesizers;

use Livewire\Mechanisms\HandleComponents\Synthesizers\Synth;
use Statamic\Fields\Value;

use function Livewire\invade;

class ValueSynthesizer extends Synth
{
public static $key = 'statamic-value';

public static function match($target)
{
return $target instanceof Value;
}

public function dehydrate($target, $dehydrateChild)
{
$value = invade($target);

$data = [
'value' => $value->raw,
'handle' => $value->handle,
'fieldtype' => $value->fieldtype,
'augmentable' => $value->augmentable,
'shallow' => $value->shallow,
];

foreach ($data as $key => $child) {
$data[$key] = $dehydrateChild($key, $child);
}

return [$data, []];
}

public function hydrate($value, $meta, $hydrateChild)
{
foreach ($value as $key => $child) {
$value[$key] = $hydrateChild($key, $child);
}

return new Value(...$value);
}
}

0 comments on commit 443651e

Please sign in to comment.