|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @package s9e\TextFormatter |
|
5
|
|
|
* @copyright Copyright (c) 2010-2017 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
|
|
|
|
|
64
|
19 |
|
$siteConfig += ['extract' => [], 'scrape' => []]; |
|
65
|
19 |
|
$siteConfig['extract'] = $this->normalizeRegexp($siteConfig['extract']); |
|
66
|
19 |
|
$siteConfig['scrape'] = $this->normalizeScrape($siteConfig['scrape']); |
|
67
|
|
|
|
|
68
|
19 |
|
return $siteConfig; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Normalize a regexp / indexed array of regexps |
|
73
|
|
|
* |
|
74
|
|
|
* @param array|string |
|
75
|
|
|
* @return array |
|
76
|
|
|
*/ |
|
77
|
19 |
|
protected function normalizeRegexp($value) |
|
78
|
|
|
{ |
|
79
|
19 |
|
return (array) $value; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Normalize the "scrape" value |
|
84
|
|
|
* |
|
85
|
|
|
* @param array |
|
86
|
|
|
* @return array |
|
87
|
|
|
*/ |
|
88
|
19 |
|
protected function normalizeScrape($value) |
|
89
|
|
|
{ |
|
90
|
19 |
|
if (!empty($value) && !isset($value[0])) |
|
91
|
|
|
{ |
|
92
|
1 |
|
$value = [$value]; |
|
93
|
|
|
} |
|
94
|
19 |
|
foreach ($value as &$scrape) |
|
95
|
|
|
{ |
|
96
|
2 |
|
$scrape += ['extract' => [], 'match' => '//']; |
|
97
|
2 |
|
$scrape['extract'] = $this->normalizeRegexp($scrape['extract']); |
|
98
|
2 |
|
$scrape['match'] = $this->normalizeRegexp($scrape['match']); |
|
99
|
|
|
} |
|
100
|
19 |
|
unset($scrape); |
|
101
|
|
|
|
|
102
|
19 |
|
return $value; |
|
103
|
|
|
} |
|
104
|
|
|
} |