|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace brendt\stitcher\parser; |
|
4
|
|
|
|
|
5
|
|
|
use brendt\stitcher\Config; |
|
6
|
|
|
use brendt\stitcher\factory\ParserFactory; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* The AbstractArrayParser class provides the abstraction needed to parse arrays of entries provided by |
|
10
|
|
|
* eg. the Yaml- or JsonParser. |
|
11
|
|
|
* |
|
12
|
|
|
* @see \brendt\stitcher\parser\YamlParser |
|
13
|
|
|
* @see \brendt\stitcher\parser\JsonParser |
|
14
|
|
|
*/ |
|
15
|
|
|
abstract class AbstractArrayParser implements Parser { |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var ParserFactory |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $parserFactory; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* AbstractParser constructor. |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct() { |
|
26
|
|
|
$this->parserFactory = Config::getDependency('factory.parser'); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Parse an array of entries loaded from eg. the Yaml- or JsonParser |
|
31
|
|
|
* |
|
32
|
|
|
* @param array $data |
|
33
|
|
|
* |
|
34
|
|
|
* @return mixed |
|
35
|
|
|
* |
|
36
|
|
|
* @see \brendt\stitcher\parser\YamlParser |
|
37
|
|
|
* @see \brendt\stitcher\parser\JsonParser |
|
38
|
|
|
*/ |
|
39
|
|
|
protected function parseArrayData(array $data) { |
|
40
|
|
|
$result = []; |
|
41
|
|
|
|
|
42
|
|
|
foreach ($data as $id => $entry) { |
|
43
|
|
|
$result[$id] = $this->parseEntryData($id, $entry); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
return $result; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Parse a single entry. An entry has multiple fields with each of them a value. This value can either be a path |
|
51
|
|
|
* to another data entry which will be parsed (using the ParserFactory); an array with a key `src` set, |
|
52
|
|
|
* which refers to another data entry; or normal data which will be kept the way it was provided. |
|
53
|
|
|
* |
|
54
|
|
|
* After parsing all fields, an additional check is performed which sets the entry's ID if it wasn't set yet. |
|
55
|
|
|
* Finally, an array with parsed fields, representing the entry, is returned. |
|
56
|
|
|
* |
|
57
|
|
|
* @param $id |
|
58
|
|
|
* @param $entry |
|
59
|
|
|
* |
|
60
|
|
|
* @return array |
|
61
|
|
|
* |
|
62
|
|
|
* @see \brendt\stitcher\factory\ParserFactory |
|
63
|
|
|
*/ |
|
64
|
|
|
protected function parseEntryData($id, $entry) { |
|
65
|
|
|
foreach ($entry as $field => $value) { |
|
66
|
|
|
if (is_string($value) && preg_match('/.*\.(md|jpg|png|json|yml)$/', $value) > 0) { |
|
67
|
|
|
$parser = $this->parserFactory->getParser($value); |
|
68
|
|
|
$entry[$field] = $parser->parse(trim($value, '/')); |
|
69
|
|
|
} elseif (is_array($value) && array_key_exists('src', $value)) { |
|
70
|
|
|
$src = $value['src']; |
|
71
|
|
|
$parser = $this->parserFactory->getParser($src); |
|
72
|
|
|
$entry[$field] = $parser->parse($value); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
if (!isset($entry['id'])) { |
|
76
|
|
|
$entry['id'] = $id; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return $entry; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
} |
|
84
|
|
|
|