for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace SolBianca\Validator;
use SolBianca\Validator\Interfaces\MessageBagInterface;
class MessageBag implements MessageBagInterface
{
/**
* The registered messages.
*
* @var array
*/
protected $messages = [];
* Creates a new MessageBag instance.
* @param array $messages
public function __construct(array $messages)
foreach ($messages as $key => $value) {
$this->messages[$key] = (array)$value;
}
* {@inheritdoc}
public function has(string $key): bool
return !is_null($this->first($key));
public function first(string $key = null): ?string
$messages = is_null($key) ? $this->flat() : $this->get($key);
return (is_array($messages) && count($messages) > 0) ? $messages[0] : null;
public function get(string $key): ?array
if (array_key_exists($key, $this->messages)) {
return !empty($this->messages[$key]) ? $this->messages[$key] : null;
return null;
public function all(): array
return $this->messages;
public function keys(): array
return array_keys($this->messages);
public function isEmpty(): bool
return empty($this->messages);
public function flat(): array
return iterator_to_array(new \RecursiveIteratorIterator(
new \RecursiveArrayIterator($this->messages)
), false);