Repository navigation
Expand file tree
/
Copy pathAbstractModel.php
More file actions
129 lines (111 loc) · 2.37 KB
/
Copy pathAbstractModel.php
File metadata and controls
129 lines (111 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php namespace Gitlab\Model;
use Gitlab\Client;
use Gitlab\Exception\RuntimeException;
use Gitlab\Api\AbstractApi;
abstract class AbstractModel
{
/**
* @var array
*/
protected static $properties;
/**
* @var array
*/
protected $data = array();
/**
* @var Client
*/
protected $client;
/**
* @return Client
*/
public function getClient()
{
return $this->client;
}
/**
* @param Client $client
* @return $this
*/
public function setClient(Client $client = null)
{
if (null !== $client) {
$this->client = $client;
}
return $this;
}
/**
* @param string $api
* @return AbstractApi|mixed
*/
public function api($api)
{
return $this->getClient()->api($api);
}
/**
* @param array $data
* @return $this
*/
protected function hydrate(array $data = array())
{
if (!empty($data)) {
foreach ($data as $field => $value) {
$this->setData($field, $value);
}
}
return $this;
}
/**
* @param string $field
* @param mixed $value
* @return $this
*/
protected function setData($field, $value)
{
if (in_array($field, static::$properties)) {
$this->data[$field] = $value;
}
return $this;
}
/**
* @return array
*/
public function getData()
{
return $this->data;
}
/**
* @param string $property
* @param mixed $value
* @throws RuntimeException
*/
public function __set($property, $value)
{
throw new RuntimeException('Model properties are immutable');
}
/**
* @param string $property
* @return mixed
*/
public function __get($property)
{
if (!in_array($property, static::$properties)) {
throw new RuntimeException(sprintf(
'Property "%s" does not exist for %s object',
$property, get_called_class()
));
}
if (isset($this->data[$property])) {
return $this->data[$property];
}
return null;
}
/**
* @param string $property
* @return bool
*/
public function __isset($property)
{
return isset($this->data[$property]);
}
}