<?php
 namespace App\Entity; class Messages { protected $class; protected $messages = []; public function __construct(\App\Entity\GeneralAbstract $entityObj) { $this->class = stripClassNamespace(get_class($entityObj)); } private function addMessage(string $type, string $text, ?string $code=null, ?string $field=null) :void { $message = new \App\Message($type, $text, $code); if ($field) { $message->setField($field); } $this->messages[] = $message; if ($message->getType()=='error') { $this->throwMessages(); } } public function addWarningCode(string $messageCode, ?string $field=null, array|string|null $params=null) :void { if ($params!==null) { $formattedText = $this->translateMessageByCode($messageCode, (array)$params); } else { $formattedText = $this->translateMessageByCode($messageCode); } $this->addMessage('warning', $formattedText, $messageCode, $field); } public function addWarningText(string $messageText, ?string $field=null, array|string|null $params=null) :void { if ($params!==null) { $formattedText = vsprintf($messageText, (array)$params); } else { $formattedText = $messageText; } $this->addMessage('warning', $formattedText, null, $field); } public function addErrorCode(string $messageCode, ?string $field=null, array|string|null $params=null) :void { if ($params!==null) { $formattedText = $this->translateMessageByCode($messageCode, (array)$params); } else { $formattedText = $this->translateMessageByCode($messageCode); } $this->addMessage('error', $formattedText, $messageCode, $field); } public function addErrorText(string $messageText, ?string $field=null, array|string|null $params=null) :void { if ($params!==null) { $formattedText = vsprintf($messageText, (array)$params); } else { $formattedText = $messageText; } $this->addMessage('error', $formattedText, null, $field); } public function addSuccessCode(string $messageCode, ?string $field=null, array|string|null $params=null) :void { if ($params!==null) { $formattedText = $this->translateMessageByCode($messageCode, (array)$params); } else { $formattedText = $this->translateMessageByCode($messageCode); } $this->addMessage('success', $formattedText, $messageCode, $field); } public function addSuccessText(string $messageText, ?string $field=null, array|string|null $params=null) :void { if ($params!==null) { $formattedText = vsprintf($messageText, (array)$params); } else { $formattedText = $messageText; } $this->addMessage('success', $formattedText, null, $field); } public function removeSuccess() :void { if ($this->messages) { foreach($this->messages as $id=>$msg) { if ($msg->getType()=='success') { unset($this->messages[$id]); } } } } public function addMessageCollector(\App\MessagesCollector $msgs) :void { foreach($msgs->getMessages() as $msg) { if ($msg->getType()=='warning' or $msg->getType()=='error') { $this->addMessage($msg->getType(), $msg->getText(), $msg->getCode(), $msg->getField()); } } } private function translateMessageByCode(string $messageCode, array $params=[]) :string { $langLoader = \App\Loader_Lang::getInstance(); $langLoader->loadAppFile("_customLoad/entities/{$this->class}.lang.php"); $langLoader->loadAppFile("_customLoad/entitiesMt/{$this->class}.lang.php"); $_LANG = $langLoader->getAll(); if ($_LANG['globalEntityMessages']["::{$this->class}"][$messageCode]) { $orgText = $_LANG['globalEntityMessages']["::{$this->class}"][$messageCode]; } elseif ($_LANG['globalEntityMessages'][$messageCode]) { $orgText = $_LANG['globalEntityMessages'][$messageCode]; } else { $orgText = sprintf($_LANG['globalEntityMessages']['untranslatedMessage'], $messageCode); } $formattedText = $orgText; if ($params) { $formattedText = vsprintf($orgText, $params); } return $formattedText; } public function throwMessages() :void { if ($this->messages) { \App\Entity\Cache::getInstance()->flushInternal(); $msgs = new \App\MessagesCollector(); foreach($this->messages as $msg) { $msgs->addMessage($msg); } throw new \App\Entity\Exception($msgs); } } public function getMessages() :array { return $this->messages; } public function hasErrorOrWarning() :bool { return (bool)array_filter($this->messages, function(\App\Message $msg) { return in_array($msg->getType(), ['warning', 'error']); }); } } 