1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Guillermoandrae\Highrise\Repositories; |
4
|
|
|
|
5
|
|
|
use Guillermoandrae\Common\Collection; |
6
|
|
|
use Guillermoandrae\Common\CollectionInterface; |
7
|
|
|
use Guillermoandrae\Highrise\Models\ModelFactory; |
8
|
|
|
use Guillermoandrae\Repositories\AbstractRepository as BaseAbstractRepository; |
9
|
|
|
use Guillermoandrae\Models\ModelInterface as BaseModelInterface; |
10
|
|
|
use Guillermoandrae\Highrise\Helpers\Xml; |
11
|
|
|
use Guillermoandrae\Highrise\Http\AdapterAwareTrait; |
12
|
|
|
use Guillermoandrae\Highrise\Http\AdapterInterface; |
13
|
|
|
use ICanBoogie\Inflector; |
14
|
|
|
|
15
|
|
|
abstract class AbstractRepository extends BaseAbstractRepository implements RepositoryInterface |
16
|
|
|
{ |
17
|
|
|
use AdapterAwareTrait; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The name to be used in the resource endpoint. |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $name; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The name of the associated model. |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $modelName; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Builds the resource object. |
35
|
|
|
* |
36
|
|
|
* @param AdapterInterface $adapter The HTTP adapter. |
37
|
|
|
*/ |
38
|
|
|
public function __construct(AdapterInterface $adapter) |
39
|
|
|
{ |
40
|
|
|
$this->setAdapter($adapter); |
41
|
|
|
if (!$this->name) { |
42
|
|
|
$this->name = strtolower( |
43
|
|
|
str_replace([__NAMESPACE__ . '\\', 'Repository'], '', get_class($this)) |
44
|
|
|
); |
45
|
|
|
} |
46
|
|
|
if (!$this->modelName) { |
47
|
|
|
$this->modelName = $this->name; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function findById($id): BaseModelInterface |
52
|
|
|
{ |
53
|
|
|
$uri = sprintf('/%s/%s.xml', $this->getName(), $id); |
54
|
|
|
$results = $this->getAdapter()->request('GET', $uri); |
55
|
|
|
return $this->hydrate($results); |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function findWhere(array $where, int $offset = 0, int $limit = null): CollectionInterface |
59
|
|
|
{ |
60
|
|
|
$uri = sprintf('/%s.xml', $this->getName()); |
61
|
|
|
$results = $this->getAdapter()->request('GET', $uri, ['query' => $where]); |
62
|
|
|
return $this->hydrate($results); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function findAll(int $offset = 0, int $limit = null): CollectionInterface |
66
|
|
|
{ |
67
|
|
|
$uri = sprintf('/%s.xml', $this->getName()); |
68
|
|
|
$results = $this->getAdapter()->request('GET', $uri); |
69
|
|
|
return $this->hydrate($results); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function search(array $criteria = []): CollectionInterface |
73
|
|
|
{ |
74
|
|
|
$uri = sprintf('/%s/search.xml', $this->getName()); |
75
|
|
|
$query = []; |
76
|
|
|
if (isset($criteria['term'])) { |
77
|
|
|
$query['term'] = $criteria['term']; |
78
|
|
|
} else { |
79
|
|
|
foreach ($criteria as $key => $value) { |
80
|
|
|
$query[sprintf('criteria[%s]', $key)] = $value; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
$results = $this->getAdapter()->request('GET', $uri, ['query' => $query]); |
84
|
|
|
return $this->hydrate($results); |
85
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function create(array $data): BaseModelInterface |
89
|
|
|
{ |
90
|
|
|
$uri = sprintf('/%s.xml', $this->getName()); |
91
|
|
|
$body = Xml::toXml(Inflector::get()->singularize($this->getName()), $data); |
92
|
|
|
$results = $this->getAdapter()->request('POST', $uri, ['body' => $body]); |
93
|
|
|
return $this->hydrate($results); |
|
|
|
|
94
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function update($id, array $data): BaseModelInterface |
98
|
|
|
{ |
99
|
|
|
$uri = sprintf('/%s/%s.xml?reload=true', $this->getName(), $id); |
100
|
|
|
$body = Xml::toXml(Inflector::get()->singularize($this->getName()), $data); |
101
|
|
|
$results = $this->getAdapter()->request('PUT', $uri, ['body' => $body]); |
102
|
|
|
return $this->hydrate($results); |
|
|
|
|
103
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function delete($id): bool |
107
|
|
|
{ |
108
|
|
|
$uri = sprintf('/%s/%s.xml', $this->getName(), $id); |
109
|
|
|
return $this->getAdapter()->request('DELETE', $uri); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
final public function getName(): string |
113
|
|
|
{ |
114
|
|
|
return $this->name; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
final public function getModelName(): string |
118
|
|
|
{ |
119
|
|
|
return $this->modelName; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
protected function hydrate(string $xml) |
123
|
|
|
{ |
124
|
|
|
libxml_use_internal_errors(true); |
125
|
|
|
if (!$obj = simplexml_load_string($xml)) { |
126
|
|
|
return []; |
127
|
|
|
} |
128
|
|
|
if ($obj->attributes()) { |
129
|
|
|
$items = []; |
130
|
|
|
foreach ($obj->children() as $child) { |
131
|
|
|
$items[] = $this->hydrate($child); |
132
|
|
|
} |
133
|
|
|
return Collection::make($items); |
134
|
|
|
} |
135
|
|
|
return ModelFactory::factory(Inflector::get()->singularize($this->getModelName()), $xml); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|