-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #62 from aerni/feature/synthesizers
Add more synthesizers
- Loading branch information
Showing
4 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |