1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Lifeboat\Models; |
4
|
|
|
|
5
|
|
|
use Lifeboat\Connector; |
6
|
|
|
use Lifeboat\Exceptions\ApiException; |
7
|
|
|
use Lifeboat\Exceptions\InvalidArgumentException; |
8
|
|
|
use Lifeboat\Exceptions\OAuthException; |
9
|
|
|
use Lifeboat\Resource\ObjectResource; |
10
|
|
|
use Lifeboat\Factory\ObjectFactory; |
11
|
|
|
use Lifeboat\Utils\ArrayLib; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class Model |
15
|
|
|
* @package Lifeboat\Models |
16
|
|
|
* |
17
|
|
|
* @property int $ID |
18
|
|
|
* @property string $model |
19
|
|
|
*/ |
20
|
|
|
abstract class Model extends ObjectResource { |
21
|
|
|
|
22
|
|
|
abstract protected function getSaveURL(): string; |
23
|
|
|
|
24
|
|
|
public function __construct(Connector $client, array $_object_data = []) |
25
|
|
|
{ |
26
|
|
|
parent::__construct($client, $_object_data); |
27
|
|
|
|
28
|
|
|
if (!ArrayLib::is_associative($_object_data)) { |
29
|
|
|
throw new InvalidArgumentException("Model::__construct() expects an associative array"); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
if (array_key_exists('model', $_object_data)) $this->model = $_object_data['model']; |
33
|
|
|
$model = $this->model; |
34
|
|
|
|
35
|
|
|
foreach ($_object_data as $field => $value) { |
36
|
|
|
if (is_array($value)) { |
37
|
|
|
if (ArrayLib::is_associative($value)) { |
38
|
|
|
// Model or Object |
39
|
|
|
if (!array_key_exists('field_name', $value)) { |
40
|
|
|
$this->$field = ObjectFactory::make($client, $value); |
41
|
|
|
} else { |
42
|
|
|
// Has One Relation - Deprecated |
43
|
|
|
$name = $value['field_name']; |
44
|
|
|
$this->$name = $value['field_value']; |
45
|
|
|
$this->$field = ObjectFactory::make($client, $value['object_data']); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
// Setup the reverse relationship unless it's already setup |
49
|
|
|
if (!property_exists($this->$field, $model)) { |
50
|
|
|
$this->$field->$model = $this; |
51
|
|
|
} |
52
|
|
|
} else { |
53
|
|
|
// HasMany / ManyMany Relation |
54
|
|
|
$list = []; |
55
|
|
|
foreach ($value as $item) { |
56
|
|
|
if (ArrayLib::is_associative($item)) { |
57
|
|
|
$list = ObjectFactory::make($client, $item); |
58
|
|
|
} else { |
59
|
|
|
$list = $item; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$this->$field = $list; |
64
|
|
|
} |
65
|
|
|
} else { |
66
|
|
|
$this->$field = $value; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return array |
73
|
|
|
*/ |
74
|
|
|
public function toArray(): array |
75
|
|
|
{ |
76
|
|
|
return iterator_to_array($this->getIterator()); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return bool |
81
|
|
|
*/ |
82
|
|
|
public function exists(): bool |
83
|
|
|
{ |
84
|
|
|
return $this->ID > 0; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return Model|null |
89
|
|
|
* @throws ApiException |
90
|
|
|
* @throws OAuthException |
91
|
|
|
*/ |
92
|
|
|
public function save(): ?Model |
93
|
|
|
{ |
94
|
|
|
$curl = $this->getClient()->curl_api($this->getSaveURL(), 'POST', $this->toArray()); |
95
|
|
|
|
96
|
|
|
if ($curl->isValid() && $curl->isJSON()) { |
97
|
|
|
/** @var Model|null $model */ |
98
|
|
|
$model = ObjectFactory::make($this->getClient(), $curl->getJSON()); |
|
|
|
|
99
|
|
|
return $model; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
throw new ApiException($curl->getError()); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|