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