Wire::messages PHP Method

messages() public method

Return messages recorded by this object
public messages ( string | array $options = [] ) : Notices | string
$options string | array One or more of array elements or space separated string of: first: only first item will be returned (string) last: only last item will be returned (string) all: include all items of type (messages or errors) beyond the scope of this object clear: clear out all items that are returned from this method errors: returns errors rather than messages. warnings: returns warnings rather than messages. array: return an array of strings rather than series of Notice objects. string: return a newline separated string rather than array/Notice objects.
return Notices | string Array of NoticeError error messages or string if last, first or str option was specified.
    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;
    }