|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Base abstract class for supported by Telegram entity classes. |
|
5
|
|
|
* |
|
6
|
|
|
* @package Teebot (Telegram bot framework) |
|
7
|
|
|
* |
|
8
|
|
|
* @author Stanislav Drozdov <[email protected]> |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Teebot\Api\Entity; |
|
12
|
|
|
|
|
13
|
|
|
use Teebot\Api\Traits\Property; |
|
14
|
|
|
|
|
15
|
|
|
abstract class AbstractEntity implements EntityInterface |
|
16
|
|
|
{ |
|
17
|
|
|
use Property; |
|
18
|
|
|
|
|
19
|
|
|
const ENTITY_TYPE = 'AbstractEntity'; |
|
20
|
|
|
|
|
21
|
|
|
protected $parent; |
|
22
|
|
|
|
|
23
|
|
|
protected $builtInEntities = []; |
|
24
|
|
|
|
|
25
|
|
|
protected $supportedProperties = []; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Constructs extended entity's class and sets properties from array if passed. |
|
29
|
|
|
* |
|
30
|
|
|
* @param array $data Array with properties to set |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct(array $data = []) |
|
33
|
|
|
{ |
|
34
|
|
|
if (empty($data)) { |
|
35
|
|
|
return; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
$this->setProperties($data); |
|
39
|
|
|
$this->initBuiltInEntities($data); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Returns entity type |
|
44
|
|
|
* |
|
45
|
|
|
* @return string |
|
46
|
|
|
*/ |
|
47
|
|
|
public function getEntityType() |
|
48
|
|
|
{ |
|
49
|
|
|
return static::ENTITY_TYPE; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Sets parent entity |
|
54
|
|
|
* |
|
55
|
|
|
* @param EntityInterface $parent Parent entity |
|
56
|
|
|
*/ |
|
57
|
|
|
public function setParent(EntityInterface $parent = null) |
|
58
|
|
|
{ |
|
59
|
|
|
$this->parent = $parent; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Returns parent entity |
|
64
|
|
|
* |
|
65
|
|
|
* @return AbstractEntity |
|
66
|
|
|
*/ |
|
67
|
|
|
public function getParent() |
|
68
|
|
|
{ |
|
69
|
|
|
return $this->parent; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Initialises built-in entity classes if any. |
|
74
|
|
|
* |
|
75
|
|
|
* @param array $data Array with data to pass to newly created instance of built-in entity |
|
76
|
|
|
*/ |
|
77
|
|
|
protected function initBuiltInEntities(array $data) |
|
78
|
|
|
{ |
|
79
|
|
|
if (empty($this->builtInEntities)) { |
|
80
|
|
|
return; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
foreach ($this->builtInEntities as $name => $class) { |
|
84
|
|
|
|
|
85
|
|
|
$initValues = null; |
|
86
|
|
|
|
|
87
|
|
|
if (property_exists($this, $name)) { |
|
88
|
|
|
if (isset($this->{$name})) { |
|
89
|
|
|
$initValues = $this->{$name}; |
|
90
|
|
|
} elseif (isset($data[$name])) { |
|
91
|
|
|
$initValues = $data[$name]; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
if ($initValues) { |
|
96
|
|
|
$object = class_exists($class) ? new $class($initValues) : null; |
|
97
|
|
|
$this->setProperty($name, $object); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|