Completed
Push — master ( 3347ae...8ec7fe )
by Romain
9s
created

QuickReply::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
namespace Kerox\Messenger\Model\Message;
3
4
use Kerox\Messenger\ValidatorTrait;
5
6
class QuickReply implements \JsonSerializable
7
{
8
    use ValidatorTrait;
9
10
    const CONTENT_TYPE_TEXT = 'text';
11
    const CONTENT_TYPE_LOCATION = 'location';
12
13
    /**
14
     * @var string
15
     */
16
    protected $contentType;
17
18
    /**
19
     * @var null|string
20
     */
21
    protected $title;
22
23
    /**
24
     * @var null|string
25
     */
26
    protected $payload;
27
28
    /**
29
     * @var null|string
30
     */
31
    protected $imageUrl;
32
33
    /**
34
     * QuickReply constructor.
35
     *
36
     * @param string $contentType
37
     * @throws \Exception
38
     */
39
    public function __construct(string $contentType)
40
    {
41
        $allowedContentType = $this->getAllowedContentType();
42
        if (!in_array($contentType, $allowedContentType)) {
43
            throw new \InvalidArgumentException('Invalid content type');
44
        }
45
        $this->contentType = $contentType;
46
    }
47
48
    /**
49
     * @param string $title
50
     * @return \Kerox\Messenger\Model\Message\QuickReply
51
     */
52
    public function setTitle(string $title): QuickReply
53
    {
54
        $this->checkContentType();
55
        $this->isValidString($title, 20);
56
        $this->title = $title;
57
58
        return $this;
59
    }
60
61
    /**
62
     * @param mixed $payload
63
     * @return \Kerox\Messenger\Model\Message\QuickReply
64
     */
65
    public function setPayload(string $payload): QuickReply
66
    {
67
        $this->checkContentType();
68
        $this->isValidString($payload, 1000);
69
        $this->payload = $payload;
70
71
        return $this;
72
    }
73
74
    /**
75
     * @param mixed $imageUrl
76
     * @return \Kerox\Messenger\Model\Message\QuickReply
77
     */
78
    public function setImageUrl(string $imageUrl): QuickReply
79
    {
80
        $this->checkContentType();
81
        $this->isValidUrl($imageUrl);
82
        $this->imageUrl = $imageUrl;
83
84
        return $this;
85
    }
86
87
    /**
88
     * @return array
89
     */
90
    private function getAllowedContentType(): array
91
    {
92
        return [
93
            self::CONTENT_TYPE_TEXT,
94
            self::CONTENT_TYPE_LOCATION,
95
        ];
96
    }
97
98
    /**
99
     * @return void
100
     * @throws \Exception
101
     */
102
    private function checkContentType()
103
    {
104
        if ($this->contentType === self::CONTENT_TYPE_LOCATION) {
105
            throw new \Exception('Content type is set to location');
106
        }
107
    }
108
109
    /**
110
     * @return array
111
     */
112
    public function jsonSerialize(): array
113
    {
114
        $quickReply = [
115
            'content_type' => $this->contentType,
116
            'title' => $this->title,
117
            'payload' => $this->payload,
118
            'image_url' => $this->imageUrl,
119
        ];
120
121
        return array_filter($quickReply);
122
    }
123
}
124