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
|
3 |
|
public function __construct( |
20
|
|
|
ObjectManager $manager, |
21
|
|
|
ValidatorInterface $validator, |
22
|
|
|
SlugifyInterface $slugify |
23
|
|
|
) |
24
|
|
|
{ |
25
|
3 |
|
$this->slugify = $slugify; |
26
|
3 |
|
parent::__construct($manager, $validator); |
27
|
3 |
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @inheritdoc |
31
|
|
|
*/ |
32
|
2 |
|
public function create(?array $ops = null): Route |
33
|
|
|
{ |
34
|
2 |
|
$component = new Route(); |
35
|
2 |
|
$this->init($component, $ops); |
36
|
2 |
|
$this->validate($component); |
37
|
2 |
|
return $component; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @inheritdoc |
42
|
|
|
*/ |
43
|
3 |
|
protected static function defaultOps(): array |
44
|
|
|
{ |
45
|
|
|
return [ |
46
|
3 |
|
'route' => null, |
47
|
|
|
'content' => null, |
48
|
|
|
'redirect' => null |
49
|
|
|
]; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param RouteAwareInterface $entity |
54
|
|
|
* @return Route |
55
|
|
|
*/ |
56
|
|
|
public function createFromRouteAwareEntity(RouteAwareInterface $entity): Route |
57
|
|
|
{ |
58
|
|
|
$pageRoute = $this->slugify->slugify($entity->getDefaultRoute()); |
59
|
|
|
$routePrefix = $this->getRoutePrefix($entity); |
60
|
|
|
return $this->create( |
61
|
|
|
[ |
62
|
|
|
'route' => $routePrefix . $pageRoute, |
63
|
|
|
'content' => $entity |
64
|
|
|
] |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param RouteAwareInterface $entity |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
|
|
private function getRoutePrefix(RouteAwareInterface $entity): string |
73
|
|
|
{ |
74
|
|
|
$parent = method_exists($entity, 'getParent') ? $entity->getParent() : null; |
75
|
|
|
if ($parent && $parent instanceof RouteAwareInterface) { |
76
|
|
|
$parentRoute = $this->getParentRoute($parent); |
77
|
|
|
return $parentRoute->getRoute() . '/'; |
78
|
|
|
} |
79
|
|
|
return '/'; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param RouteAwareInterface $parent |
84
|
|
|
* @return mixed|Route |
85
|
|
|
*/ |
86
|
|
|
private function getParentRoute(RouteAwareInterface $parent) |
87
|
|
|
{ |
88
|
|
|
$parentRoute = $parent->getRoutes()->first(); |
89
|
|
|
if (!$parentRoute) { |
90
|
|
|
$parentRoute = $this->createFromRouteAwareEntity($parentRoute); |
91
|
|
|
} |
92
|
|
|
return $parentRoute; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|