Files
animethemes-server/app/Http/Api/Field/Base/IdField.php
T
paranarimasuandGitHub 27778857b1 Request action composition (#516)
* refactor(api): prefer composition to strict inheritance for API queries, requests & actions

* style: fix StyleCI findigns
2023-02-16 23:30:57 -06:00

49 lines
1.2 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Http\Api\Field\Base;
use App\Http\Api\Field\IntField;
use App\Http\Api\Query\Query;
use App\Http\Api\Schema\Schema;
use App\Http\Resources\BaseResource;
/**
* Class IdField.
*/
class IdField extends IntField
{
/**
* Create a new field instance.
*
* @param Schema $schema
* @param string $column
*/
public function __construct(Schema $schema, string $column)
{
parent::__construct($schema, BaseResource::ATTRIBUTE_ID, $column);
}
/**
* Determine if the field should be included in the select clause of our query.
*
* @param Query $query
* @param Schema $schema
* @return bool
*/
public function shouldSelect(Query $query, Schema $schema): bool
{
// We can only exclude ID fields for top-level models that are not including related resources.
$includeCriteria = $query->getIncludeCriteria($this->schema->type());
if (
$this->schema->type() === $schema->type()
&& ($includeCriteria === null || $includeCriteria->getPaths()->isEmpty())
) {
return parent::shouldSelect($query, $schema);
}
return true;
}
}