1: <?php
2:
3: /**
4: * Copyright 2016 LINE Corporation
5: *
6: * LINE Corporation licenses this file to you under the Apache License,
7: * version 2.0 (the "License"); you may not use this file except in compliance
8: * with the License. You may obtain a copy of the License at:
9: *
10: * https://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15: * License for the specific language governing permissions and limitations
16: * under the License.
17: */
18:
19: namespace LINE\LINEBot\Event;
20:
21: use LINE\LINEBot\Constant\EventSourceType;
22: use LINE\LINEBot\Exception\InvalidEventSourceException;
23:
24: /**
25: * Base class of each events.
26: *
27: * Don't instantiate this class individually.
28: *
29: * @package LINE\LINEBot\Event
30: */
31: class BaseEvent
32: {
33: /** @var array */
34: protected $event;
35:
36: /**
37: * BaseEvent constructor.
38: *
39: * @param array $event
40: */
41: public function __construct($event)
42: {
43: $this->event = $event;
44: }
45:
46: /**
47: * Returns event type.
48: *
49: * @return string
50: */
51: public function getType()
52: {
53: return $this->event['type'];
54: }
55:
56: /**
57: * Returns timestamp of the event.
58: *
59: * @return int
60: */
61: public function getTimestamp()
62: {
63: return $this->event['timestamp'];
64: }
65:
66: /**
67: * Returns reply token of the event.
68: *
69: * @return string|null
70: */
71: public function getReplyToken()
72: {
73: return array_key_exists('replyToken', $this->event) ? $this->event['replyToken'] : null;
74: }
75:
76: /**
77: * Returns the event is user's one or not.
78: *
79: * @return bool
80: */
81: public function isUserEvent()
82: {
83: return $this->event['source']['type'] === EventSourceType::USER;
84: }
85:
86: /**
87: * Returns the event is group's one or not.
88: *
89: * @return bool
90: */
91: public function isGroupEvent()
92: {
93: return $this->event['source']['type'] === EventSourceType::GROUP;
94: }
95:
96: /**
97: * Returns the event is room's one or not.
98: *
99: * @return bool
100: */
101: public function isRoomEvent()
102: {
103: return $this->event['source']['type'] === EventSourceType::ROOM;
104: }
105:
106: /**
107: * Returns user ID of the event.
108: *
109: * @return string|null
110: * @throws InvalidEventSourceException Raise when called with non user type event.
111: */
112: public function getUserId()
113: {
114: if (!$this->isUserEvent()) {
115: throw new InvalidEventSourceException('This event source is not a user type');
116: }
117: return array_key_exists('userId', $this->event['source'])
118: ? $this->event['source']['userId']
119: : null;
120: }
121:
122: /**
123: * Returns group ID of the event.
124: *
125: * @return string|null
126: * @throws InvalidEventSourceException Raise when called with non group type event.
127: */
128: public function getGroupId()
129: {
130: if (!$this->isGroupEvent()) {
131: throw new InvalidEventSourceException('This event source is not a group type');
132: }
133: return array_key_exists('groupId', $this->event['source'])
134: ? $this->event['source']['groupId']
135: : null;
136: }
137:
138: /**
139: * Returns room ID of the event.
140: *
141: * @return string|null
142: * @throws InvalidEventSourceException Raise when called with non room type event.
143: */
144: public function getRoomId()
145: {
146: if (!$this->isRoomEvent()) {
147: throw new InvalidEventSourceException('This event source is not a room type');
148: }
149: return array_key_exists('roomId', $this->event['source'])
150: ? $this->event['source']['roomId']
151: : null;
152: }
153:
154: /**
155: * Returns the identifier of the event source that associated with event source type
156: * (i.e. userId, groupId or roomId).
157: *
158: * @return null|string
159: *
160: * @throws InvalidEventSourceException Raise when event source type is invalid
161: */
162: public function getEventSourceId()
163: {
164: if ($this->isUserEvent()) {
165: return $this->getUserId();
166: }
167:
168: if ($this->isGroupEvent()) {
169: return $this->getGroupId();
170: }
171:
172: if ($this->isRoomEvent()) {
173: return $this->getRoomId();
174: }
175:
176: throw new InvalidEventSourceException('Invalid event source type, neither `user`, `room` nor `group`');
177: }
178: }
179: