Passed
Push — master ( 29bd61...08926b )
by Brent
06:03
created

AbstractAdapter::transform()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 4
nop 2
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Brendt\Stitcher\Adapter;
4
5
use Brendt\Stitcher\Config;
6
use Brendt\Stitcher\factory\ParserFactory;
7
use Brendt\Stitcher\Site\Page;
8
9
/**
10
 * The AbstractAdapter class provides a base for adapters who need to parse template variables.
11
 */
12
abstract class AbstractAdapter implements Adapter
13
{
14
15
    /**
16
     * @var ParserFactory
17
     */
18
    protected $parserFactory;
19
20
    /**
21
     * Construct the adapter and set the parser factory variable.
22
     *
23
     * @see \Brendt\Stitcher\Factory\ParserFactory
24
     */
25
    public function __construct() {
26
        $this->parserFactory = Config::getDependency('factory.parser');
27
    }
28
29
    /**
30
     * @param Page|Page[] $pages
31
     * @param mixed  $filter
32
     *
33
     * @return Page[]
34
     */
35
    public function transform($pages, $filter = null) : array {
36
        if (!is_array($pages)) {
37
            $pages = [$pages];
38
        }
39
40
        /** @var Page[] $result */
41
        $result = [];
42
43
        foreach ($pages as $page) {
44
            $result += $this->transformPage($page, $filter);
45
        }
46
47
        return $result;
48
    }
49
50
    /**
51
     * This function will get the parser based on the value provided.
52
     * This value is parsed by the parser, or returned if no suitable parser was found.
53
     *
54
     * @param $value
55
     *
56
     * @return mixed
57
     *
58
     * @see \Brendt\Stitcher\Factory\ParserFactory
59
     */
60
    protected function getData($value) {
61
        $parser = $this->parserFactory->getParser($value);
62
63
        if (!$parser) {
64
            return $value;
65
        }
66
67
        return $parser->parse($value);
68
    }
69
70
}
71