1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package s9e\TextFormatter |
5
|
|
|
* @copyright Copyright (c) 2010-2018 The s9e Authors |
6
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License |
7
|
|
|
*/ |
8
|
|
|
namespace s9e\TextFormatter\Plugins\MediaEmbed\Configurator\Collections; |
9
|
|
|
|
10
|
|
|
use InvalidArgumentException; |
11
|
|
|
use RuntimeException; |
12
|
|
|
use s9e\TextFormatter\Configurator\Collections\NormalizedCollection; |
13
|
|
|
|
14
|
|
|
class SiteDefinitionCollection extends NormalizedCollection |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* {@inheritdoc} |
18
|
|
|
*/ |
19
|
|
|
protected $onDuplicateAction = 'replace'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* {@inheritdoc} |
23
|
|
|
*/ |
24
|
1 |
|
protected function getAlreadyExistsException($key) |
25
|
|
|
{ |
26
|
1 |
|
return new RuntimeException("Media site '" . $key . "' already exists"); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* {@inheritdoc} |
31
|
|
|
*/ |
32
|
3 |
|
protected function getNotExistException($key) |
33
|
|
|
{ |
34
|
3 |
|
return new RuntimeException("Media site '" . $key . "' does not exist"); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Validate and normalize a site ID |
39
|
|
|
* |
40
|
|
|
* @param string $siteId |
41
|
|
|
* @return string |
42
|
|
|
*/ |
43
|
30 |
|
public function normalizeKey($siteId) |
44
|
|
|
{ |
45
|
30 |
|
$siteId = strtolower($siteId); |
46
|
30 |
|
if (!preg_match('(^[a-z0-9]+$)', $siteId)) |
47
|
|
|
{ |
48
|
5 |
|
throw new InvalidArgumentException('Invalid site ID'); |
49
|
|
|
} |
50
|
|
|
|
51
|
27 |
|
return $siteId; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
20 |
|
public function normalizeValue($siteConfig) |
58
|
|
|
{ |
59
|
20 |
|
if (!is_array($siteConfig)) |
60
|
|
|
{ |
61
|
1 |
|
throw new InvalidArgumentException('Invalid site definition type'); |
62
|
|
|
} |
63
|
19 |
|
if (!isset($siteConfig['host'])) |
64
|
|
|
{ |
65
|
|
|
throw new InvalidArgumentException('Missing host from site definition'); |
66
|
|
|
} |
67
|
|
|
|
68
|
19 |
|
$siteConfig += ['attributes' => [], 'extract' => [], 'scrape' => []]; |
69
|
19 |
|
$siteConfig['extract'] = $this->normalizeRegexp($siteConfig['extract']); |
70
|
19 |
|
$siteConfig['host'] = array_map('strtolower', (array) $siteConfig['host']); |
71
|
19 |
|
$siteConfig['scrape'] = $this->normalizeScrape($siteConfig['scrape']); |
72
|
|
|
|
73
|
19 |
|
foreach ($siteConfig['attributes'] as &$attrConfig) |
74
|
|
|
{ |
75
|
14 |
|
if (isset($attrConfig['filterChain'])) |
76
|
|
|
{ |
77
|
14 |
|
$attrConfig['filterChain'] = (array) $attrConfig['filterChain']; |
78
|
|
|
} |
79
|
|
|
} |
80
|
19 |
|
unset($attrConfig); |
81
|
|
|
|
82
|
19 |
|
return $siteConfig; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Normalize a regexp / indexed array of regexps |
87
|
|
|
* |
88
|
|
|
* @param array|string |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
19 |
|
protected function normalizeRegexp($value) |
92
|
|
|
{ |
93
|
19 |
|
return (array) $value; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Normalize the "scrape" value |
98
|
|
|
* |
99
|
|
|
* @param array |
100
|
|
|
* @return array |
101
|
|
|
*/ |
102
|
19 |
|
protected function normalizeScrape($value) |
103
|
|
|
{ |
104
|
19 |
|
if (!empty($value) && !isset($value[0])) |
105
|
|
|
{ |
106
|
1 |
|
$value = [$value]; |
107
|
|
|
} |
108
|
19 |
|
foreach ($value as &$scrape) |
109
|
|
|
{ |
110
|
2 |
|
$scrape += ['extract' => [], 'match' => '//']; |
111
|
2 |
|
$scrape['extract'] = $this->normalizeRegexp($scrape['extract']); |
112
|
2 |
|
$scrape['match'] = $this->normalizeRegexp($scrape['match']); |
113
|
|
|
} |
114
|
19 |
|
unset($scrape); |
115
|
|
|
|
116
|
19 |
|
return $value; |
117
|
|
|
} |
118
|
|
|
} |