Skip to content

Commit

Permalink
Rework event system.
Browse files Browse the repository at this point in the history
  • Loading branch information
Syndesi committed Aug 13, 2023
1 parent 78c1d9d commit 4397617
Show file tree
Hide file tree
Showing 23 changed files with 424 additions and 439 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
"post-update-cmd": [
"@auto-scripts"
],
"test:unit": "php vendor/phpunit/phpunit/phpunit",
"test:unit": "php vendor/phpunit/phpunit/phpunit ./tests/UnitTests",
"test:feature": "./test-feature-prepare && php vendor/phpunit/phpunit/phpunit ./tests/FeatureTests",
"test:feature:test": "./test-feature-prepare && php vendor/phpunit/phpunit/phpunit ./tests/FeatureTests --group test",
"test:coverage:xml": "export XDEBUG_MODE=coverage && php ./vendor/phpunit/phpunit/phpunit --coverage-clover coverage.xml",
Expand Down
2 changes: 2 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ imports:
- {resource: ../src/EventSystem/Kernel/config.yaml}
- {resource: ../src/EventSystem/RawToElement/config.yaml}
- {resource: ../src/EventSystem/Request/config.yaml}
- {resource: ../src/EventSystem/NormalizedValueToRawValue/config.yaml}
- {resource: ../src/EventSystem/RawValueToNormalizedValue/config.yaml}

services:
# default configuration for services in *this* file
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace App\EventSystem\NormalizedValueToRawValue\Event;

use App\Contract\EventInterface;
use App\Trait\StoppableEventTrait;

class NormalizedValueToRawValueEvent implements EventInterface
{
use StoppableEventTrait;

private mixed $rawValue;

public function __construct(
private readonly mixed $normalizedValue
) {
}

public function getNormalizedValue(): mixed
{
return $this->normalizedValue;
}

public function getRawValue(): mixed
{
return $this->rawValue;
}

public function setRawValue(mixed $rawValue): self
{
$this->rawValue = $rawValue;

return $this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\EventSystem\NormalizedValueToRawValue\EventListener;

use App\EventSystem\NormalizedValueToRawValue\Event\NormalizedValueToRawValueEvent;

class DateTimeNormalizedValueToRawValueEventListener
{
public function onNormalizedValueToRawValueEvent(NormalizedValueToRawValueEvent $event): void
{
$normalizedValue = $event->getNormalizedValue();
if (!($normalizedValue instanceof \DateTime)) {
return;
}
$event->setRawValue($normalizedValue->format(\DateTime::ATOM));
$event->stopPropagation();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace App\EventSystem\NormalizedValueToRawValue\EventListener;

use App\EventSystem\NormalizedValueToRawValue\Event\NormalizedValueToRawValueEvent;

class GenericNormalizedValueToRawValueEventListener
{
public function onNormalizedValueToRawValueEvent(NormalizedValueToRawValueEvent $event): void
{
$normalizedValue = $event->getNormalizedValue();
if (
is_array($normalizedValue)
// is_object($normalizedValue) ||
|| is_numeric($normalizedValue)
|| is_bool($normalizedValue)
|| is_string($normalizedValue)
|| is_null($normalizedValue)
) {
$event->setRawValue($normalizedValue);
$event->stopPropagation();
}
}
}
19 changes: 19 additions & 0 deletions src/EventSystem/NormalizedValueToRawValue/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
services:

_defaults:
autowire: true
autoconfigure: true

App\EventSystem\NormalizedValueToRawValue\EventListener\DateTimeNormalizedValueToRawValueEventListener:
tags:
- name: kernel.event_listener
event: 'App\EventSystem\NormalizedValueToRawValue\Event\NormalizedValueToRawValueEvent'
method: 'onNormalizedValueToRawValueEvent'
priority: 16

App\EventSystem\NormalizedValueToRawValue\EventListener\GenericNormalizedValueToRawValueEventListener:
tags:
- name: kernel.event_listener
event: 'App\EventSystem\NormalizedValueToRawValue\Event\NormalizedValueToRawValueEvent'
method: 'onNormalizedValueToRawValueEvent'
priority: 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace App\EventSystem\RawValueToNormalizedValue\Event;

use App\Contract\EventInterface;
use App\Trait\StoppableEventTrait;

class RawValueToNormalizedValueEvent implements EventInterface
{
use StoppableEventTrait;

private mixed $normalizedValue;

public function __construct(
private readonly mixed $rawValue
) {
}

public function getRawValue(): mixed
{
return $this->rawValue;
}

public function getNormalizedValue(): mixed
{
return $this->normalizedValue;
}

public function setNormalizedValue(mixed $normalizedValue): self
{
$this->normalizedValue = $normalizedValue;

return $this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\EventSystem\RawValueToNormalizedValue\EventListener;

use App\EventSystem\RawValueToNormalizedValue\Event\RawValueToNormalizedValueEvent;

class DateTimeRawValueToNormalizedValueEventListener
{
public function onRawValueToNormalizedValueEvent(RawValueToNormalizedValueEvent $event): void
{
$rawValue = $event->getRawValue();
if (!is_string($rawValue)) {
return;
}
if (
strlen($rawValue) < 22
|| strlen($rawValue) > 25
) {
return;
}
$possibleDate = \DateTime::createFromFormat(\DateTime::ATOM, $rawValue);
if (false === $possibleDate) {
return;
}
$event->setNormalizedValue($possibleDate);
$event->stopPropagation();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace App\EventSystem\RawValueToNormalizedValue\EventListener;

use App\EventSystem\RawValueToNormalizedValue\Event\RawValueToNormalizedValueEvent;

class GenericRawValueToNormalizedValueEventListener
{
public function onRawValueToNormalizedValueEvent(RawValueToNormalizedValueEvent $event): void
{
$rawValue = $event->getRawValue();
if (
is_array($rawValue)
// is_object($rawValue) ||
|| is_numeric($rawValue)
|| is_bool($rawValue)
|| is_string($rawValue)
|| is_null($rawValue)
) {
$event->setNormalizedValue($rawValue);
$event->stopPropagation();
}
}
}
19 changes: 19 additions & 0 deletions src/EventSystem/RawValueToNormalizedValue/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
services:

_defaults:
autowire: true
autoconfigure: true

App\EventSystem\RawValueToNormalizedValue\EventListener\DateTimeRawValueToNormalizedValueEventListener:
tags:
- name: kernel.event_listener
event: 'App\EventSystem\RawValueToNormalizedValue\Event\RawValueToNormalizedValueEvent'
method: 'onRawValueToNormalizedValueEvent'
priority: 16

App\EventSystem\RawValueToNormalizedValue\EventListener\GenericRawValueToNormalizedValueEventListener:
tags:
- name: kernel.event_listener
event: 'App\EventSystem\RawValueToNormalizedValue\Event\RawValueToNormalizedValueEvent'
method: 'onRawValueToNormalizedValueEvent'
priority: 0
31 changes: 0 additions & 31 deletions tests/UnitTests/Event/ElementToRawEventTest.php

This file was deleted.

63 changes: 0 additions & 63 deletions tests/UnitTests/EventListener/ElementToRawEventListenerTest.php

This file was deleted.

This file was deleted.

Loading

0 comments on commit 4397617

Please sign in to comment.