|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Silverback\ApiComponentBundle\Factory\Entity\Route; |
|
4
|
|
|
|
|
5
|
|
|
use Cocur\Slugify\SlugifyInterface; |
|
6
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
|
7
|
|
|
use Silverback\ApiComponentBundle\Entity\Route\Route; |
|
8
|
|
|
use Silverback\ApiComponentBundle\Entity\Route\RouteAwareInterface; |
|
9
|
|
|
use Silverback\ApiComponentBundle\Factory\Entity\AbstractFactory; |
|
10
|
|
|
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter; |
|
11
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
|
12
|
|
|
|
|
13
|
|
|
class RouteFactory extends AbstractFactory |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var SlugifyInterface |
|
17
|
|
|
*/ |
|
18
|
|
|
private $slugify; |
|
19
|
|
|
|
|
20
|
7 |
|
public function __construct( |
|
21
|
|
|
ObjectManager $manager, |
|
22
|
|
|
ValidatorInterface $validator, |
|
23
|
|
|
SlugifyInterface $slugify |
|
24
|
|
|
) { |
|
25
|
7 |
|
$this->slugify = $slugify; |
|
26
|
7 |
|
parent::__construct($manager, $validator); |
|
27
|
7 |
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @inheritdoc |
|
31
|
|
|
*/ |
|
32
|
4 |
|
public function create(?array $ops = null): Route |
|
33
|
|
|
{ |
|
34
|
4 |
|
$component = new Route(); |
|
35
|
4 |
|
$this->init($component, $ops); |
|
36
|
4 |
|
$this->validate($component); |
|
37
|
4 |
|
return $component; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @inheritdoc |
|
42
|
|
|
*/ |
|
43
|
5 |
|
protected static function defaultOps(): array |
|
44
|
|
|
{ |
|
45
|
|
|
return [ |
|
46
|
5 |
|
'name' => null, |
|
47
|
|
|
'route' => null, |
|
48
|
|
|
'content' => null, |
|
49
|
|
|
'redirect' => null |
|
50
|
|
|
]; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param RouteAwareInterface $entity |
|
55
|
|
|
* @param int|null $postfix |
|
56
|
|
|
* @return Route |
|
57
|
|
|
*/ |
|
58
|
3 |
|
public function createFromRouteAwareEntity(RouteAwareInterface $entity, int $postfix = 0): Route |
|
59
|
|
|
{ |
|
60
|
3 |
|
$pageRoute = $this->slugify->slugify($entity->getDefaultRoute() ?: ''); |
|
61
|
3 |
|
$routePrefix = $this->getRoutePrefix($entity); |
|
62
|
3 |
|
$fullRoute = $routePrefix . $pageRoute; |
|
63
|
3 |
|
if ($postfix > 0) { |
|
64
|
|
|
$fullRoute .= '-' . $postfix; |
|
65
|
|
|
} |
|
66
|
3 |
|
$existing = $this->manager->getRepository(Route::class)->find($fullRoute); |
|
67
|
3 |
|
if ($existing) { |
|
68
|
|
|
return $this->createFromRouteAwareEntity($entity, $postfix + 1); |
|
69
|
|
|
} |
|
70
|
3 |
|
$converter = new CamelCaseToSnakeCaseNameConverter(); |
|
71
|
3 |
|
$route = $this->create( |
|
72
|
|
|
[ |
|
73
|
3 |
|
'name' => $converter->normalize(str_replace(' ', '', $entity->getDefaultRouteName())), |
|
74
|
3 |
|
'route' => $fullRoute, |
|
75
|
3 |
|
'content' => $entity |
|
76
|
|
|
] |
|
77
|
|
|
); |
|
78
|
3 |
|
$entity->addRoute($route); |
|
79
|
3 |
|
return $route; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param RouteAwareInterface $entity |
|
84
|
|
|
* @return string |
|
85
|
|
|
*/ |
|
86
|
3 |
|
private function getRoutePrefix(RouteAwareInterface $entity): string |
|
87
|
|
|
{ |
|
88
|
3 |
|
$parent = $entity->getParentRoute(); |
|
89
|
3 |
|
if ($parent) { |
|
90
|
2 |
|
return $parent->getRoute() . '/'; |
|
91
|
|
|
} |
|
92
|
2 |
|
return '/'; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|