From 514369e6553c87e4f58dd2e874b52b8f3d2c81cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Proch=C3=A1zka?= Date: Tue, 24 Oct 2023 14:08:31 +0200 Subject: [PATCH] Added targets and improved params handling --- composer.json | 2 +- src/Entity/Record.php | 47 +++++++++++++++++++++++++++++++++++++------ src/RecordBuilder.php | 7 +++---- 3 files changed, 45 insertions(+), 11 deletions(-) diff --git a/composer.json b/composer.json index 79d650c..ae0d2f5 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ "php": ">=8.1", "doctrine/orm": "^2.0|^3.0", "juniwalk/orm": "^0.1", - "juniwalk/utils": ">=2.0" + "juniwalk/utils": ">=2.4.8" }, "suggest": { diff --git a/src/Entity/Record.php b/src/Entity/Record.php index c396ae7..6e15dcc 100644 --- a/src/Entity/Record.php +++ b/src/Entity/Record.php @@ -12,7 +12,8 @@ use JuniWalk\Nestor\Enums\Type; use JuniWalk\Utils\Arrays; use JuniWalk\Utils\Enums\Color; -use Nette\Utils\Json; +use JuniWalk\Utils\Format; +use JuniWalk\Utils\Json; #[ORM\MappedSuperclass] abstract class Record @@ -26,6 +27,12 @@ abstract class Record #[ORM\Column(type: 'string')] protected ?string $message; + #[ORM\Column(type: 'string', length: 32, nullable: true)] + protected string $target; + + #[ORM\Column(type: 'integer', nullable: true)] + protected ?int $targetId; + #[ORM\Column(type: 'datetimetz')] protected DateTime $date; @@ -55,7 +62,9 @@ final public function __construct(string $event, string $message) public function __toString(): string { - return strtr("[%type%, %level%] %event%: %message% (%params%)", [ + return strtr("[%type%, %level%] %target%(%targetId%) %event%: %message% (%params%)", [ + '%target%' => $this->getTarget(), + '%targetId%' => $this->getTargetId(), '%type%' => $this->getType()->value, '%level%' => $this->getLevel()->value, '%event%' => $this->getEvent(), @@ -109,6 +118,25 @@ public function getMessageFormatted(): string } + public function setTarget(object $target, ?int $targetId = null): void + { + $this->targetId = $target->getId() ?? $targetId; + $this->target = Format::className($target); + } + + + public function getTarget(): string + { + return $this->target; + } + + + public function getTargetId(): ?int + { + return $this->targetId; + } + + public function setDate(DateTime $date): void { $this->date = clone $date; @@ -153,9 +181,16 @@ public function isFinishable(): bool public function setParams(array $params): void { - $params = array_filter($params, function($v): bool { - return !is_null($v); - }); + $this->params = null; + $this->addParams($params); + } + + + public function addParams(array $params): void + { + $params = Arrays::map($params, fn($v) => Format::scalarize($v)); + $params = array_filter($params, fn($v) => !is_null($v)); + $params = array_merge($params, $this->params ?? []); $this->params = $params ?: null; } @@ -167,7 +202,7 @@ public function getParams(): array } - public function getParam(string $key)//: mixed + public function getParam(string $key): mixed { return $this->params[$key] ?? null; } diff --git a/src/RecordBuilder.php b/src/RecordBuilder.php index c71769e..8fb6345 100644 --- a/src/RecordBuilder.php +++ b/src/RecordBuilder.php @@ -16,8 +16,7 @@ final class RecordBuilder { - private iterable $record = []; - + private array $record = []; public function __construct( private readonly Chronicler $chronicler @@ -95,7 +94,7 @@ public function withDate(DateTime $date): static } - public function withParams(iterable $params): static + public function withParams(array $params): static { foreach ($params as $key => $value) { if (!$matches = Strings::match($key, '/record\.(\w+)/i')) { @@ -115,7 +114,7 @@ public function withParams(iterable $params): static } - public function withParam(string $name, $value): static + public function withParam(string $name, mixed $value): static { $this->record['params'][$name] = $value; return $this;