diff --git a/src/Handler/Show.php b/src/Handler/Show.php index 2dbcebd..4c55962 100644 --- a/src/Handler/Show.php +++ b/src/Handler/Show.php @@ -35,7 +35,7 @@ class Show implements RequestHandlerInterface $serializer = new Serializer($this->api, $request); - $serializer->add($this->resource, $this->model, $include); + $serializer->add($this->resource, $this->model, $include, true); return new JsonApiResponse( new CompoundDocument( diff --git a/src/Schema/Field.php b/src/Schema/Field.php index 08ee5b3..d1e885f 100644 --- a/src/Schema/Field.php +++ b/src/Schema/Field.php @@ -14,6 +14,7 @@ abstract class Field private $name; private $property; private $visible = true; + private $single = false; private $writable = false; private $getter; private $setter; @@ -49,6 +50,18 @@ abstract class Field 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) { $this->writable = $condition ?: true; @@ -137,6 +150,11 @@ abstract class Field return $this->visible; } + public function getSingle() + { + return $this->single; + } + public function getWritable() { return $this->writable; diff --git a/src/Serializer.php b/src/Serializer.php index d2cc350..7190cd5 100644 --- a/src/Serializer.php +++ b/src/Serializer.php @@ -30,14 +30,14 @@ final class Serializer $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']; } - private function addToMap(ResourceType $resource, $model, array $include) + private function addToMap(ResourceType $resource, $model, array $include, bool $single = false) { $adapter = $resource->getAdapter(); $schema = $resource->getSchema(); @@ -69,6 +69,10 @@ final class Serializer continue; } + if ($field->getSingle() && ! $single) { + continue; + } + if (! evaluate($field->getVisible(), [$model, $this->request])) { continue; }