Completed
Pull Request — master (#18)
by Vladimir
02:06
created

PageView::getChildren()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace allejo\stakx\Object;
4
5
use org\bovigo\vfs\vfsStream;
6
use org\bovigo\vfs\vfsStreamDirectory;
7
use Symfony\Component\Yaml\Yaml;
8
9
class PageView extends FrontMatterObject
10
{
11
    /**
12
     * The Content Items that belong to this Page View. This array will only have elements if it is a dynamic Page View.
13
     *
14
     * @var ContentItem[]
15
     */
16
    private $contentItems;
17
18
    /**
19
     * @var PageView[]
20
     */
21
    private $children;
22
23
    /**
24
     * @var vfsStreamDirectory
25
     */
26
    private static $vfsRoot;
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function __construct($filePath)
32
    {
33
        parent::__construct($filePath);
34
35
        $this->children = array();
36
    }
37
38
    /**
39
     * @param ContentItem $contentItem
40
     */
41
    public function addContentItem (&$contentItem)
42
    {
43
        $filePath = $this->fs->getRelativePath($contentItem->getFilePath());
44
45
        $this->contentItems[$filePath] = &$contentItem;
46
        $contentItem->setPageView($this);
47
    }
48
49
    /**
50
     * Get child PageViews
51
     *
52
     * A child is defined as a static PageView whose URL has a parent. For example, a PageView with a URL of
53
     * `/gallery/france/` would have the PageView whose URL is `/gallery` as a parent.
54
     *
55
     * @return PageView[]
56
     */
57
    public function &getChildren ()
58
    {
59
        return $this->children;
60
    }
61
62
    /**
63
     * @return string Twig body
64
     */
65
    public function getContent ()
66
    {
67
        return $this->bodyContent;
68
    }
69
70
    /**
71
     * Get all of the Content Items
72
     *
73
     * @return ContentItem[]
74
     */
75
    public function getContentItems ()
76
    {
77
        return $this->contentItems;
78
    }
79
80
    /**
81
     * A page is considered "dynamic" if it is dynamically generated from data in a collection.
82
     *
83
     * @return bool
84
     */
85
    public function isDynamicPage ()
86
    {
87
        return isset($this->frontMatter['collection']);
88
    }
89
90
    /**
91
     * A fallback for the site menus that use the `url` field.
92
     *
93
     * @deprecated 0.1.0
94
     * @todo Remove this in the next major release
0 ignored issues
show
Coding Style introduced by
Comment refers to a TODO task

This check looks TODO comments that have been left in the code.

``TODO``s show that something is left unfinished and should be attended to.

Loading history...
95
     */
96
    public function getUrl ()
97
    {
98
        return $this->getPermalink();
99
    }
100
101
    /**
102
     * Create a virtual PageView to create redirect files
103
     *
104
     * @param  string $redirectFrom The URL that will be redirecting to the target location
105
     * @param  string $redirectTo   The URL of the destination
106
     *
107
     * @return PageView A virtual PageView with the redirection template
108
     */
109
    public static function createRedirect ($redirectFrom, $redirectTo)
110
    {
111
        if (is_null(self::$vfsRoot))
112
        {
113
            self::$vfsRoot = vfsStream::setup();
114
        }
115
116
        $fileTemplate = "---\n%s\n---\n\n%s";
117
        $redirectFile = vfsStream::newFile(sprintf('%s.html.twig', uniqid()));
118
119
        $redirectFile
120
            ->setContent(
121
                sprintf(
122
                    $fileTemplate,
123
                    Yaml::dump(array(
124
                        'permalink' => $redirectFrom,
125
                        'redirect'  => $redirectTo,
126
                        'menu' => false
127
                    ), 2),
128
                    file_get_contents(
129
                        implode(DIRECTORY_SEPARATOR, array(
130
                            __DIR__, '..', 'Resources', 'redirect.html.twig'
131
                        ))
132
                    )
133
                )
134
            )
135
            ->at(self::$vfsRoot);
136
137
        return (new PageView($redirectFile->url()));
138
    }
139
}