forked from Herzult/HerzultForumBundle
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathForumUrlGenerator.php
73 lines (61 loc) · 2.36 KB
/
ForumUrlGenerator.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
66
67
68
69
70
71
72
73
<?php
namespace Herzult\Bundle\ForumBundle\Router;
use Herzult\Bundle\ForumBundle\Model\Category;
use Herzult\Bundle\ForumBundle\Model\Topic;
use Herzult\Bundle\ForumBundle\Model\Post;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class ForumUrlGenerator
{
protected $urlGenerator;
protected $nbPostsPerPage;
public function __construct(UrlGeneratorInterface $urlGenerator, $nbPostsPerPage)
{
$this->urlGenerator = $urlGenerator;
$this->nbPostsPerPage = $nbPostsPerPage;
}
public function urlForCategory(Category $category, $absolute = false)
{
return $this->urlGenerator->generate('herzult_forum_category_show', array(
'slug' => $category->getSlug()
), $absolute);
}
public function urlForCategoryAtomFeed(Category $category, $absolute = false)
{
return $this->urlGenerator->generate('herzult_forum_category_show', array(
'slug' => $category->getSlug(),
'_format' => 'xml'
), $absolute);
}
public function urlForTopic(Topic $topic, $absolute = false)
{
return $this->urlGenerator->generate('herzult_forum_topic_show', array(
'categorySlug' => $topic->getCategory()->getSlug(),
'slug' => $topic->getSlug()
), $absolute);
}
public function urlForTopicAtomFeed(Topic $topic, $absolute = false)
{
return $this->urlGenerator->generate('herzult_forum_topic_show', array(
'categorySlug' => $topic->getCategory()->getSlug(),
'slug' => $topic->getSlug(),
'_format' => 'xml'
), $absolute);
}
public function urlForTopicReply(Topic $topic, $absolute = false)
{
return $this->urlGenerator->generate('herzult_forum_topic_post_new', array(
'categorySlug' => $topic->getCategory()->getSlug(),
'slug' => $topic->getSlug()
));
}
public function urlForPost(Post $post, $absolute = false)
{
$topicUrl = $this->urlForTopic($post->getTopic(), $absolute);
$topicPage = ceil($post->getNumber() / $this->nbPostsPerPage);
return sprintf('%s?page=%d#%d', $topicUrl, $topicPage, $post->getNumber());
}
public function getTopicNumPages(Topic $topic)
{
return ceil($topic->getNumPosts() / $this->nbPostsPerPage);
}
}