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 Notification implements \JsonSerializable
10
{
11
    /** @var string|null */
12
    private $title;
13

14
    /** @var string|null */
15
    private $body;
16

17
    /** @var string|null */
18
    private $imageUrl;
19

20
    /**
21
     * @throws InvalidArgumentException if both title and body are null
22
     */
23 12
    private function __construct(?string $title = null, ?string $body = null, ?string $imageUrl = null)
24
    {
25 12
        $this->title = $title;
26 12
        $this->body = $body;
27 12
        $this->imageUrl = $imageUrl;
28

29 12
        if ($this->title === null && $this->body === null) {
30 9
            throw new InvalidArgumentException('The title and body of a notification cannot both be NULL');
31
        }
32
    }
33

34
    /**
35
     * @throws InvalidArgumentException if both title and body are null
36
     */
37 9
    public static function create(?string $title = null, ?string $body = null, ?string $imageUrl = null): self
38
    {
39 9
        return new self($title, $body, $imageUrl);
40
    }
41

42
    /**
43
     * @param array<string, string> $data
44
     *
45
     * @throws InvalidArgumentException if both title and body are null
46
     */
47 12
    public static function fromArray(array $data): self
48
    {
49 12
        return new self(
50 12
            $data['title'] ?? null,
51 12
            $data['body'] ?? null,
52 12
            $data['image'] ?? null
53
        );
54
    }
55

56 9
    public function withTitle(string $title): self
57
    {
58 9
        $notification = clone $this;
59 9
        $notification->title = $title;
60

61 9
        return $notification;
62
    }
63

64 9
    public function withBody(string $body): self
65
    {
66 9
        $notification = clone $this;
67 9
        $notification->body = $body;
68

69 9
        return $notification;
70
    }
71

72 9
    public function withImageUrl(string $imageUrl): self
73
    {
74 9
        $notification = clone $this;
75 9
        $notification->imageUrl = $imageUrl;
76

77 9
        return $notification;
78
    }
79

80 9
    public function title(): ?string
81
    {
82 9
        return $this->title;
83
    }
84

85 9
    public function body(): ?string
86
    {
87 9
        return $this->body;
88
    }
89

90 9
    public function imageUrl(): ?string
91
    {
92 9
        return $this->imageUrl;
93
    }
94

95
    /**
96
     * @return array<string, string>
97
     */
98 12
    public function jsonSerialize(): array
99
    {
100 12
        return \array_filter([
101 12
            'title' => $this->title,
102 12
            'body' => $this->body,
103 12
            'image' => $this->imageUrl,
104
        ], static function ($value) {
105 12
            return $value !== null;
106 12
        });
107
    }
108
}

Read our documentation on viewing source code .

Loading