Completed
Push — master ( a6c8f9...b97be3 )
by Vladimir
03:52 queued 19s
created

RepeaterPageView::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @copyright 2017 Vladimir Jimenez
5
 * @license   https://github.com/allejo/stakx/blob/master/LICENSE.md MIT
6
 */
7
8
namespace allejo\stakx\Document;
9
10
use allejo\stakx\Filesystem\File;
11
use allejo\stakx\FrontMatter\ExpandedValue;
12
13
class RepeaterPageView extends BasePageView implements TemplateReadyDocument
14
{
15
    /** @var \ArrayIterator An iterator for the permalinks used in order for this entity to be treated as a static PageView. */
16
    private $permalinksIterator;
17
18
    /** @var ExpandedValue[] All of the expanded permalinks. */
19
    private $permalinks;
20
21
    /** @var ExpandedValue[][] All of expanded redirects that should point to the respective permalink; this is estimated by index. */
22
    private $redirectLinks;
23
24
    /**
25
     * RepeaterPageView constructor.
26
     */
27 2
    public function __construct(File $file)
28
    {
29 2
        parent::__construct($file);
30
31 2
        $this->type = BasePageView::REPEATER_TYPE;
32
33 2
        $this->configurePermalinks();
34 2
    }
35
36
    /**
37
     * Get the expanded values for the permalinks to this PageView.
38
     *
39
     * @return ExpandedValue[]
40
     */
41 2
    public function getRepeaterPermalinks()
42
    {
43 2
        return $this->permalinks;
44
    }
45
46
    /**
47
     * Get the expanded values for the redirects pointing to this PageView.
48
     *
49
     * @return ExpandedValue[][]
50
     */
51 2
    public function getRepeaterRedirects()
52
    {
53 2
        return $this->redirectLinks;
54
    }
55
56
    /**
57
     * When looping through permalinks in a RepeaterPageView, the permalink needs to be updated each time so that it may
58
     * behave as a static PageView.
59
     */
60 2
    public function bumpPermalink()
61
    {
62 2
        $this->permalink = $this->permalinksIterator->current()->getEvaluated();
63 2
        $this->permalinksIterator->next();
64 2
    }
65
66
    /**
67
     * Rewind the permalink iterator to the beginning.
68
     */
69 2
    public function rewindPermalink()
70
    {
71 2
        $this->permalinksIterator->rewind();
72 2
    }
73
74
    /**
75
     * Configure permalinks from expanded values internally.
76
     */
77 2
    private function configurePermalinks()
78
    {
79
        // Cause the Front Matter to be evaluated
80 2
        $this->getFrontMatter();
81
82 2
        $evaluated = $this->frontMatter['permalink'];
83
84 2
        $this->permalinks = $evaluated[0];
85 2
        array_shift($evaluated);
86 2
        $this->redirectLinks = $evaluated;
87
88 2
        $this->permalinksIterator = new \ArrayIterator($this->permalinks);
89 2
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94 2
    public function buildPermalink($force = false)
95
    {
96 2
        return;
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102 2
    public function createJail()
103
    {
104 2
        return (new JailedDocument($this, self::$whiteListedFunctions));
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110
    public function jsonSerialize()
111
    {
112
        return [];
113
    }
114
}
115