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