Transform payload

@param Template $message

@return array
@throws \Casperlaitw\LaravelFbMessenger\Exceptions\RequiredArgumentException
484582
public function transform(Template $message)
    {
        if (empty($message->getText())) {
            throw new RequiredArgumentException('Text is required');
        }

        return [
            'template_type' => 'button',
            'text' => $message->getText(),
            'buttons' => $message->getCollections()->toData(),
        ];
    }
571448
public function format($message, array $format)
    {
        $replace = [
            ':message' => $message,
            ':code' => $this->getStatusCode(),
        ];

        array_walk_recursive($format, function (&$value, $key) use ($replace) {
            if (isset($replace[$value])) {
                $value = $replace[$value];
            }
        });

        return $format;
    }
64389
private function preparePayload(Message $message): array
    {
        $payload = [
            'text' => $message->getText(),
            'channel' => $message->getChannel(),
            'username' => $message->getUsername(),
            'link_names' => $this->options['link_names'] ? 1 : 0,
            'unfurl_links' => $this->options['unfurl_links'],
            'unfurl_media' => $this->options['unfurl_media'],
            'mrkdwn' => $this->options['allow_markdown'],
        ];

        if ($icon = $message->getIcon()) {
            $payload[$message->getIconType()] = $icon;
        }

        $payload['attachments'] = $this->getAttachmentsAsArrays($message);

        return $payload;
    }
1166447
protected function interpolate($message, array $context = [])
    {
        if (is_string($message)) {
            return $this->interpolateString($message, $context);
        }
        
        if (is_array($message) && $this->isArraySupported) {
            return \array_merge($message, $context);
        }
        
        return $this->interpolateString(
            var_export($message, true),
            $context
        );
    }
1113283
protected function interpolate($message, array $context = [])
    {
        $replace = [];
        foreach ($context as $key => $val) {
            // check that the value can be casted to string
            if (!is_array($val) && (!is_object($val) || method_exists($val, '__toString'))) {
                $replace['{' . $key . '}'] = $val;
            }
        }

        return strtr($message, $replace);
    }
