Allow adding inline documentation

This commit is contained in:
Toby Zerner 2020-05-09 17:55:09 +09:30
parent c6ef714eb4
commit 2570c24390
4 changed files with 66 additions and 3 deletions

View File

@ -246,7 +246,7 @@ class Index implements RequestHandlerInterface
}
if (isset($filters[$name])) {
$filters[$name]($query, $value, $request);
$filters[$name]->getCallback()($query, $value, $request);
continue;
}

View File

@ -20,6 +20,7 @@ abstract class Field
use HasListeners;
private $name;
private $description;
private $property;
private $visible = true;
private $single = false;
@ -41,6 +42,16 @@ abstract class Field
*/
abstract public function getLocation(): string;
/**
* Set the description of the field for documentation generation.
*/
public function description(string $description)
{
$this->description = $description;
return $this;
}
/**
* Set the model property to which this field corresponds.
*/

43
src/Schema/Filter.php Normal file
View File

@ -0,0 +1,43 @@
<?php
/*
* This file is part of tobyz/json-api-server.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tobyz\JsonApiServer\Schema;
final class Filter
{
private $name;
private $callback;
private $description;
public function __construct(string $name, callable $callback)
{
$this->name = $name;
$this->callback = $callback;
}
public function getName(): string
{
return $this->name;
}
public function getCallback(): callable
{
return $this->callback;
}
/**
* Set the description of the type for documentation generation.
*/
public function description(string $description)
{
$this->description = $description;
}
}

View File

@ -19,6 +19,7 @@ final class Type
{
use HasListeners, HasMeta;
private $description;
private $fields = [];
private $filters = [];
private $sortFields = [];
@ -35,6 +36,14 @@ final class Type
private $deletable = false;
private $deleteCallback;
/**
* Set the description of the type for documentation generation.
*/
public function description(string $description)
{
$this->description = $description;
}
/**
* Add an attribute to the resource type.
*
@ -98,9 +107,9 @@ final class Type
/**
* Add a filter to the resource type.
*/
public function filter(string $name, callable $callback): void
public function filter(string $name, callable $callback): Filter
{
$this->filters[$name] = $callback;
return $this->filters[$name] = new Filter($name, $callback);
}
/**