-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathModel.php
More file actions
87 lines (76 loc) · 1.82 KB
/
Copy pathModel.php
File metadata and controls
87 lines (76 loc) · 1.82 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
<?php
namespace Takeaway;
use Takeaway\Traits\MakesRequests;
/**
* A model containing data, and possibly having related models.
*/
abstract class Model
{
/**
* Data of the model.
* @var array
*/
protected $data;
/**
* Extra data which should not be publicly accessible.
* @var array
*/
protected $extra;
/**
* Whether or not the model has lazy loaded extra data.
* @var boolean
*/
protected $hasLazyLoaded;
/**
* Construct a new model.
* @param array|null $data Data of the model.
* @param array|null $extra Extra data which should not be publicly
* accessible.
*/
public function __construct($data = [], $extra = [])
{
$this->data = $data;
$this->extra = $extra;
$this->hasLazyLoaded = false;
}
/**
* Lazy load extra data. Implementations might implement this method if they
* are able to fetch more data on-the-fly.
*
* @return void
*/
protected function lazyLoad()
{
//
}
/**
* Update the data in the model.
*
* Duplicate keys will prefer the provided data.
*
* @param array $data New data to add.
* @return void
*/
protected function fill($data)
{
$this->data = array_merge($this->data, $data);
}
/**
* Access a property of the model.
*
* Attempts to lazy load data if it has not done so already.
*
* @param string $name Name of the property.
* @return mixed|null
*/
public function __get($name)
{
if (!isset($this->data[$name])) {
if (!$this->hasLazyLoaded) {
$this->hasLazyLoaded = true;
$this->lazyLoad();
}
}
return $this->data[$name] ?? null;
}
}