1
<?php
2

3
declare(strict_types=1);
4

5
namespace Kreait\Firebase\Messaging;
6

7
use Kreait\Firebase\Exception\InvalidArgumentException;
8

9
final class MessageData implements \JsonSerializable
10
{
11
    /** @var array<string, string> */
12
    private $data = [];
13

14 12
    private function __construct()
15
    {
16
    }
17

18
    /**
19
     * @param array<string, string> $data
20
     */
21 12
    public static function fromArray(array $data): self
22
    {
23 12
        $messageData = new self();
24

25 12
        foreach ($data as $key => $value) {
26 12
            if (!self::isStringable($key) || !self::isStringable($value)) {
27 9
                throw new InvalidArgumentException('Message data must be a one-dimensional array of string(able) keys and values.');
28
            }
29

30 12
            $messageData->data[(string) $key] = (string) $value;
31
        }
32

33 12
        return $messageData;
34
    }
35

36
    /**
37
     * @return array<string, string>
38
     */
39 12
    public function jsonSerialize(): array
40
    {
41 12
        return $this->data;
42
    }
43

44
    /**
45
     * @param mixed $value
46
     */
47 12
    private static function isStringable($value): bool
48
    {
49 12
        return \is_null($value) || \is_scalar($value) || (\is_object($value) && \method_exists($value, '__toString'));
50
    }
51
}

Read our documentation on viewing source code .

Loading