Skip to content

Commit

Permalink
Merge pull request #7 from justbetter/feature/find-with-casts
Browse files Browse the repository at this point in the history
Implemented date cast when using the find method
  • Loading branch information
VincentBean authored Dec 6, 2022
2 parents d023045 + 005a9fe commit 82f63b7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
],
"require": {
"php": "^8.0",
"justbetter/odata-client": "^1.0",
"justbetter/odata-client": "^1.1",
"laravel/framework": "^9.0"
},
"require-dev": {
Expand Down
7 changes: 6 additions & 1 deletion src/Concerns/HasCasts.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@ trait HasCasts
{
public array $casts = [];

public function getCastType(string $key): ?string
{
return $this->casts[$key] ?? null;
}

public function cast(string $key, mixed $value): string
{
return match ($this->casts[$key] ?? null) {
return match ($this->getCastType($key)) {
'int', 'date', 'decimal' => (string) $value,
default => '\''.$value.'\'',
};
Expand Down
12 changes: 11 additions & 1 deletion src/Query/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,18 @@ public function find(mixed ...$values): ?BaseResource
/** @var BaseResource $baseResource */
$baseResource = app($class);

$combined = array_combine($baseResource->primaryKey, $values);

foreach ($combined as $key => $value) {
if ($baseResource->getCastType($key) === 'date') {
$this->builder->whereDate($key, '=', $value);
} else {
$this->builder->where($key, '=', $value);
}
}

/** @var ?Entity $entity */
$entity = $this->builder->where(array_combine($baseResource->primaryKey, $values))->first();
$entity = $this->builder->first();

return is_null($entity)
? null
Expand Down

0 comments on commit 82f63b7

Please sign in to comment.