Skip to content

Commit

Permalink
Added targets and improved params handling
Browse files Browse the repository at this point in the history
  • Loading branch information
juniwalk authored Oct 24, 2023
1 parent d1b6005 commit 514369e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 11 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
47 changes: 41 additions & 6 deletions src/Entity/Record.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;

Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down
7 changes: 3 additions & 4 deletions src/RecordBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@

final class RecordBuilder
{
private iterable $record = [];

private array $record = [];

public function __construct(
private readonly Chronicler $chronicler
Expand Down Expand Up @@ -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')) {
Expand All @@ -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;
Expand Down

0 comments on commit 514369e

Please sign in to comment.