public function messages($options = array())
{
if (!is_array($options)) {
$options = explode(' ', strtolower($options));
}
if (in_array('errors', $options)) {
$type = 'errors';
} else {
if (in_array('warnings', $options)) {
$type = 'warnings';
} else {
$type = 'messages';
}
}
$clear = in_array('clear', $options);
if (in_array('all', $options)) {
// get all of either messages, warnings or errors (either in or out of this object instance)
$value = new Notices();
foreach ($this->wire('notices') as $notice) {
if ($notice->getName() != $type) {
continue;
}
$value->add($notice);
if ($clear) {
$this->wire('notices')->remove($notice);
}
// clear global
}
if ($clear) {
$this->_notices[$type] = null;
}
// clear local
} else {
// get messages, warnings or errors specific to this object instance
$value = is_null($this->_notices[$type]) ? new Notices() : $this->_notices[$type];
if (in_array('first', $options)) {
$value = $clear ? $value->shift() : $value->first();
} else {
if (in_array('last', $options)) {
$value = $clear ? $value->pop() : $value->last();
} else {
if ($clear) {
$this->_notices[$type] = null;
}
}
}
if ($clear && $value) {
$this->wire('notices')->removeItems($value);
}
// clear from global notices
}
if (in_array('array', $options) || in_array('string', $options)) {
if ($value instanceof Notice) {
$value = array($value->text);
} else {
$_value = array();
foreach ($value as $notice) {
$_value[] = $notice->text;
}
$value = $_value;
}
if (in_array('string', $options)) {
$value = implode("\n", $value);
}
}
return $value;
}