-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 464bbaa
Showing
9 changed files
with
419 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
## ide | ||
.idea | ||
|
||
## composer | ||
vendor | ||
composer.lock |
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,13 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPSTORM_META; | ||
|
||
use WebChemistry\FormSerializer\Event\AfterDenormalizeEvent; | ||
use WebChemistry\FormSerializer\Event\SuccessEvent; | ||
|
||
override(SuccessEvent::getObject(0), map([ | ||
'' => '@', | ||
])); | ||
override(AfterDenormalizeEvent::getObject(0), map([ | ||
'' => '@', | ||
])); |
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,11 @@ | ||
{ | ||
"name": "webchemistry/form-serializer", | ||
"autoload": { | ||
"psr-4": { | ||
"WebChemistry\\FormSerializer\\": "src" | ||
} | ||
}, | ||
"require": { | ||
"php": ">= 8.0" | ||
} | ||
} |
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,34 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace WebChemistry\FormSerializer\Event; | ||
|
||
use LogicException; | ||
use Nette\Application\UI\Form; | ||
|
||
class AfterDenormalizeEvent extends Event | ||
{ | ||
|
||
public function __construct( | ||
private object $object, | ||
private Form $form, | ||
) | ||
{ | ||
} | ||
|
||
public function getForm(): Form | ||
{ | ||
return $this->form; | ||
} | ||
|
||
public function getObject(?string $typeOf = null): object | ||
{ | ||
if ($typeOf && !$this->object instanceof $typeOf) { | ||
throw new LogicException( | ||
sprintf('Object must be instance of %s, %s given', $typeOf, get_debug_type($this->object)) | ||
); | ||
} | ||
|
||
return $this->object; | ||
} | ||
|
||
} |
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 declare(strict_types = 1); | ||
|
||
namespace WebChemistry\FormSerializer\Event; | ||
|
||
use Nette\Application\UI\Form; | ||
|
||
final class BeforeDenormalizeEvent extends Event | ||
{ | ||
|
||
/** | ||
* @param mixed[] $values | ||
*/ | ||
public function __construct( | ||
private array $values, | ||
private Form $form, | ||
) | ||
{ | ||
} | ||
|
||
public function getForm(): Form | ||
{ | ||
return $this->form; | ||
} | ||
|
||
/** | ||
* @return mixed[] | ||
*/ | ||
public function getValues(): array | ||
{ | ||
return $this->values; | ||
} | ||
|
||
/** | ||
* @param mixed[] $values | ||
*/ | ||
public function setValues(array $values): void | ||
{ | ||
$this->values = $values; | ||
} | ||
|
||
public function setValue(string|int $index, mixed $value): void | ||
{ | ||
$this->values[$index] = $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,22 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace WebChemistry\FormSerializer\Event; | ||
|
||
use Psr\EventDispatcher\StoppableEventInterface; | ||
|
||
abstract class Event implements StoppableEventInterface | ||
{ | ||
|
||
private bool $propagationStopped = false; | ||
|
||
public function stopPropagation(): void | ||
{ | ||
$this->propagationStopped = true; | ||
} | ||
|
||
public function isPropagationStopped(): bool | ||
{ | ||
return $this->propagationStopped; | ||
} | ||
|
||
} |
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,43 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace WebChemistry\FormSerializer\Event; | ||
|
||
final class EventDispatcher | ||
{ | ||
|
||
/** @var mixed[] */ | ||
private array $listeners = []; | ||
|
||
public function addBeforeDenormalize(callable $callback): self | ||
{ | ||
$this->listeners[BeforeDenormalizeEvent::class][] = $callback; | ||
|
||
return $this; | ||
} | ||
|
||
public function addAfterDenormalize(callable $callback): self | ||
{ | ||
$this->listeners[AfterDenormalizeEvent::class][] = $callback; | ||
|
||
return $this; | ||
} | ||
|
||
public function addSuccess(callable $callback): self | ||
{ | ||
$this->listeners[SuccessEvent::class][] = $callback; | ||
|
||
return $this; | ||
} | ||
|
||
public function dispatch(Event $event): void | ||
{ | ||
foreach ($this->listeners[get_class($event)] ?? [] as $listener) { | ||
$listener($event); | ||
|
||
if ($event->isPropagationStopped()) { | ||
break; | ||
} | ||
} | ||
} | ||
|
||
} |
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,34 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace WebChemistry\FormSerializer\Event; | ||
|
||
use LogicException; | ||
use Nette\Application\UI\Form; | ||
|
||
final class SuccessEvent extends Event | ||
{ | ||
|
||
public function __construct( | ||
private object $object, | ||
private Form $form, | ||
) | ||
{ | ||
} | ||
|
||
public function getForm(): Form | ||
{ | ||
return $this->form; | ||
} | ||
|
||
public function getObject(?string $typeOf = null): object | ||
{ | ||
if ($typeOf && !$this->object instanceof $typeOf) { | ||
throw new LogicException( | ||
sprintf('Object must be instance of %s, %s given', $typeOf, get_debug_type($this->object)) | ||
); | ||
} | ||
|
||
return $this->object; | ||
} | ||
|
||
} |
Oops, something went wrong.