/
DictionaryEntityApiDocHandler.php
65 lines (55 loc) · 2.28 KB
/
DictionaryEntityApiDocHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
namespace Oro\Bundle\EntityBundle\Routing;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
use Nelmio\ApiDocBundle\Extractor\HandlerInterface;
use Oro\Bundle\EntityBundle\ORM\EntityAliasResolver;
use Oro\Bundle\EntityBundle\Provider\EntityClassNameProviderInterface;
use Symfony\Component\Routing\Route;
class DictionaryEntityApiDocHandler implements HandlerInterface
{
const DESCRIPTION_TEMPLATE = 'Get {plural_name}';
const DOCUMENTATION_TEMPLATE = 'Get {plural_name}';
const FALLBACK_DESCRIPTION_TEMPLATE = 'Get values of {class}';
const FALLBACK_DOCUMENTATION_TEMPLATE = 'Get values of {class}';
/** @var EntityAliasResolver */
protected $entityAliasResolver;
/** @var EntityClassNameProviderInterface */
protected $entityClassNameProvider;
public function __construct(
EntityAliasResolver $entityAliasResolver,
EntityClassNameProviderInterface $entityClassNameProvider
) {
$this->entityAliasResolver = $entityAliasResolver;
$this->entityClassNameProvider = $entityClassNameProvider;
}
/**
* {@inheritdoc}
*/
public function handle(ApiDoc $annotation, array $annotations, Route $route, \ReflectionMethod $method)
{
if ($route->getOption('group') !== DictionaryEntityRouteOptionsResolver::ROUTE_GROUP) {
return;
}
$pluralAlias = $route->getDefault(DictionaryEntityRouteOptionsResolver::ENTITY_ATTRIBUTE);
if (!$pluralAlias) {
return;
}
$className = $this->entityAliasResolver->getClassByPluralAlias($pluralAlias);
$pluralName = $this->entityClassNameProvider->getEntityClassPluralName($className);
if ($pluralName) {
$annotation->setDescription(
strtr(static::DESCRIPTION_TEMPLATE, ['{plural_name}' => $pluralName])
);
$annotation->setDocumentation(
strtr(static::DOCUMENTATION_TEMPLATE, ['{plural_name}' => $pluralName])
);
} else {
$annotation->setDescription(
strtr(static::FALLBACK_DESCRIPTION_TEMPLATE, ['{class}' => $className])
);
$annotation->setDocumentation(
strtr(static::FALLBACK_DOCUMENTATION_TEMPLATE, ['{class}' => $className])
);
}
}
}