<?php
namespace ZBateson\MailMimeParser;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use Throwable;
abstract class ErrorBag implements IErrorBag
{
protected LoggerInterface $logger;
private array $errors = [];
private bool $validated = false;
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}
public function getErrorLoggingContextName() : string
{
return static::class;
}
abstract protected function getErrorBagChildren() : array;
protected function validate() : void
{
}
public function addError(string $message, string $psrLogLevel, ?Throwable $exception = null) : static
{
$error = new Error($message, $psrLogLevel, $this, $exception);
$this->errors[] = $error;
$this->logger->log(
$psrLogLevel,
'{contextName} {message} {exception}',
[
'contextName' => $this->getErrorLoggingContextName(),
'message' => $message,
'exception' => $exception ?? ''
]
);
return $this;
}
public function getErrors(bool $validate = false, string $minPsrLevel = LogLevel::ERROR) : array
{
if ($validate && !$this->validated) {
$this->validated = true;
$this->validate();
}
return \array_values(\array_filter(
$this->errors,
function($e) use ($minPsrLevel) {
return $e->isPsrLevelGreaterOrEqualTo($minPsrLevel);
}
));
}
public function hasErrors(bool $validate = false, string $minPsrLevel = LogLevel::ERROR) : bool
{
return (\count($this->getErrors($validate, $minPsrLevel)) > 0);
}
public function getAllErrors(bool $validate = false, string $minPsrLevel = LogLevel::ERROR) : array
{
$arr = \array_values(\array_map(
function($e) use ($validate, $minPsrLevel) {
return $e->getAllErrors($validate, $minPsrLevel);
},
$this->getErrorBagChildren()
));
return \array_merge($this->getErrors($validate, $minPsrLevel), ...$arr);
}
public function hasAnyErrors(bool $validate = false, string $minPsrLevel = LogLevel::ERROR) : bool
{
if ($this->hasErrors($validate, $minPsrLevel)) {
return true;
}
foreach ($this->getErrorBagChildren() as $ch) {
if ($ch->hasAnyErrors($validate, $minPsrLevel)) {
return true;
}
}
return false;
}
}