Source of file Messages.php
Size: 4,399 Bytes - Last Modified: 2017-11-08T13:54:24+00:00
/home/travis/build/bluzphp/framework/src/Messages/Messages.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 | <?php /** * Bluz Framework Component * * @copyright Bluz PHP Team * @link https://github.com/bluzphp/framework */ declare(strict_types=1); namespace Bluz\Messages; use Bluz\Common\Options; use Bluz\Proxy\Session; use Bluz\Proxy\Translator; /** * Realization of Flash Messages * * @package Bluz\Messages * @author Anton Shevchuk * @link https://github.com/bluzphp/framework/wiki/Messages */ class Messages { use Options; const TYPE_ERROR = 'error'; const TYPE_SUCCESS = 'success'; const TYPE_NOTICE = 'notice'; /** * @var array list of messages types */ protected $types = [ self::TYPE_ERROR, self::TYPE_SUCCESS, self::TYPE_NOTICE ]; /** * Initialize Messages container * * @return Messages */ protected function init() { if (!$this->getMessagesStore()) { $this->reset(); } return $this; } /** * Add notice * * @param string $message * @param string[] $text * * @return void * @since 1.0.0 added $text */ public function addNotice($message, ...$text) { $this->add(self::TYPE_NOTICE, $message, ...$text); } /** * Add success * * @param string $message * @param string[] $text * * @return void * @since 1.0.0 added $text */ public function addSuccess($message, ...$text) { $this->add(self::TYPE_SUCCESS, $message, ...$text); } /** * Add error * * @param string $message * @param string[] $text * * @return void * @since 1.0.0 added $text */ public function addError($message, ...$text) { $this->add(self::TYPE_ERROR, $message, ...$text); } /** * Add message to container * * @param string $type One of error, notice or success * @param string $message * @param string[] $text * * @return void */ protected function add($type, $message, ...$text) { $this->init(); $this->getMessagesStore()[$type][] = Translator::translate($message, ...$text); } /** * Pop a message * * @param string $type * * @return \stdClass|null */ public function pop($type = null) { if (!$this->getMessagesStore()) { return null; } if ($type !== null) { $text = array_shift($this->getMessagesStore()[$type]); if ($text) { $message = new \stdClass(); $message->text = $text; $message->type = $type; return $message; } } else { foreach ($this->types as $aType) { if ($message = $this->pop($aType)) { return $message; } } } return null; } /** * Pop all messages * * @return \ArrayObject|array */ public function popAll() { if (!$this->getMessagesStore()) { return $this->createEmptyMessagesStore(); } $messages = $this->getMessagesStore()->getArrayCopy(); $this->reset(); return $messages; } /** * Get size of messages container * * @return integer */ public function count() { $size = 0; if (!$store = $this->getMessagesStore()) { return $size; } foreach ($store as $messages) { $size += count($messages); } return $size; } /** * Reset messages * * @return void */ public function reset() { Session::set('messages:store', $this->createEmptyMessagesStore()); } /** * Returns current messages store * * @return \ArrayObject|null Returns null if store not exists yet */ protected function getMessagesStore() { return Session::get('messages:store'); } /** * Creates a new empty store for messages * * @return \ArrayObject */ protected function createEmptyMessagesStore() { return new \ArrayObject( [ self::TYPE_ERROR => [], self::TYPE_SUCCESS => [], self::TYPE_NOTICE => [] ] ); } } |