-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathResourceModel.php
63 lines (50 loc) · 1.63 KB
/
ResourceModel.php
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
<?php
namespace BigCommerce\ApiV3\ResourceModels;
use JsonSerializable;
use stdClass;
abstract class ResourceModel implements JsonSerializable
{
private ?stdClass $optionObject;
public function __construct(?stdClass $optionObject = null)
{
$this->optionObject = $optionObject;
$this->beforeBuildObject();
if (!is_null($this->optionObject)) {
foreach ($this->optionObject as $key => $value) {
$this->$key = $value;
}
}
}
public function jsonSerialize(): array
{
$data = [];
foreach ($this as $key => $value) {
if (isset($this->$key) && $key !== 'optionObject') {
$data[$key] = $value;
}
}
return $data;
}
protected function buildObjectArray(string $property, string $className): void
{
if (!is_null($this->optionObject) && isset($this->optionObject->$property)) {
$this->$property = array_map(function ($m) use ($className) {
return new $className($m);
}, $this->optionObject->$property);
unset($this->optionObject->$property);
}
}
protected function buildPropertyObject(string $property, string $className): void
{
if (!is_null($this->optionObject) && isset($this->optionObject->$property)) {
$this->$property = new $className($this->optionObject->$property);
unset($this->optionObject->$property);
}
}
/**
* Override this function to implement custom build functionality
*/
protected function beforeBuildObject(): void
{
}
}