generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add estate repository builder query
- Loading branch information
Showing
9 changed files
with
644 additions
and
53 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,126 @@ | ||
<?php | ||
|
||
namespace Katalam\OnOfficeAdapter\Query; | ||
|
||
use Illuminate\Support\Arr; | ||
use Illuminate\Support\Collection; | ||
use Illuminate\Support\Str; | ||
use Illuminate\Support\Traits\Conditionable; | ||
|
||
abstract class Builder | ||
{ | ||
use Conditionable; | ||
|
||
/** | ||
* An array of columns to be selected. | ||
*/ | ||
public array $columns = []; | ||
|
||
/** | ||
* An array of filters. | ||
*/ | ||
public array $filters = []; | ||
|
||
/** | ||
* The limit for the query. | ||
*/ | ||
public int $limit = 500; | ||
|
||
/** | ||
* An array of columns to order by. | ||
* Each element should be an array with the column name and the direction. | ||
*/ | ||
public array $orderBy = []; | ||
|
||
/** | ||
* The offset for the query. | ||
*/ | ||
public int $offset = 0; | ||
|
||
public function select(array|string $columns = ['ID']): self | ||
{ | ||
$this->columns = Arr::wrap($columns); | ||
|
||
return $this; | ||
} | ||
|
||
public function addSelect(array|string $column): self | ||
{ | ||
$column = Arr::wrap($column); | ||
|
||
$this->columns = array_merge($this->columns, $column); | ||
|
||
return $this; | ||
} | ||
|
||
public function orderBy(string $column, string $direction = 'asc'): self | ||
{ | ||
$direction = Str::upper($direction); | ||
|
||
$this->orderBy[] = [$column, $direction]; | ||
|
||
return $this; | ||
} | ||
|
||
public function orderByDesc(string $column): self | ||
{ | ||
return $this->orderBy($column, 'desc'); | ||
} | ||
|
||
public function offset(int $value): self | ||
{ | ||
$this->offset = max(0, $value); | ||
|
||
return $this; | ||
} | ||
|
||
public function limit(int $value): self | ||
{ | ||
$this->limit = max(0, $value); | ||
|
||
return $this; | ||
} | ||
|
||
public function where(string $column, mixed $operator, mixed $value = null): self | ||
{ | ||
if (is_null($value)) { | ||
$value = $operator; | ||
$operator = '='; | ||
} | ||
|
||
$this->filters[] = [$column, $operator, $value]; | ||
|
||
return $this; | ||
} | ||
|
||
protected function getFilters(): array | ||
{ | ||
return collect($this->filters)->mapWithKeys(function ($value) { | ||
[$column, $operator, $value] = $value; | ||
|
||
return [ | ||
$column => [ | ||
'op' => $operator, | ||
'val' => $value, | ||
], | ||
]; | ||
})->toArray(); | ||
} | ||
|
||
protected function getOrderBy(): array | ||
{ | ||
return collect($this->orderBy)->mapWithKeys(function ($value) { | ||
[$column, $direction] = $value; | ||
|
||
return [ | ||
$column => $direction, | ||
]; | ||
})->toArray(); | ||
} | ||
|
||
abstract public function get(): Collection; | ||
|
||
abstract public function first(): array; | ||
|
||
abstract public function find(int $id): array; | ||
} |
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,88 @@ | ||
<?php | ||
|
||
namespace Katalam\OnOfficeAdapter\Query; | ||
|
||
use Illuminate\Support\Collection; | ||
use Katalam\OnOfficeAdapter\Enums\OnOfficeAction; | ||
use Katalam\OnOfficeAdapter\Enums\OnOfficeResourceType; | ||
use Katalam\OnOfficeAdapter\Exceptions\OnOfficeException; | ||
use Katalam\OnOfficeAdapter\Services\OnOfficeService; | ||
|
||
class EstateBuilder extends Builder | ||
{ | ||
public function __construct( | ||
private readonly OnOfficeService $onOfficeService, | ||
) { | ||
} | ||
|
||
public function get(): Collection | ||
{ | ||
$columns = $this->columns; | ||
$filter = $this->getFilters(); | ||
$listLimit = $this->limit; | ||
$listOffset = $this->offset; | ||
$orderBy = $this->getOrderBy(); | ||
|
||
return $this->onOfficeService->requestAll(/** | ||
* @throws OnOfficeException | ||
*/ function (int $pageSize, int $offset) use ($filter, $orderBy, $columns) { | ||
return $this->onOfficeService->requestApi( | ||
OnOfficeAction::Read, | ||
OnOfficeResourceType::Estate, | ||
parameters: [ | ||
OnOfficeService::DATA => $columns, | ||
OnOfficeService::FILTER => $filter, | ||
OnOfficeService::LISTLIMIT => $pageSize, | ||
OnOfficeService::LISTOFFSET => $offset, | ||
OnOfficeService::SORTBY => $orderBy, | ||
|
||
] | ||
); | ||
}, pageSize: $listLimit, offset: $listOffset); | ||
} | ||
|
||
/** | ||
* @throws OnOfficeException | ||
*/ | ||
public function first(): array | ||
{ | ||
$columns = $this->columns; | ||
$filter = $this->getFilters(); | ||
$listLimit = $this->limit; | ||
$listOffset = $this->offset; | ||
$orderBy = $this->getOrderBy(); | ||
|
||
$response = $this->onOfficeService->requestApi( | ||
OnOfficeAction::Read, | ||
OnOfficeResourceType::Estate, | ||
parameters: [ | ||
OnOfficeService::DATA => $columns, | ||
OnOfficeService::FILTER => $filter, | ||
OnOfficeService::LISTLIMIT => $listLimit, | ||
OnOfficeService::LISTOFFSET => $listOffset, | ||
OnOfficeService::SORTBY => $orderBy, | ||
] | ||
); | ||
|
||
return $response->json('response.results.0.data.records.0'); | ||
} | ||
|
||
/** | ||
* @throws OnOfficeException | ||
*/ | ||
public function find(int $id): array | ||
{ | ||
$columns = $this->columns; | ||
|
||
$response = $this->onOfficeService->requestApi( | ||
OnOfficeAction::Get, | ||
OnOfficeResourceType::Estate, | ||
$id, | ||
parameters: [ | ||
OnOfficeService::DATA => $columns, | ||
] | ||
); | ||
|
||
return $response->json('response.results.0.data.records.0'); | ||
} | ||
} |
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.