public function filterMessages(array $messages, array $supportedMessages, $canCreateNewMessages = false)
    {
        // Adding the keys `value` and `extension` to the messages, only if it is missing.
        $addValueToArray = function (array &$a) {
            foreach ($a as $k => $v) {
                if (false === isset($v['value'])) {
                    $a[$k]['value'] = '';
                }
                if (false === isset($v['extension'])) {
                    $a[$k]['extension'] = '';
                }
            }

            return $a;
        };

        $messagesArray = [];
        foreach ($messages as $key => $message) {
            if ($message instanceof FormzMessage) {
                $message = $message->toArray();
            }

            $messagesArray[$key] = $message;
        }

        $addValueToArray($messagesArray);
        $addValueToArray($supportedMessages);

        $messagesResult = $supportedMessages;

        ArrayUtility::mergeRecursiveWithOverrule(
            $messagesResult,
            $messagesArray,
            (bool)$canCreateNewMessages
        );

        return $messagesResult;
    }