1
<?php
2

3
declare(strict_types=1);
4

5
namespace Kreait\Firebase\Messaging;
6

7
use Countable;
8
use IteratorAggregate;
9
use Kreait\Firebase\Exception\InvalidArgumentException;
10
use Traversable;
11

12
/**
13
 * @implements IteratorAggregate<RegistrationToken>
14
 */
15
final class RegistrationTokens implements Countable, IteratorAggregate
16
{
17
    /** @var RegistrationToken[] */
18
    private $tokens;
19

20 12
    public function __construct(RegistrationToken ...$tokens)
21
    {
22 12
        $this->tokens = $tokens;
23
    }
24

25
    /**
26
     * @param mixed $values
27
     *
28
     * @throws InvalidArgumentException
29
     */
30 12
    public static function fromValue($values): self
31
    {
32 12
        if ($values instanceof self) {
33 9
            $tokens = $values->values();
34 12
        } elseif ($values instanceof RegistrationToken) {
35 9
            $tokens = [$values];
36 12
        } elseif (\is_string($values)) {
37 12
            $tokens = [RegistrationToken::fromValue($values)];
38 12
        } elseif (\is_array($values)) {
39 12
            $tokens = [];
40

41 12
            foreach ($values as $value) {
42 12
                if ($value instanceof RegistrationToken) {
43 9
                    $tokens[] = $value;
44 12
                } elseif (\is_string($value)) {
45 12
                    $tokens[] = RegistrationToken::fromValue($value);
46
                }
47
            }
48
        } else {
49 9
            throw new InvalidArgumentException('Unsupported value(s)');
50
        }
51

52 12
        return new self(...$tokens);
53
    }
54

55
    /**
56
     * @codeCoverageIgnore
57
     *
58
     * @return Traversable<RegistrationToken>|RegistrationToken[]
59
     */
60
    public function getIterator()
61
    {
62
        yield from $this->tokens;
63
    }
64

65 12
    public function isEmpty(): bool
66
    {
67 12
        return \count($this->tokens) === 0;
68
    }
69

70
    /**
71
     * @return RegistrationToken[]
72
     */
73 9
    public function values(): array
74
    {
75 9
        return $this->tokens;
76
    }
77

78
    /**
79
     * @return string[]
80
     */
81 12
    public function asStrings(): array
82
    {
83 12
        return \array_map('strval', $this->tokens);
84
    }
85

86 12
    public function count(): int
87
    {
88 12
        return \count($this->tokens);
89
    }
90
}

Read our documentation on viewing source code .

Loading