Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
Notification.php
1<?php
2
4
7
8final class Notification implements \JsonSerializable
9{
10 private const SEPARATOR = 'u1F9D1';
11
12 private $uid;
13 private $category;
14 private $title;
15 private $text;
16 private $icon;
17 private $inputPlaceholderText;
18 private $button1Text;
19 private $button2Text;
20
38 public function __construct(array $options)
39 {
40 $this->setUid($options['id']);
41 $this->setCategory($options['category']);
42 $this->setTitle($options['title']);
43 $this->setText($options['text']);
44 $this->setIcon($options['icon']);
45 $this->setInputPlaceholderText($options['inputPlaceholderText']);
46 $this->setButton1Text($options['button1Text']);
47 $this->setButton2Text($options['button2Text']);
48 }
49
50 private function setUid($id): void
51 {
52 $id = (string)$id;
53 if ($id === '')
54 {
55 throw new ArgumentException('NotificationManager: Cannot create a notification without an ID');
56 }
57
58 $this->uid = $id . self::SEPARATOR . self::getUuidV4();
59 }
60
61 private function setCategory($category): void
62 {
63 $this->category = (string)$category;
64 }
65
66 private function setTitle($title): void
67 {
68 $this->title = (string)$title;
69 }
70
71 private function setText($text): void
72 {
73 $this->text = (string)$text;
74 }
75
76 private function setIcon($icon): void
77 {
78 $this->icon = (string)$icon;
79 }
80
81 private function setInputPlaceholderText($inputPlaceholderText): void
82 {
83 $this->inputPlaceholderText = (string)$inputPlaceholderText;
84 }
85
86 private function setButton1Text($button1Text): void
87 {
88 $this->button1Text = (string)$button1Text;
89 }
90
91 private function setButton2Text($button2Text): void
92 {
93 $this->button2Text = (string)$button2Text;
94 }
95
96 public function jsonSerialize(): array
97 {
98 return [
99 'id' => $this->uid,
100 'category' => $this->category,
101 'title' => $this->title,
102 'text' => $this->text,
103 'icon' => $this->icon,
104 'inputPlaceholderText' => $this->inputPlaceholderText,
105 'button1Text' => $this->button1Text,
106 'button2Text' => $this->button2Text,
107 ];
108 }
109
110 private static function getUuidV4(): string
111 {
112 return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
113 random_int(0, 0xffff),
114 random_int(0, 0xffff),
115 random_int(0, 0xffff),
116 random_int(0, 0x0fff) | 0x4000,
117 random_int(0, 0x3fff) | 0x8000,
118 random_int(0, 0xffff),
119 random_int(0, 0xffff),
120 random_int(0, 0xffff)
121 );
122 }
123}