-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Create fields.md * Update QueryInterpreter.php
- Loading branch information
1 parent
c37cb3c
commit cd2cbe2
Showing
2 changed files
with
73 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# Fields | ||
|
||
|
||
Look at the `HasFields` trait for all available types (string, int etc). | ||
|
||
## Example | ||
|
||
``` | ||
class DamageBuilder extends QueryBuilder | ||
{ | ||
public function fields(): array | ||
{ | ||
return [ | ||
'id' => $this->uuid('uuid'), | ||
'name' => $this->string('name')->nullable(), | ||
'created_at' => $this->datetime('created_at'), | ||
'updated_at' => $this->datetime('updated_at'), | ||
]; | ||
} | ||
} | ||
``` | ||
|
||
## Custom types | ||
|
||
Extend the QueryBuilder to modify or add specific types. Here we want the string to return an empty string `""`, instead of a `null` (for backwards compatiblity) | ||
|
||
```php | ||
abstract class QueryBuilder extends \Apitizer\QueryBuilder | ||
{ | ||
protected function string(string $key): Field | ||
{ | ||
return $this->field($key, 'string')->transform(static function ($value) { | ||
// Always cast to string, even if null, because of backwards compatibility | ||
return (string) $value; | ||
}); | ||
} | ||
} | ||
``` | ||
|
||
|
||
## Callable fields | ||
|
||
|
||
```php | ||
use Apitizer\Types\GeneratedField; | ||
|
||
class InvoiceBuilder { | ||
public function fields(): array | ||
{ | ||
return [ | ||
'total' => $this->generatedField('int', function ($row, GeneratedField $field) { | ||
return 1; | ||
}); | ||
]; | ||
} | ||
} | ||
``` | ||
|
||
Any `callable` is accepted, so something like this would also suffice: | ||
```php | ||
use Apitizer\Types\GeneratedField; | ||
|
||
class InvoiceBuilder { | ||
public function fields(): array | ||
{ | ||
return [ | ||
'total' => $this->generatedField('int', new CalculateOrderTotal), | ||
]; | ||
} | ||
} | ||
``` |
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