1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Harentius\BlogBundle; |
6
|
|
|
|
7
|
|
|
use Harentius\BlogBundle\Entity\AbstractPost; |
8
|
|
|
use Harentius\BlogBundle\Entity\Article; |
9
|
|
|
use Harentius\BlogBundle\Entity\Category; |
10
|
|
|
use Harentius\BlogBundle\Entity\Tag; |
11
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
12
|
|
|
use WhiteOctober\BreadcrumbsBundle\Model\Breadcrumbs; |
13
|
|
|
|
14
|
|
|
class BreadCrumbsManager |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var Breadcrumbs |
18
|
|
|
*/ |
19
|
|
|
private $breadcrumbs; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var UrlGeneratorInterface |
23
|
|
|
*/ |
24
|
|
|
private $urlGenerator; |
25
|
|
|
|
26
|
|
|
public function __construct(Breadcrumbs $breadcrumbs, UrlGeneratorInterface $urlGenerator) |
27
|
|
|
{ |
28
|
|
|
$this->urlGenerator = $urlGenerator; |
29
|
|
|
$this->breadcrumbs = $breadcrumbs; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param Category $category |
34
|
|
|
*/ |
35
|
|
|
public function buildCategory(Category $category): void |
36
|
|
|
{ |
37
|
|
|
do { |
38
|
|
|
$this->breadcrumbs->prependItem( |
39
|
|
|
$category->getName(), |
40
|
|
|
$this->urlGenerator->generate('harentius_blog_category', ['slug' => $category->getSlugWithParents()]) |
41
|
|
|
); |
42
|
|
|
} while ($category = $category->getParent()); |
43
|
|
|
|
44
|
|
|
$this->prependHomepage(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param Tag $tag |
49
|
|
|
*/ |
50
|
|
|
public function buildTag(Tag $tag): void |
51
|
|
|
{ |
52
|
|
|
$this->breadcrumbs->addItem($tag->getName()); |
53
|
|
|
$this->prependHomepage(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string $year |
58
|
|
|
* @param string|null $month |
59
|
|
|
*/ |
60
|
|
|
public function buildArchive(string $year, ?string $month = null): void |
61
|
|
|
{ |
62
|
|
|
$this->breadcrumbs->addItem($year, $this->urlGenerator->generate('harentius_blog_archive_year', ['year' => $year])); |
63
|
|
|
|
64
|
|
|
if ($month) { |
|
|
|
|
65
|
|
|
$this->breadcrumbs->addItem($month); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$this->prependHomepage(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function buildPost(AbstractPost $post) |
72
|
|
|
{ |
73
|
|
|
if ($post instanceof Article && $post->getCategory()) { |
74
|
|
|
$this->buildCategory($post->getCategory()); |
75
|
|
|
$this->breadcrumbs->addItem($post->getTitle()); |
76
|
|
|
} else { |
77
|
|
|
$this->breadcrumbs->addItem($post->getTitle()); |
78
|
|
|
$this->prependHomepage(); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
private function prependHomepage(): void |
83
|
|
|
{ |
84
|
|
|
$this->breadcrumbs->prependItem('Homepage', $this->urlGenerator->generate('harentius_blog_homepage')); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: