usamamuneerchaudhary /
daftravel
| 1 | <?php |
||
| 2 | |||
| 3 | namespace UsamamuneerChaudhary\Daftravel\Services; |
||
| 4 | |||
| 5 | use UsamamuneerChaudhary\Daftravel\Http\Client; |
||
| 6 | |||
| 7 | abstract class BaseService |
||
| 8 | { |
||
| 9 | protected Client $client; |
||
| 10 | protected array $config; |
||
| 11 | protected ?string $endpoint; |
||
| 12 | |||
| 13 | public function __construct(Client $client, array $config) |
||
| 14 | { |
||
| 15 | $this->client = $client; |
||
| 16 | $this->config = $config; |
||
| 17 | } |
||
| 18 | |||
| 19 | public function all(array $params = []) |
||
| 20 | { |
||
| 21 | return $this->client->get($this->endpoint . '.json', $params); |
||
| 22 | } |
||
| 23 | |||
| 24 | public function find(int $id) |
||
| 25 | { |
||
| 26 | return $this->client->get("{$this->endpoint}/{$id}.json"); |
||
| 27 | } |
||
| 28 | |||
| 29 | public function create(array $data) |
||
| 30 | { |
||
| 31 | return $this->client->post($this->endpoint, $data); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 32 | } |
||
| 33 | |||
| 34 | public function update(int $id, array $data) |
||
| 35 | { |
||
| 36 | return $this->client->put("{$this->endpoint}/{$id}", $data); |
||
| 37 | } |
||
| 38 | |||
| 39 | public function delete(int $id) |
||
| 40 | { |
||
| 41 | return $this->client->delete("{$this->endpoint}/{$id}"); |
||
| 42 | } |
||
| 43 | |||
| 44 | public function paginate(int $page = 1, int $limit = 20, array $params = []) |
||
| 45 | { |
||
| 46 | // Validate parameters according to Daftra API documentation |
||
| 47 | $page = max(1, $page); // page >= 1 |
||
| 48 | $limit = max(1, min(1000, $limit)); // limit between 1 and 1000 |
||
| 49 | |||
| 50 | $params['page'] = $page; |
||
| 51 | $params['limit'] = $limit; |
||
| 52 | |||
| 53 | return $this->client->get($this->endpoint . '.json', $params); |
||
| 54 | } |
||
| 55 | } |
||
| 56 |