1269281
public function formatMessage($message)
    {
        if ($message instanceof \Exception || (interface_exists('\Throwable') && $message instanceof \Throwable)) {
            return $this->exceptionToString($message);
        } elseif ($message instanceof \JsonSerializable) {
            return json_encode($message, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
        } elseif ($message instanceof \Serializable) {
            return serialize($message);
        } elseif (!is_array($message)) {
            return (string)$message;
        }

        if (!isset($message[0]) || !is_string($message[0])) {
            return json_encode($message, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
        }

        if (substr_count($message[0], '%') + 1 >= ($count = count($message)) && isset($message[$count - 1])) {
            foreach ((array)$message as $k => $v) {
                if ($k === 0 || is_scalar($v) || $v === null) {
                    continue;
                }

                if ($v instanceof \Exception || (interface_exists('\Throwable') && $v instanceof \Throwable)) {
                    $message[$k] = $this->exceptionToString($v);
                } elseif (is_array($v) || $v instanceof \JsonSerializable) {
                    $message[$k] = json_encode($v, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
                }
            }
            /** @noinspection ArgumentUnpackingCanBeUsedInspection */
            return (string)call_user_func_array('sprintf', $message);
        }

        if (count($message) === 2) {
            if (isset($message[1]) && strpos($message[0], ':1') === false) {
                $message[0] = rtrim($message[0], ': ') . ': :1';
            }
        } elseif (count($message) === 3) {
            /** @noinspection NotOptimalIfConditionsInspection */
            if (isset($message[1], $message[2]) && strpos($message[0], ':1') === false && is_scalar($message[1])) {
                $message[0] = rtrim($message[0], ': ') . ': :1 => :2';
            }
        }

        $replaces = [];
        /** @noinspection ForeachSourceInspection */
        foreach ($message as $k => $v) {
            if ($k === 0) {
                continue;
            }

            if ($v instanceof \Exception || (interface_exists('\Throwable') && $v instanceof \Throwable)) {
                $v = $this->exceptionToString($v);
            } elseif (is_array($v) || $v instanceof \JsonSerializable) {
                $v = json_encode($v, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
            } elseif ($v instanceof \Serializable) {
                $v = serialize($v);
            } elseif (is_string($v)) {
                null;
            } elseif ($v === null || is_scalar($v)) {
                $v = json_encode($v);
            } else {
                $v = (string)$v;
            }

            $replaces[":$k"] = $v;
        }

        return strtr($message[0], $replaces);
    }
936072
public function renderImg($message, $extension = null, $size = null, $padding = null, $attribs = array())
    {
        if (isset($extension)) {
            if (is_array($extension)) {
                $attribs = $extension;
                $extension = null;
            } elseif (isset($size)) {
                if (is_array($size)) {
                    $attribs = $size;
                    $size = null;
                } elseif (isset($padding) && is_array($padding)) {
                    $attribs = $padding;
                    $padding = null;
                }
            }
        }

        return $this->renderer->render(self::IMG_TEMPLATE, array(
            'src'       => $this->assembleRoute($message, $extension, $size, $padding),
            'attribs'   => $attribs
        ));
    }
23314
private function createFromTemplate($message, $template) {
        ob_start();
        ob_implicit_flush(false);

        if (is_array($message) && !empty($message)) {
            extract($message, EXTR_OVERWRITE);
        }

        $current_module = RouteController::$currentRoute['module'];
        $templatePath = MODULES_DIR . '/' . $current_module . '/Views/' . $template . '.php';
        require $templatePath;
        return ob_get_clean();
    }
144399
protected function parseMessage($message)
    {
        $headerAndBody = explode("\r\n\r\n", $message, 2);
        $headers = explode("\r\n", $headerAndBody[0]);

        return [$this->parseHeader($headers), $headerAndBody[1]];

    }
868464
public function withMessage(string $message): PayloadInterface
    {
        if ($this->message === $message) {
            return $this;
        }

        $clone = clone $this;
        $clone->message = $message;

        return $clone;
    }
174896
public function serialize(Message $message, array $options = [])
    {
        $userMessage = $this->getUserMessage($message);

        //mainly for tests or if someting went wrong
        if (!$userMessage) {
            $userMessage = new UserMessage();
        }

        $data = [
          'id' => $message->getUuid(),
          'object' => $message->getObject(),
          'content' => $message->getContent(),
          'to' => $message->getTo(),
          'meta' => [
            'date' => DateNormalizer::normalize($message->getDate()),
            'read' => $userMessage->isRead(),
            'removed' => $userMessage->isRemoved(),
            'sent' => $userMessage->isSent(),
            'umuuid' => $userMessage->getUuid(),
          ],
        ];

        $data['from'] = $message->getSender() ?
            $this->userSerializer->serialize($message->getSender(), [Options::SERIALIZE_MINIMAL]) :
            ['username' => $message->getSenderUsername()];

        if (in_array(Options::IS_RECURSIVE, $options)) {
            $data['children'] = array_map(function (Message $child) {
                return $this->serialize($child, $options);
            }, $message->getChildren()->toArray());
        }

        return $data;
    }
170960
protected function prefixMessage($message) {
        if ($this->prefix !== null) {
            return s('%s: %s', $this->prefix, $message);
        }

        return $message;
    }
1105573
public function translate($message)
    {
        // message must be an array
        if (!is_array($message)) {
            return null;
        }

        // we must have an event property
        if (!isset($message['event'])) {
            return null;
        }

        // don't include the event name as an attribute
        $attributes = $message;
        unset($attributes['event']);

        return new SimpleEvent($message['event'], $attributes);
    }
554929
public static function t($message)
    {
        if (!isset(static::$_handlers[static::$_domain])) {
            static::loadHandler('Horde_Translation_Handler_Gettext');
        }
        return static::$_handlers[static::$_domain]->t($message);
    }
462731
public function translate($message, array $params = array())
    {
        // Update the current message with the domain translation, if we have one.
        if (isset($this->messages[$message]) && ! empty($this->messages[$message])) {
            $message = $this->messages[$message];
        }

        if (empty($params)) {
            return $message;
        }

        $formatter = new MessageFormatter();

        return $formatter->format($message, $params, $this->locale);
    }
977803
public function route(Message $message) : array
    {
        if (!is_object($message->getPayload())) {
            throw new InvalidArgumentException("Can't route by payload type, when payload is not object. Got: {$message->getPayload()}");
        }

        $classToRoute = get_class($message->getPayload());
        if ($this->routeByClassName) {
            return [$classToRoute];
        }

        $channelsToRoute = [];
        foreach ($this->typeToChannelMapping as $type => $channelName) {
            if ($classToRoute === $type) {
                $channelsToRoute[] = $channelName;
            }
        }

        return $channelsToRoute;
    }
281034
private function getHeaders(PushMessage $message) : array
    {
        return [
            'Authorization' => 'key='.$this->apiKey,
            'Content-Type' => 'application/json',
            'Content-Length' => $message->getContentLength(),
            'Encryption' => 'keyid=p256dh;salt='.$message->getSalt(),
            'Crypto-Key' => 'keyid=p256dh;dh='.$message->getCryptoKey(),
            'Content-Encoding' => 'aesgcm',
            'TTL' => $message->getTTL(),
        ];
    }