|
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\Validator\Validator\ValidatorInterface; |
|
11
|
|
|
|
|
12
|
|
|
class RouteFactory extends AbstractFactory |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var SlugifyInterface |
|
16
|
|
|
*/ |
|
17
|
|
|
private $slugify; |
|
18
|
|
|
|
|
19
|
5 |
|
public function __construct( |
|
20
|
|
|
ObjectManager $manager, |
|
21
|
|
|
ValidatorInterface $validator, |
|
22
|
|
|
SlugifyInterface $slugify |
|
23
|
|
|
) |
|
24
|
|
|
{ |
|
25
|
5 |
|
$this->slugify = $slugify; |
|
26
|
5 |
|
parent::__construct($manager, $validator); |
|
27
|
5 |
|
} |
|
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 |
|
'route' => null, |
|
47
|
|
|
'content' => null, |
|
48
|
|
|
'redirect' => null |
|
49
|
|
|
]; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param RouteAwareInterface $entity |
|
54
|
|
|
* @return Route |
|
55
|
|
|
*/ |
|
56
|
2 |
|
public function createFromRouteAwareEntity(RouteAwareInterface $entity): Route |
|
57
|
|
|
{ |
|
58
|
2 |
|
$pageRoute = $this->slugify->slugify($entity->getDefaultRoute()); |
|
59
|
2 |
|
$routePrefix = $this->getRoutePrefix($entity); |
|
60
|
2 |
|
return $this->create( |
|
61
|
|
|
[ |
|
62
|
2 |
|
'route' => $routePrefix . $pageRoute, |
|
63
|
2 |
|
'content' => $entity |
|
64
|
|
|
] |
|
65
|
|
|
); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param RouteAwareInterface $entity |
|
70
|
|
|
* @return string |
|
71
|
|
|
*/ |
|
72
|
2 |
|
private function getRoutePrefix(RouteAwareInterface $entity): string |
|
73
|
|
|
{ |
|
74
|
2 |
|
$parent = method_exists($entity, 'getParent') ? $entity->getParent() : null; |
|
75
|
2 |
|
if ($parent && $parent instanceof RouteAwareInterface) { |
|
76
|
1 |
|
$parentRoute = $this->getParentRoute($parent); |
|
77
|
1 |
|
return $parentRoute->getRoute() . '/'; |
|
78
|
|
|
} |
|
79
|
2 |
|
return '/'; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param RouteAwareInterface $parent |
|
84
|
|
|
* @return mixed|Route |
|
85
|
|
|
*/ |
|
86
|
1 |
|
private function getParentRoute(RouteAwareInterface $parent) |
|
87
|
|
|
{ |
|
88
|
1 |
|
$parentRoute = $parent->getRoutes()->first(); |
|
89
|
1 |
|
if (!$parentRoute) { |
|
90
|
1 |
|
$parentRoute = $this->createFromRouteAwareEntity($parent); |
|
91
|
|
|
} |
|
92
|
1 |
|
return $parentRoute; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|