. */ namespace SP\Core\Messages; /** * Class LogMessage * * @package SP\Core\Messages */ final class LogMessage extends MessageBase { /** * @var string Acción realizada */ protected $action; /** * @var array Detalles de la acción en formato "detalle : descripción" */ protected $details = []; /** * @var int */ protected $descriptionCounter = 0; /** * @var int */ protected $detailsCounter = 0; /** * Establece los detalles de la acción realizada * * @param $key string * @param $value string * * @return $this */ public function addDetails(string $key, string $value) { if ($value === '' || $key === '') { return $this; } $this->details[] = [$this->formatString($key), $this->formatString($value)]; $this->detailsCounter++; return $this; } /** * Formatear una cadena para guardarla en el registro * * @param $string string La cadena a formatear * * @return string */ private function formatString(string $string): string { return strip_tags($string); } /** * Establece la descripción de la acción realizada * * @param string $description * * @return $this */ public function addDescription(string $description = ''): LogMessage { $this->description[] = $this->formatString($description); return $this; } /** * Añadir una línea en blanco a la descripción */ public function addDescriptionLine() { $this->description[] = ''; $this->descriptionCounter++; return $this; } /** * Componer un mensaje en formato texto * * @param string $delimiter * * @return string */ public function composeText(string $delimiter = PHP_EOL): string { $formatter = new TextFormatter(); $message[] = $this->getAction(true); $message[] = $this->getDescription($formatter, true); $message[] = $this->getDetails($formatter, true); return implode(PHP_EOL, $message); } /** * Devuelve la acción realizada * * @param bool $translate * * @return string */ public function getAction(bool $translate = false): string { return $translate ? __($this->action) : $this->action; } /** * Establece la acción realizada * * @param string $action * * @return $this */ public function setAction(string $action): LogMessage { $this->action = $this->formatString($action); return $this; } /** * Devuelve la descripción de la acción realizada * * @param FormatterInterface $formatter * @param bool $translate * * @return string */ public function getDescription(FormatterInterface $formatter, $translate = false): string { if (count($this->description) === 0) { return ''; } return $formatter->formatDescription($this->description, $translate); } /** * Devuelve los detalles de la acción realizada * * @param FormatterInterface $formatter * @param bool $translate * * @return string */ public function getDetails(FormatterInterface $formatter, $translate = false): string { if (count($this->details) === 0) { return ''; } return $formatter->formatDetail($this->details, $translate); } /** * Componer un mensaje en formato HTML * * @return string */ public function composeHtml(): string { $formatter = new HtmlFormatter(); $message = '
'; return $message; } /** * Restablecer la variable de descripcion */ public function resetDescription() { $this->description = []; $this->descriptionCounter = 0; return $this; } /** * Restablecer la variable de detalles */ public function resetDetails(): LogMessage { $this->details = []; $this->detailsCounter = 0; return $this; } /** * @return int */ public function getDescriptionCounter() { return $this->descriptionCounter; } /** * @return int */ public function getDetailsCounter() { return $this->detailsCounter; } }