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