1 | <?php |
||
2 | /** |
||
3 | * Copyright (c) 2019 - present |
||
4 | * updown - BaseObject.php |
||
5 | * author: Roberto Belotti - [email protected] |
||
6 | * web : robertobelotti.com, github.com/biscolab |
||
7 | * Initial version created on: 18/2/2019 |
||
8 | * MIT license: https://github.com/biscolab/updown-php/blob/master/LICENSE |
||
9 | */ |
||
10 | |||
11 | namespace Biscolab\UpDown\Objects; |
||
12 | |||
13 | use Biscolab\UpDown\Abstracts\AbstractCollection; |
||
14 | use Biscolab\UpDown\Abstracts\AbstractObject; |
||
15 | use Biscolab\UpDown\Http\UpDownResponse; |
||
16 | use Biscolab\UpDown\UpDown; |
||
17 | |||
18 | /** |
||
19 | * Class BaseObject |
||
20 | * @package Biscolab\UpDown\BaseObject |
||
21 | */ |
||
22 | class BaseObject extends AbstractObject |
||
23 | { |
||
24 | |||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | protected static $endpoint = ''; |
||
29 | |||
30 | /** |
||
31 | * @var string |
||
32 | */ |
||
33 | protected static $collection_type = ''; |
||
34 | |||
35 | /** |
||
36 | * @var string |
||
37 | */ |
||
38 | protected static $key = ''; |
||
39 | |||
40 | /** |
||
41 | * CrudObject constructor. |
||
42 | * |
||
43 | * @param mixed $args |
||
44 | */ |
||
45 | public function __construct($args = []) |
||
46 | { |
||
47 | |||
48 | if (!is_array($args)) { |
||
49 | $args_[self::getPrimaryKey()] = $args; |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
|
|||
50 | } else { |
||
51 | $args_ = $args; |
||
52 | } |
||
53 | |||
54 | parent::__construct($args_); |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * @return string |
||
59 | */ |
||
60 | public static function getPrimaryKey(): string |
||
61 | { |
||
62 | |||
63 | return static::$key; |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * @return null|AbstractCollection |
||
68 | */ |
||
69 | public function all(): AbstractCollection |
||
70 | { |
||
71 | |||
72 | $path = static::getEndpoint(); |
||
73 | |||
74 | $response = $this->updown->get($path); |
||
75 | |||
76 | if (static::$collection_type) { |
||
77 | |||
78 | return $this->getCollection($response); |
||
79 | } |
||
80 | |||
81 | return null; |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * @return string |
||
86 | */ |
||
87 | protected static function getEndpoint(): string |
||
88 | { |
||
89 | |||
90 | return UpDown::API_URL . static::$endpoint; |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * @param UpDownResponse $response |
||
95 | * |
||
96 | * @return mixed |
||
97 | */ |
||
98 | protected function getCollection(UpDownResponse $response): AbstractCollection |
||
99 | { |
||
100 | |||
101 | $collection_type = static::$collection_type; |
||
102 | |||
103 | return new $collection_type($response->getResult()->getData()); |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * @param bool|null $with_id |
||
108 | * |
||
109 | * @return string |
||
110 | */ |
||
111 | public function prepareApiPath(?bool $with_id = true): string |
||
112 | { |
||
113 | |||
114 | $path = static::getEndpoint(); |
||
115 | |||
116 | if ($with_id) { |
||
117 | $path .= '/' . $this->getId(); |
||
118 | } |
||
119 | |||
120 | return $path; |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * @return mixed |
||
125 | */ |
||
126 | public function getId() |
||
127 | { |
||
128 | |||
129 | return $this->{static::getPrimaryKey()}; |
||
130 | |||
131 | } |
||
132 | |||
133 | /** |
||
134 | * @return string |
||
135 | */ |
||
136 | public function getStaticEndpoint(): string |
||
137 | { |
||
138 | |||
139 | return static::getEndpoint(); |
||
140 | } |
||
141 | |||
142 | } |