Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
MartkCz committed Dec 30, 2020
0 parents commit 464bbaa
Show file tree
Hide file tree
Showing 9 changed files with 419 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
## ide
.idea

## composer
vendor
composer.lock
13 changes: 13 additions & 0 deletions .phpstorm.metadata.php
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([
'' => '@',
]));
11 changes: 11 additions & 0 deletions composer.json
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"
}
}
34 changes: 34 additions & 0 deletions src/Event/AfterDenormalizeEvent.php
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;
}

}
46 changes: 46 additions & 0 deletions src/Event/BeforeDenormalizeEvent.php
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;
}

}
22 changes: 22 additions & 0 deletions src/Event/Event.php
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;
}

}
43 changes: 43 additions & 0 deletions src/Event/EventDispatcher.php
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;
}
}
}

}
34 changes: 34 additions & 0 deletions src/Event/SuccessEvent.php
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;
}

}
Loading

0 comments on commit 464bbaa

Please sign in to comment.