Set a hash of element names/messages to use when validation fails

@param  array|Traversable $messages
@return Element|ElementInterface|FieldsetInterface
@throws Exception\InvalidArgumentException
487729
public function setMessages($messages)
    {
        if (!is_array($messages) && !$messages instanceof Traversable) {
            throw new Exception\InvalidArgumentException(sprintf(
                '%s expects an array or %s object of messages; received "%s"',
                __METHOD__,
                Traversable::class,
                (is_object($messages) ? get_class($messages) : gettype($messages))
            ));
        }

        foreach ($messages as $key => $messageSet) {
            if (!$this->has($key)) {
                $this->messages[$key] = $messageSet;
                continue;
            }

            $element = $this->get($key);
            $element->setMessages($messageSet);
        }

        return $this;
    }
336693
private function formatMessage(array $message)
    {
        if (isset($message['data']) && $this->encode) {
            $message['data'] = base64_encode($message['data']);
        }

        if (!array_key_exists('data', $message) &&
            !array_key_exists('attributes', $message)) {
            throw new InvalidArgumentException('At least one of $data or
                $attributes must be specified on each message, but neither
                was given.');
        }

        return $message;
    }
924162
protected function validate(array $input, array &$messages)
    {
        $field1Name = $this->field1->getName();
        $field2Name = $this->field2->getName();

        if (!isset($input[$field1Name]) && !isset($input[$field2Name])) {
            // If here, fields are optional and have not been supplied
            // so no validation is necessary
            return;
        }

        if (!$this->doValuesSatisfyComparison($input[$field1Name], $input[$field2Name])) {
            $messages[] = new Message(
                    $this->getMessageId(),
                    ['field1' => $this->field1->getLabel(), 'field2' => $this->field2->getLabel()]
            );
        }
    }
751631
public function set(array $fields = null): Update
    {
        if (!\is_array($fields)) {
            throw new InvalidArgumentException('$fields has to be an array or a string');
        }

        $this->namesClass->addAliases(\array_keys($fields));
        foreach ($fields as $name => $value) {
            $this->fields[] = \sprintf(
                '%s=%s',
                $name,
                $this->escapeClass->escape($value, $this->namesClass->getAliases())
            );
        }

        return $this;
    }
422565
protected function mapMessagesToElementCollection(array $messages, ElementCollection $children)
    {
        $children->forAll(function (ElementInterface $element) use ($messages) {
            $this->mapMessagesToElement($messages, $element);
        });
    }
847412
private function belowErrorCountThreshold(array $messages)
    {
        if (is_int($this->configuration[self::ERROR_COUNT_THRESHOLD_KEY]) === false) {
            throw new Exception(sprintf('Invalid «%s» config key.', self::ERROR_COUNT_THRESHOLD_KEY));
        }

        $threshold = $this->configuration[self::ERROR_COUNT_THRESHOLD_KEY];
        $belowThreshold = count($messages) <= $threshold;

        return $belowThreshold;
    }
687123
protected function validate($input, array &$messages)
    {
        /** @var IUploadedFile $input */

        if (!$input->hasUploadedSuccessfully()) {
            if (!isset(self::$errorMap[$input->getUploadError()])) {
                throw InvalidArgumentException::format('Unknown file upload error: \'%s\'', $input->getUploadError());
            }

            $messages[] = new Message(self::$errorMap[$input->getUploadError()]);
        }
    }
225136
public function setTransactionsAttribute($transactions)
    {
        if (!is_array($transactions)) {
            throw new \InvalidArgumentException('Transactions must be an array');
        }

        foreach ($transactions as $key => $transaction) {
            if (!($transaction instanceof Transaction)) {
                $transactions[$key] = new Transaction($transaction);
            }
        }

        $this->attributes['Transactions'] = $transactions;

        return $this;
    }
1253485
public function setText($message) {
        if (empty($message)) {
            throw new \InvalidArgumentException('Message cannot be empty');
        }

        $this->message->setText($message);
        return $this;
    }
748575
private static function fromArray(array $messages, Translations $translations)
    {
        if (!empty($messages['domain'])) {
            $translations->setDomain($messages['domain']);
        }

        if (!empty($messages['plural-forms'])) {
            $translations->setHeader(Translations::HEADER_PLURAL, $messages['plural-forms']);
        }

        foreach ($messages['messages'] as $context => $contextTranslations) {
            foreach ($contextTranslations as $original => $value) {
                if ($context === '' && $original === '') {
                    self::extractHeaders(is_array($value) ? array_shift($value) : $value, $translations);
                    continue;
                }

                $translation = $translations->insert($context, $original);

                if (is_array($value)) {
                    $translation->setTranslation(array_shift($value));
                    $translation->setPluralTranslations($value);
                } else {
                    $translation->setTranslation($value);
                }
            }
        }
    }
135055
public function setMessage(
        $message,
        $messageType = ValidationResult::TYPE_ERROR,
        $messageCast = ValidationResult::CAST_TEXT
    ) {
        if (!in_array($messageCast, [ValidationResult::CAST_TEXT, ValidationResult::CAST_HTML])) {
            throw new InvalidArgumentException("Invalid message cast type");
        }
        $this->message = $message;
        $this->messageType = $messageType;
        $this->messageCast = $messageCast;
        return $this;
    }
788884
public function assertNotMultipleWarningMessage(TableNode $messages)
    {
        $this->assertValidMessageTable($messages, 'warning messages');
        foreach ($messages->getHash() as $key => $value) {
            $value = array_change_key_case($value);
            $message = trim($value['warning messages']);
            $this->assertNotWarningMessage($message);
        }
    }
124578
public function message($message)
    {
        if (is_string($message) || is_numeric($message)) {
            $message = new Text($message);
        }

        if (!($message instanceof Message)) {
            throw new InvalidArgumentException('Invalid message.');
        }

        $this->message = $message;

        return $this;
    }
873242
private function removeMessage($messages)
    {
        if (! is_null($this->messageKey)) {
            unset($messages[$this->messageKey]);
            $this->messageKey = null;
        }

        return $messages;
    }
1047853
public function setMessage(string $message)
    {
        if (strlen($message) > 0) {
            $properties = $this->getProperties();
            $properties['message'] = $message;
            $this->setProperties($properties);
            return $this;
        }
        return false;
    }
998016
public function sendBatch(array $messages)
    {
        $body = '';
        /** @var Message\Message $message */
        foreach ($messages as $i => $message) {
            if (!$message instanceof Message\Message) {
                throw new InvalidArgumentException();
            }

            $body .= "[{$i}]\n";
            $body .= $message->toINI();
        }
        $body = trim($body);

        $request = $this->client->newRequest(
            'POST',
            $this->client->buildUriWithQuery('/SmSendPost.asp', ['encoding' => 'UTF8']),
            'text/plain',
            $body
        );

        $response = $this->client->sendRequest($request);

        return $this->parseMessageResponse($response);
    }
29381
public function send(Message $message)
    {
        if (empty($message->getTo()) || empty($message->getSubject()) || empty($message->getMessage())) {
            throw new InvalidArgumentException(
                "An error has occurred. The sender or the message or object omits.",
                E_USER_ERROR
            );
        }

        if (isset($this->config['mail'])) {
            $section = $this->config['mail']['default'];

            if (!$message->fromIsDefined()) {
                $form = $this->config['mail'][$section];

                $message->from($form["address"], $form["username"]);
            } elseif (!Str::isMail($message->getFrom())) {
                $form = $this->config['mail'][$message->getFrom()];

                $message->from($form["address"], $form["username"]);
            }
        }

        $to = '';

        $message->setDefaultHeader();

        foreach ($message->getTo() as $value) {
            if ($value[0] !== null) {
                $to .= $value[0] . ' <' . $value[1] . '>';
            } else {
                $to .= '<' . $value[1] . '>';
            }
        }

        $headers = $message->compileHeaders();

        $headers .= 'Content-Type: ' . $message->getType() . '; charset=' . $message->getCharset() . Message::END;

        $headers .= 'Content-Transfer-Encoding: 8bit' . Message::END;

        // Send email use the php native function
        $status = @mail($to, $message->getSubject(), $message->getMessage(), $headers);

        return (bool) $status;
    }