1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the slince/shopify-api-php |
5
|
|
|
* |
6
|
|
|
* (c) Slince <[email protected]> |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Slince\Shopify\Common\Manager; |
13
|
|
|
|
14
|
|
|
use Doctrine\Common\Inflector\Inflector; |
15
|
|
|
use Slince\Shopify\Common\CursorBasedPagination; |
16
|
|
|
use Slince\Shopify\Common\Model\ModelInterface; |
17
|
|
|
|
18
|
|
|
abstract class GeneralCurdable extends AbstractManager |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Finds the resources by given query condition. |
22
|
|
|
* |
23
|
|
|
* @param array $query |
24
|
|
|
* |
25
|
|
|
* @return ModelInterface[] |
26
|
|
|
*/ |
27
|
|
|
public function findAll(array $query = []) |
28
|
|
|
{ |
29
|
|
|
$data = $this->client->get(Inflector::pluralize($this->getResourceName()), $query); |
30
|
|
|
|
31
|
|
|
return $this->createMany(reset($data)); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Create a paging query. |
36
|
|
|
* |
37
|
|
|
* @param array $query |
38
|
|
|
* |
39
|
|
|
* @return CursorBasedPagination |
40
|
|
|
* @codeCoverageIgnore |
41
|
|
|
*/ |
42
|
|
|
public function paginate(array $query = []) |
43
|
|
|
{ |
44
|
|
|
$resource = Inflector::pluralize($this->getResourceName()); |
45
|
|
|
|
46
|
|
|
return new CursorBasedPagination($this, $resource, $query); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Finds the resource by given id. |
51
|
|
|
* |
52
|
|
|
* @param int $id |
53
|
|
|
* |
54
|
|
|
* @return ModelInterface |
55
|
|
|
*/ |
56
|
|
|
public function find($id) |
57
|
|
|
{ |
58
|
|
|
$data = $this->client->get(Inflector::pluralize($this->getResourceName()).'/'.$id); |
59
|
|
|
|
60
|
|
|
return $this->fromArray(reset($data)); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Delete a resource. |
65
|
|
|
* |
66
|
|
|
* @param int $id |
67
|
|
|
*/ |
68
|
|
|
public function remove($id) |
69
|
|
|
{ |
70
|
|
|
$this->client->delete(Inflector::pluralize($this->getResourceName()).'/'.$id); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Updates a resource. |
75
|
|
|
* |
76
|
|
|
* @param int $id |
77
|
|
|
* @param array $data |
78
|
|
|
* |
79
|
|
|
* @return ModelInterface |
80
|
|
|
*/ |
81
|
|
|
public function update($id, array $data) |
82
|
|
|
{ |
83
|
|
|
$data = $this->client->put(Inflector::pluralize($this->getResourceName()).'/'.$id, [$this->getResourceName() => $data]); |
84
|
|
|
|
85
|
|
|
return $this->fromArray(reset($data)); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Creates a resource. |
90
|
|
|
* |
91
|
|
|
* @param array $data |
92
|
|
|
* |
93
|
|
|
* @return ModelInterface |
94
|
|
|
*/ |
95
|
|
|
public function create(array $data) |
96
|
|
|
{ |
97
|
|
|
$data = $this->client->post(Inflector::pluralize($this->getResourceName()), [$this->getResourceName() => $data]); |
98
|
|
|
|
99
|
|
|
return $this->fromArray(reset($data)); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Gets the number of resource with given query. |
104
|
|
|
* |
105
|
|
|
* @param array $query |
106
|
|
|
* |
107
|
|
|
* @return int |
108
|
|
|
*/ |
109
|
|
|
public function count(array $query = []) |
110
|
|
|
{ |
111
|
|
|
$partial = Inflector::pluralize($this->getResourceName()).'/count'; |
112
|
|
|
|
113
|
|
|
return $this->client->get($partial, $query)['count']; |
114
|
|
|
} |
115
|
|
|
} |