2013-04-12 17:05:13 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the NelmioApiDocBundle.
|
|
|
|
*
|
|
|
|
* (c) Nelmio <hello@nelm.io>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Nelmio\ApiDocBundle\Extractor\Handler;
|
|
|
|
|
Unified data types [actualType and subType]
This is the result of https://github.com/nelmio/NelmioApiDocBundle/issues/410.
This PR aims to provide a uniform way of declaring data-types of parameters for
parsers and handlers to follow. In turn, this would allow formatters to
determine data-types in a cleaner and less volatile manner. (See use-case that
can be improved with this PR:
https://github.com/nelmio/NelmioApiDocBundle/blob/master/Formatter/AbstractFormatter.php#L103)
This is possible by the addition two properties to each property item in
`response`, and `parameters` fields in each API endpoint produced by the
`ApiDocExtractor`:
* `actualType` Contains a value from one of the `DataTypes` class constants.
* `subType` Can contain either `null`, or any other `DataTypes` class constant.
This is relevant when the `actualType` is a `DataTypes::COLLECTION`, wherein
`subType` would specify the type of the collection items. It is also relevant
when `actualType` is a `DataTypes::MODEL`, wherein `subType` would contain an
identifier of the model (the FQCN or anything the parser would wish to specify)
Examples:
```php
array(
'id' => array(
'dataType' => 'integer',
'actualType' => DataTypes::INTEGER,
'subType' => null,
),
'profile' => array(
'dataType' => 'object (Profile)',
'actualType' => DataTypes::MODEL,
'subType' => 'Foo\Entity\Profile',
'children' => array(
'name' => array(
'dataType' => 'string',
'actualType' => DataTypes::STRING,
'subType' => null,
),
'birthDate' => array(
'dataType' => 'date',
'actualType' => DataTypes::DATE,
'subType' => null,
),
)
),
'languages' => array(
'dataType' => 'array of strings',
'actualType' => DataTypes::COLLECTION,
'subType' => DataTypes::STRING,
),
'roles' => array(
'dataType' => 'array of choices',
'actualType' => DataTypes::COLLECTION,
'subType' => DataTypes::ENUM,
),
'groups' => array(
'dataType' => 'array of objects (Group)',
'actualType' => DataTypes::COLLECTION,
'subType' => 'Foo\Entity\Group',
),
'profileRevisions' => array(
'dataType' => 'array of objects (Profile)',
'actualType' => DataTypes::COLLECTION,
'subType' => 'Foo\Entity\Profile',
),
'address' => array(
'dataType' => 'object (a_type_a_custom_JMS_serializer_handler_handles)',
'actualType' => DataTypes::MODEL,
'subType' => 'a_type_a_custom_JMS_serializer_handler_handles',
),
);
```
When a formatter omits the `dataType` property or leaves it blank, it is
inferred within `ApiDocExtractor` before everything is passed to formatters.
2014-06-17 17:05:00 -07:00
|
|
|
use Nelmio\ApiDocBundle\DataTypes;
|
2013-04-12 17:05:13 +02:00
|
|
|
use Nelmio\ApiDocBundle\Extractor\HandlerInterface;
|
2013-04-16 13:46:15 +02:00
|
|
|
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
|
|
|
|
use Symfony\Component\Routing\Route;
|
2014-04-17 14:06:41 +02:00
|
|
|
use Symfony\Component\Validator\Constraint;
|
|
|
|
use Symfony\Component\Validator\Constraints\Regex;
|
2013-04-17 13:41:28 +02:00
|
|
|
use FOS\RestBundle\Controller\Annotations\RequestParam;
|
|
|
|
use FOS\RestBundle\Controller\Annotations\QueryParam;
|
2013-04-12 17:05:13 +02:00
|
|
|
|
2013-04-16 13:46:15 +02:00
|
|
|
class FosRestHandler implements HandlerInterface
|
2013-04-12 17:05:13 +02:00
|
|
|
{
|
2014-04-17 14:06:41 +02:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
2013-04-17 14:24:45 +02:00
|
|
|
public function handle(ApiDoc $annotation, array $annotations, Route $route, \ReflectionMethod $method)
|
2013-04-12 17:05:13 +02:00
|
|
|
{
|
|
|
|
foreach ($annotations as $annot) {
|
2013-04-17 13:41:28 +02:00
|
|
|
if ($annot instanceof RequestParam) {
|
Unified data types [actualType and subType]
This is the result of https://github.com/nelmio/NelmioApiDocBundle/issues/410.
This PR aims to provide a uniform way of declaring data-types of parameters for
parsers and handlers to follow. In turn, this would allow formatters to
determine data-types in a cleaner and less volatile manner. (See use-case that
can be improved with this PR:
https://github.com/nelmio/NelmioApiDocBundle/blob/master/Formatter/AbstractFormatter.php#L103)
This is possible by the addition two properties to each property item in
`response`, and `parameters` fields in each API endpoint produced by the
`ApiDocExtractor`:
* `actualType` Contains a value from one of the `DataTypes` class constants.
* `subType` Can contain either `null`, or any other `DataTypes` class constant.
This is relevant when the `actualType` is a `DataTypes::COLLECTION`, wherein
`subType` would specify the type of the collection items. It is also relevant
when `actualType` is a `DataTypes::MODEL`, wherein `subType` would contain an
identifier of the model (the FQCN or anything the parser would wish to specify)
Examples:
```php
array(
'id' => array(
'dataType' => 'integer',
'actualType' => DataTypes::INTEGER,
'subType' => null,
),
'profile' => array(
'dataType' => 'object (Profile)',
'actualType' => DataTypes::MODEL,
'subType' => 'Foo\Entity\Profile',
'children' => array(
'name' => array(
'dataType' => 'string',
'actualType' => DataTypes::STRING,
'subType' => null,
),
'birthDate' => array(
'dataType' => 'date',
'actualType' => DataTypes::DATE,
'subType' => null,
),
)
),
'languages' => array(
'dataType' => 'array of strings',
'actualType' => DataTypes::COLLECTION,
'subType' => DataTypes::STRING,
),
'roles' => array(
'dataType' => 'array of choices',
'actualType' => DataTypes::COLLECTION,
'subType' => DataTypes::ENUM,
),
'groups' => array(
'dataType' => 'array of objects (Group)',
'actualType' => DataTypes::COLLECTION,
'subType' => 'Foo\Entity\Group',
),
'profileRevisions' => array(
'dataType' => 'array of objects (Profile)',
'actualType' => DataTypes::COLLECTION,
'subType' => 'Foo\Entity\Profile',
),
'address' => array(
'dataType' => 'object (a_type_a_custom_JMS_serializer_handler_handles)',
'actualType' => DataTypes::MODEL,
'subType' => 'a_type_a_custom_JMS_serializer_handler_handles',
),
);
```
When a formatter omits the `dataType` property or leaves it blank, it is
inferred within `ApiDocExtractor` before everything is passed to formatters.
2014-06-17 17:05:00 -07:00
|
|
|
|
|
|
|
$requirements = $this->handleRequirements($annot->requirements);
|
2014-06-26 12:27:47 -07:00
|
|
|
$data = array(
|
2014-06-18 11:50:26 +02:00
|
|
|
'required' => $annot->strict && $annot->nullable === false && $annot->default === null,
|
Unified data types [actualType and subType]
This is the result of https://github.com/nelmio/NelmioApiDocBundle/issues/410.
This PR aims to provide a uniform way of declaring data-types of parameters for
parsers and handlers to follow. In turn, this would allow formatters to
determine data-types in a cleaner and less volatile manner. (See use-case that
can be improved with this PR:
https://github.com/nelmio/NelmioApiDocBundle/blob/master/Formatter/AbstractFormatter.php#L103)
This is possible by the addition two properties to each property item in
`response`, and `parameters` fields in each API endpoint produced by the
`ApiDocExtractor`:
* `actualType` Contains a value from one of the `DataTypes` class constants.
* `subType` Can contain either `null`, or any other `DataTypes` class constant.
This is relevant when the `actualType` is a `DataTypes::COLLECTION`, wherein
`subType` would specify the type of the collection items. It is also relevant
when `actualType` is a `DataTypes::MODEL`, wherein `subType` would contain an
identifier of the model (the FQCN or anything the parser would wish to specify)
Examples:
```php
array(
'id' => array(
'dataType' => 'integer',
'actualType' => DataTypes::INTEGER,
'subType' => null,
),
'profile' => array(
'dataType' => 'object (Profile)',
'actualType' => DataTypes::MODEL,
'subType' => 'Foo\Entity\Profile',
'children' => array(
'name' => array(
'dataType' => 'string',
'actualType' => DataTypes::STRING,
'subType' => null,
),
'birthDate' => array(
'dataType' => 'date',
'actualType' => DataTypes::DATE,
'subType' => null,
),
)
),
'languages' => array(
'dataType' => 'array of strings',
'actualType' => DataTypes::COLLECTION,
'subType' => DataTypes::STRING,
),
'roles' => array(
'dataType' => 'array of choices',
'actualType' => DataTypes::COLLECTION,
'subType' => DataTypes::ENUM,
),
'groups' => array(
'dataType' => 'array of objects (Group)',
'actualType' => DataTypes::COLLECTION,
'subType' => 'Foo\Entity\Group',
),
'profileRevisions' => array(
'dataType' => 'array of objects (Profile)',
'actualType' => DataTypes::COLLECTION,
'subType' => 'Foo\Entity\Profile',
),
'address' => array(
'dataType' => 'object (a_type_a_custom_JMS_serializer_handler_handles)',
'actualType' => DataTypes::MODEL,
'subType' => 'a_type_a_custom_JMS_serializer_handler_handles',
),
);
```
When a formatter omits the `dataType` property or leaves it blank, it is
inferred within `ApiDocExtractor` before everything is passed to formatters.
2014-06-17 17:05:00 -07:00
|
|
|
'dataType' => $requirements,
|
|
|
|
'actualType' => $this->inferType($requirements),
|
|
|
|
'subType' => null,
|
2013-04-16 13:46:15 +02:00
|
|
|
'description' => $annot->description,
|
|
|
|
'readonly' => false
|
2014-06-26 12:27:47 -07:00
|
|
|
);
|
|
|
|
if ($annot->strict === false) {
|
|
|
|
$data['default'] = $annot->default;
|
|
|
|
}
|
|
|
|
$annotation->addParameter($annot->name, $data);
|
2013-04-17 13:41:28 +02:00
|
|
|
} elseif ($annot instanceof QueryParam) {
|
2013-08-18 22:33:04 +02:00
|
|
|
if ($annot->strict && $annot->nullable === false && $annot->default === null) {
|
2013-04-12 17:05:13 +02:00
|
|
|
$annotation->addRequirement($annot->name, array(
|
2014-04-17 14:06:41 +02:00
|
|
|
'requirement' => $this->handleRequirements($annot->requirements),
|
2013-04-12 17:05:13 +02:00
|
|
|
'dataType' => '',
|
|
|
|
'description' => $annot->description,
|
|
|
|
));
|
2013-11-14 10:28:42 +01:00
|
|
|
} elseif ($annot->default !== null) {
|
2013-04-30 16:21:12 +04:00
|
|
|
$annotation->addFilter($annot->name, array(
|
2014-04-17 14:06:41 +02:00
|
|
|
'requirement' => $this->handleRequirements($annot->requirements),
|
2013-04-30 16:21:12 +04:00
|
|
|
'description' => $annot->description,
|
|
|
|
'default' => $annot->default,
|
|
|
|
));
|
2013-04-12 17:05:13 +02:00
|
|
|
} else {
|
|
|
|
$annotation->addFilter($annot->name, array(
|
2014-04-17 14:06:41 +02:00
|
|
|
'requirement' => $this->handleRequirements($annot->requirements),
|
2013-04-12 17:05:13 +02:00
|
|
|
'description' => $annot->description,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-06-25 08:46:01 +02:00
|
|
|
|
2014-04-17 14:06:41 +02:00
|
|
|
/**
|
|
|
|
* Handle FOSRestBundle requirements in order to return a string.
|
|
|
|
*
|
2014-06-25 08:46:01 +02:00
|
|
|
* @param mixed $requirements
|
2014-04-17 14:06:41 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function handleRequirements($requirements)
|
|
|
|
{
|
|
|
|
if (is_object($requirements) && $requirements instanceof Constraint) {
|
|
|
|
if ($requirements instanceof Regex) {
|
|
|
|
return $requirements->getHtmlPattern();
|
|
|
|
}
|
|
|
|
$class = get_class($requirements);
|
2014-06-25 08:46:01 +02:00
|
|
|
|
2014-04-17 14:06:41 +02:00
|
|
|
return substr($class, strrpos($class, '\\')+1);
|
|
|
|
}
|
2014-06-25 08:46:01 +02:00
|
|
|
|
|
|
|
return (string) $requirements;
|
2014-04-17 14:06:41 +02:00
|
|
|
}
|
Unified data types [actualType and subType]
This is the result of https://github.com/nelmio/NelmioApiDocBundle/issues/410.
This PR aims to provide a uniform way of declaring data-types of parameters for
parsers and handlers to follow. In turn, this would allow formatters to
determine data-types in a cleaner and less volatile manner. (See use-case that
can be improved with this PR:
https://github.com/nelmio/NelmioApiDocBundle/blob/master/Formatter/AbstractFormatter.php#L103)
This is possible by the addition two properties to each property item in
`response`, and `parameters` fields in each API endpoint produced by the
`ApiDocExtractor`:
* `actualType` Contains a value from one of the `DataTypes` class constants.
* `subType` Can contain either `null`, or any other `DataTypes` class constant.
This is relevant when the `actualType` is a `DataTypes::COLLECTION`, wherein
`subType` would specify the type of the collection items. It is also relevant
when `actualType` is a `DataTypes::MODEL`, wherein `subType` would contain an
identifier of the model (the FQCN or anything the parser would wish to specify)
Examples:
```php
array(
'id' => array(
'dataType' => 'integer',
'actualType' => DataTypes::INTEGER,
'subType' => null,
),
'profile' => array(
'dataType' => 'object (Profile)',
'actualType' => DataTypes::MODEL,
'subType' => 'Foo\Entity\Profile',
'children' => array(
'name' => array(
'dataType' => 'string',
'actualType' => DataTypes::STRING,
'subType' => null,
),
'birthDate' => array(
'dataType' => 'date',
'actualType' => DataTypes::DATE,
'subType' => null,
),
)
),
'languages' => array(
'dataType' => 'array of strings',
'actualType' => DataTypes::COLLECTION,
'subType' => DataTypes::STRING,
),
'roles' => array(
'dataType' => 'array of choices',
'actualType' => DataTypes::COLLECTION,
'subType' => DataTypes::ENUM,
),
'groups' => array(
'dataType' => 'array of objects (Group)',
'actualType' => DataTypes::COLLECTION,
'subType' => 'Foo\Entity\Group',
),
'profileRevisions' => array(
'dataType' => 'array of objects (Profile)',
'actualType' => DataTypes::COLLECTION,
'subType' => 'Foo\Entity\Profile',
),
'address' => array(
'dataType' => 'object (a_type_a_custom_JMS_serializer_handler_handles)',
'actualType' => DataTypes::MODEL,
'subType' => 'a_type_a_custom_JMS_serializer_handler_handles',
),
);
```
When a formatter omits the `dataType` property or leaves it blank, it is
inferred within `ApiDocExtractor` before everything is passed to formatters.
2014-06-17 17:05:00 -07:00
|
|
|
|
|
|
|
public function inferType($requirement)
|
|
|
|
{
|
|
|
|
if (DataTypes::isPrimitive($requirement)) {
|
|
|
|
return $requirement;
|
|
|
|
}
|
|
|
|
|
|
|
|
return DataTypes::STRING;
|
|
|
|
}
|
2013-04-12 17:05:13 +02:00
|
|
|
}
|