Allow fields to be made only visible for single root resources
This commit is contained in:
parent
8ae0682d3a
commit
606a79a0cf
|
|
@ -35,7 +35,7 @@ class Show implements RequestHandlerInterface
|
||||||
|
|
||||||
$serializer = new Serializer($this->api, $request);
|
$serializer = new Serializer($this->api, $request);
|
||||||
|
|
||||||
$serializer->add($this->resource, $this->model, $include);
|
$serializer->add($this->resource, $this->model, $include, true);
|
||||||
|
|
||||||
return new JsonApiResponse(
|
return new JsonApiResponse(
|
||||||
new CompoundDocument(
|
new CompoundDocument(
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,7 @@ abstract class Field
|
||||||
private $name;
|
private $name;
|
||||||
private $property;
|
private $property;
|
||||||
private $visible = true;
|
private $visible = true;
|
||||||
|
private $single = false;
|
||||||
private $writable = false;
|
private $writable = false;
|
||||||
private $getter;
|
private $getter;
|
||||||
private $setter;
|
private $setter;
|
||||||
|
|
@ -49,6 +50,18 @@ abstract class Field
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Indicates that the field should only be visible on single root resources
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function single()
|
||||||
|
{
|
||||||
|
$this->single = true;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
public function writable(Closure $condition = null)
|
public function writable(Closure $condition = null)
|
||||||
{
|
{
|
||||||
$this->writable = $condition ?: true;
|
$this->writable = $condition ?: true;
|
||||||
|
|
@ -137,6 +150,11 @@ abstract class Field
|
||||||
return $this->visible;
|
return $this->visible;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getSingle()
|
||||||
|
{
|
||||||
|
return $this->single;
|
||||||
|
}
|
||||||
|
|
||||||
public function getWritable()
|
public function getWritable()
|
||||||
{
|
{
|
||||||
return $this->writable;
|
return $this->writable;
|
||||||
|
|
|
||||||
|
|
@ -30,14 +30,14 @@ final class Serializer
|
||||||
$this->request = $request;
|
$this->request = $request;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function add(ResourceType $resource, $model, array $include)
|
public function add(ResourceType $resource, $model, array $include, bool $single = false)
|
||||||
{
|
{
|
||||||
$data = $this->addToMap($resource, $model, $include);
|
$data = $this->addToMap($resource, $model, $include, $single);
|
||||||
|
|
||||||
$this->primary[] = $data['type'].':'.$data['id'];
|
$this->primary[] = $data['type'].':'.$data['id'];
|
||||||
}
|
}
|
||||||
|
|
||||||
private function addToMap(ResourceType $resource, $model, array $include)
|
private function addToMap(ResourceType $resource, $model, array $include, bool $single = false)
|
||||||
{
|
{
|
||||||
$adapter = $resource->getAdapter();
|
$adapter = $resource->getAdapter();
|
||||||
$schema = $resource->getSchema();
|
$schema = $resource->getSchema();
|
||||||
|
|
@ -69,6 +69,10 @@ final class Serializer
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($field->getSingle() && ! $single) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if (! evaluate($field->getVisible(), [$model, $this->request])) {
|
if (! evaluate($field->getVisible(), [$model, $this->request])) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue