-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48 from 5pm-HDH/feature/refactor-request-builder
Refactor RequestBuilder
- Loading branch information
Showing
11 changed files
with
113 additions
and
237 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
|
||
namespace CTApi\Requests; | ||
|
||
|
||
use CTApi\CTClient; | ||
use CTApi\Exceptions\CTModelException; | ||
use CTApi\Models\AbstractModel; | ||
use CTApi\Requests\Traits\OrderByCondition; | ||
use CTApi\Requests\Traits\Pagination; | ||
use CTApi\Requests\Traits\WhereCondition; | ||
use CTApi\Utils\CTResponseUtil; | ||
use GuzzleHttp\Exception\GuzzleException; | ||
|
||
abstract class AbstractRequestBuilder | ||
{ | ||
|
||
use Pagination, WhereCondition, OrderByCondition; | ||
|
||
public function all(): array | ||
{ | ||
$data = $this->collectDataFromPages($this->getApiEndpoint(), []); | ||
return $this->getModelClass()::createModelsFromArray($data); | ||
} | ||
|
||
public function findOrFail(int $id) | ||
{ | ||
$model = $this->find($id); | ||
if ($model != null) { | ||
return $model; | ||
} else { | ||
throw new CTModelException("Could not retrieve model!"); | ||
} | ||
} | ||
|
||
public function find(int $id) | ||
{ | ||
$modelData = null; | ||
try { | ||
$response = CTClient::getClient()->get($this->getApiEndpoint() . '/' . $id); | ||
$modelData = CTResponseUtil::dataAsArray($response); | ||
} catch (GuzzleException $e) { | ||
// ignore | ||
} | ||
|
||
if (empty($modelData)) { | ||
return null; | ||
} else { | ||
return $this->getModelClass()::createModelFromData($modelData); | ||
} | ||
} | ||
|
||
public function get(): array | ||
{ | ||
$options = []; | ||
$this->addWhereConditionsToOption($options); | ||
|
||
$data = $this->collectDataFromPages($this->getApiEndpoint(), $options); | ||
|
||
$this->orderRawData($data); | ||
|
||
return $this->getModelClass()::createModelsFromArray($data); | ||
} | ||
|
||
abstract protected function getApiEndpoint(): string; | ||
abstract protected function getModelClass(): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.