1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DJStarCOM\BookingComSDK\Models; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class Model |
7
|
|
|
* @package DJStarCOM\BookingComSDK\Models |
8
|
|
|
*/ |
9
|
|
|
abstract class Model |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var array |
13
|
|
|
*/ |
14
|
|
|
protected static $attributeMap = []; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Model primary key. Adding to collection with this key. |
18
|
|
|
* @return string|int|null |
19
|
|
|
*/ |
20
|
|
|
public function getPrimaryKey() |
21
|
|
|
{ |
22
|
|
|
return null; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param array $data |
27
|
|
|
*/ |
28
|
|
|
public function setAttributes(array $data): void |
29
|
|
|
{ |
30
|
|
|
$prepareData = array_intersect_key($data, $this->getAttributeMap()); |
31
|
|
|
|
32
|
|
|
foreach ($prepareData as $key => $value) { |
33
|
|
|
if (!property_exists($this, $key)) { |
34
|
|
|
continue; |
35
|
|
|
} |
36
|
|
|
$key = str_replace('-', '_', $key); |
37
|
|
|
$this->$key = $value; |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return array |
43
|
|
|
*/ |
44
|
|
|
abstract protected function getAttributeMap(): array; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Converts the model into an array. |
48
|
|
|
* @return array |
49
|
|
|
*/ |
50
|
|
|
public function toArray(): array |
51
|
|
|
{ |
52
|
|
|
return get_object_vars($this); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* PHP getter magic method. |
57
|
|
|
* |
58
|
|
|
* @param string $name property name |
59
|
|
|
* @return mixed property value |
60
|
|
|
*/ |
61
|
|
|
public function __get($name) |
62
|
|
|
{ |
63
|
|
|
if (!property_exists($this, $name)) { |
64
|
|
|
return null; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $this->$name; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* PHP setter magic method. |
72
|
|
|
* |
73
|
|
|
* @param string $name property name |
74
|
|
|
* @param mixed $value property value |
75
|
|
|
*/ |
76
|
|
|
public function __set($name, $value): void |
77
|
|
|
{ |
78
|
|
|
$this->$name = $value; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param string $name |
83
|
|
|
* @return bool |
84
|
|
|
*/ |
85
|
|
|
public function __isset($name) |
86
|
|
|
{ |
87
|
|
|
return property_exists($this, $name); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|