* @author Ivan Klimchuk, * * @method static self createInfo(string $message, $data = null, array $children = []): self * @method static self createSuccess(string $message, $data = null, array $children = []): self * @method static self createWarning(string $message, $data = null, array $children = []): self * @method static self createError(string $message, $data = null, array $children = []): self * * @method $this getInfos(bool $asFlat = false): array * @method $this getSuccesses(bool $asFlat = false): array * @method $this getWarnings(bool $asFlat = false): array * @method $this getErrors(bool $asFlat = false): array * * @method $this containsInfo() Whenever or not the report contains info messages * @method $this containsSuccess() Whenever or not the report contains successes * @method $this containsWarning() Whenever or not the report contains warnings * @method $this containsError() Whenever or not the report contains errors */ class Report extends common_report_Report { /** @var string */ private $interpolationMessage; /** @var array */ private $interpolationData; /** * Create Report with translations support * * @throws common_exception_Error */ public static function create(string $type, string $interpolationMessage, array $interpolationData = []): Report { return (new self($type, sprintf($interpolationMessage, ...$interpolationData))) ->setInterpolationMessage($interpolationMessage, $interpolationData); } public function setInterpolationMessage(string $interpolationMessage, array $interpolationData = []): self { $this->interpolationMessage = $interpolationMessage; $this->interpolationData = $interpolationData; return $this; } public function jsonSerialize(): array { $data = parent::jsonSerialize(); if ($this->interpolationMessage) { $data['interpolationMessage'] = $this->interpolationMessage; } if ($this->interpolationData) { $data['interpolationData'] = $this->interpolationData; } return $data; } public static function jsonUnserialize($data): ?common_report_Report { /** @var Report $report */ $report = parent::jsonUnserialize($data); if (isset($data['interpolationMessage'])) { $report->setInterpolationMessage( (string) $data['interpolationMessage'], (array) ($data['interpolationData'] ?? []) ); } return $report; } public function translateMessage(): string { if ($this->interpolationMessage && count($this->interpolationData) > 0) { return __($this->interpolationMessage, ...$this->interpolationData); } return __($this->interpolationMessage ?? $this->getMessage()); } }