1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace unreal4u\TelegramAPI\Abstracts; |
6
|
|
|
|
7
|
|
|
use Psr\Log\LoggerInterface; |
8
|
|
|
use unreal4u\TelegramAPI\InternalFunctionality\DummyLogger; |
9
|
|
|
|
10
|
|
|
abstract class TelegramTypes |
11
|
|
|
{ |
12
|
|
|
protected $logger = null; |
13
|
|
|
|
14
|
17 |
|
public function __construct(array $data = null, LoggerInterface $logger = null) |
15
|
|
|
{ |
16
|
17 |
|
if (is_null($logger)) { |
17
|
4 |
|
$logger = new DummyLogger(); |
18
|
|
|
} |
19
|
|
|
|
20
|
17 |
|
$this->logger = $logger; |
21
|
17 |
|
$this->populateObject($data); |
22
|
17 |
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Fills the class with the data passed on through the constructor |
26
|
|
|
* |
27
|
|
|
* @param array $data |
28
|
|
|
* @return TelegramTypes |
29
|
|
|
*/ |
30
|
17 |
|
final protected function populateObject(array $data = null): TelegramTypes |
31
|
|
|
{ |
32
|
17 |
|
if (!is_null($data)) { |
33
|
16 |
|
$this->logger->debug('Detected incoming data on object, foreaching the general data', [ |
34
|
16 |
|
'object' => get_class($this) |
35
|
|
|
]); |
36
|
16 |
|
foreach ($data as $key => $value) { |
37
|
16 |
|
$candidateKey = null; |
38
|
16 |
|
if (is_array($value)) { |
39
|
14 |
|
$this->logger->debug('Array detected, mapping subobjects for key', ['key' => $key]); |
40
|
14 |
|
$candidateKey = $this->mapSubObjects($key, $value); |
41
|
|
|
} |
42
|
|
|
|
43
|
16 |
|
if (!empty($candidateKey)) { |
44
|
14 |
|
if ($candidateKey instanceof CustomType) { |
45
|
4 |
|
$this->logger->debug('Done with mapping, injecting custom data type to class', ['key' => $key]); |
46
|
4 |
|
$this->$key = $candidateKey->data; |
|
|
|
|
47
|
|
|
} else { |
48
|
11 |
|
$this->logger->debug('Done with mapping, injecting native data type to class', ['key' => $key]); |
49
|
14 |
|
$this->$key = $candidateKey; |
50
|
|
|
} |
51
|
|
|
} else { |
52
|
16 |
|
$this->logger->debug('Performing direct assign for key', ['key' => $key]); |
53
|
16 |
|
$this->$key = $value; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
17 |
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* The default is that we have no subobjects at all, so this function will return nothing |
63
|
|
|
* |
64
|
|
|
* @param string $key |
65
|
|
|
* @param array $data |
66
|
|
|
* |
67
|
|
|
* @return TelegramTypes |
68
|
|
|
*/ |
69
|
|
|
protected function mapSubObjects(string $key, array $data): TelegramTypes |
70
|
|
|
{ |
71
|
|
|
return null; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.