Files
animethemes-server/app/Http/Api/Field/StringField.php
T

64 lines
1.7 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Http\Api\Field;
use App\Contracts\Http\Api\Field\FilterableField;
use App\Contracts\Http\Api\Field\RenderableField;
use App\Contracts\Http\Api\Field\SelectableField;
use App\Contracts\Http\Api\Field\SortableField;
use App\Http\Api\Filter\Filter;
use App\Http\Api\Filter\StringFilter;
use App\Http\Api\Query\Query;
use App\Http\Api\Schema\Schema;
use App\Http\Api\Sort\Sort;
use Illuminate\Database\Eloquent\Model;
abstract class StringField extends Field implements FilterableField, RenderableField, SelectableField, SortableField
{
/**
* Get the filter that can be applied to the field.
*/
public function getFilter(): Filter
{
return new StringFilter($this->getKey(), $this->getColumn());
}
/**
* Determine if the field should be displayed to the user.
*/
public function shouldRender(Query $query): bool
{
$criteria = $query->getFieldCriteria($this->schema->type());
return $criteria === null || $criteria->isAllowedField($this->getKey());
}
/**
* Get the value to display to the user.
*/
public function render(Model $model): mixed
{
return $model->getAttribute($this->getColumn());
}
/**
* Determine if the field should be included in the select clause of our query.
*/
public function shouldSelect(Query $query, Schema $schema): bool
{
$criteria = $query->getFieldCriteria($this->schema->type());
return $criteria === null || $criteria->isAllowedField($this->getKey());
}
/**
* Get the sort that can be applied to the field.
*/
public function getSort(): Sort
{
return new Sort($this->getKey(), $this->getColumn());
}
}