|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Bigwhoop\Trumpet\Config\Params; |
|
4
|
|
|
|
|
5
|
|
|
use Bigwhoop\Trumpet\Config\Author; |
|
6
|
|
|
use Bigwhoop\Trumpet\Config\ConfigException; |
|
7
|
|
|
use Bigwhoop\Trumpet\Config\Presentation; |
|
8
|
|
|
|
|
9
|
|
|
final class AuthorsParam implements Param |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* {@inheritdoc} |
|
13
|
|
|
*/ |
|
14
|
10 |
|
public function parse($value, Presentation $presentation) |
|
15
|
|
|
{ |
|
16
|
10 |
|
if (!is_array($value)) { |
|
17
|
|
|
throw new ConfigException('Authors have to be an array.'); |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
10 |
|
$authors = []; |
|
21
|
|
|
|
|
22
|
10 |
|
foreach (array_values($value) as $idx => $entry) { |
|
23
|
10 |
|
if (is_array($entry)) { |
|
24
|
3 |
|
$authors[] = $this->parseArrayEntry($idx, $entry); |
|
25
|
7 |
|
} elseif (is_string($entry)) { |
|
26
|
7 |
|
$authors[] = $this->parseStringEntry($idx, $entry); |
|
27
|
|
|
} else { |
|
28
|
|
|
throw new ConfigException("Author#$idx must either be an array or a string."); |
|
29
|
|
|
} |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
10 |
|
$presentation->authors = $authors; |
|
33
|
10 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param int $idx |
|
37
|
|
|
* @param string $entry |
|
38
|
|
|
* |
|
39
|
|
|
* @return Author |
|
40
|
|
|
*/ |
|
41
|
7 |
|
private function parseStringEntry($idx, $entry) |
|
|
|
|
|
|
42
|
|
|
{ |
|
43
|
7 |
|
$parts = array_map('trim', explode(',', $entry)); |
|
44
|
7 |
|
$name = array_shift($parts); |
|
45
|
|
|
|
|
46
|
7 |
|
$author = new Author($name); |
|
47
|
|
|
|
|
48
|
7 |
|
if (!empty($parts)) { |
|
49
|
6 |
|
$author->company = array_shift($parts); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
7 |
|
while (!empty($parts)) { |
|
53
|
5 |
|
if (filter_var($parts[0], FILTER_VALIDATE_EMAIL)) { |
|
54
|
2 |
|
$author->email = array_shift($parts); |
|
55
|
4 |
|
} elseif (substr($parts[0], 0, 1) === '@') { |
|
56
|
2 |
|
$author->twitter = array_shift($parts); |
|
57
|
3 |
|
} elseif (substr($parts[0], 0, 4) === 'http') { |
|
58
|
3 |
|
$author->website = array_shift($parts); |
|
59
|
|
|
} else { |
|
60
|
1 |
|
array_shift($parts); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
7 |
|
return $author; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param int $idx |
|
69
|
|
|
* @param array $entry |
|
70
|
|
|
* |
|
71
|
|
|
* @return Author |
|
72
|
|
|
* |
|
73
|
|
|
* @throws ConfigException |
|
74
|
|
|
*/ |
|
75
|
|
|
private function parseArrayEntry($idx, array $entry) |
|
76
|
|
|
{ |
|
77
|
|
|
$fnCreateIsStringValidator = function ($idx, $key) { |
|
78
|
3 |
|
return function ($value) use ($idx, $key) { |
|
79
|
3 |
|
if (!is_string($value)) { |
|
80
|
|
|
throw new ConfigException("Author#$idx must have a '$key' property that is a string."); |
|
81
|
|
|
} |
|
82
|
3 |
|
}; |
|
83
|
|
|
}; |
|
84
|
|
|
|
|
85
|
3 |
|
$name = $this->pickFromArray($entry, 'name', '', $fnCreateIsStringValidator($idx, 'name')); |
|
86
|
|
|
|
|
87
|
3 |
|
$author = new Author($name); |
|
88
|
|
|
|
|
89
|
3 |
|
$author->company = $this->pickFromArray($entry, 'company', '', $fnCreateIsStringValidator($idx, 'company')); |
|
90
|
3 |
|
$author->email = $this->pickFromArray($entry, 'email', '', $fnCreateIsStringValidator($idx, 'email')); |
|
91
|
3 |
|
$author->twitter = $this->formatTwitterHandle($this->pickFromArray($entry, 'twitter', '', $fnCreateIsStringValidator($idx, 'twitter'))); |
|
92
|
3 |
|
$author->website = $this->formatWebsite($this->pickFromArray($entry, 'website', '', $fnCreateIsStringValidator($idx, 'website'))); |
|
93
|
3 |
|
$author->email = $this->pickFromArray($entry, 'email', '', $fnCreateIsStringValidator($idx, 'email')); |
|
94
|
3 |
|
$author->skype = $this->pickFromArray($entry, 'skype', '', $fnCreateIsStringValidator($idx, 'skype')); |
|
95
|
|
|
|
|
96
|
3 |
|
return $author; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param array $params |
|
101
|
|
|
* @param string $key |
|
102
|
|
|
* @param string $defaultValue |
|
103
|
|
|
* @param callable $validator |
|
104
|
|
|
* |
|
105
|
|
|
* @return mixed |
|
106
|
|
|
* |
|
107
|
|
|
* @throws ConfigException |
|
108
|
|
|
*/ |
|
109
|
3 |
|
private function pickFromArray(array $params, $key, $defaultValue, callable $validator) |
|
110
|
|
|
{ |
|
111
|
3 |
|
if (!array_key_exists($key, $params)) { |
|
112
|
2 |
|
return $defaultValue; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
3 |
|
$value = $params[$key]; |
|
116
|
3 |
|
$validationResult = $validator($value); |
|
117
|
|
|
|
|
118
|
3 |
|
if (is_string($validationResult) && $validationResult !== '') { |
|
119
|
|
|
throw new ConfigException($validationResult); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
3 |
|
return $value; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @param string $handle |
|
127
|
|
|
* |
|
128
|
|
|
* @return string |
|
129
|
|
|
*/ |
|
130
|
3 |
|
private function formatTwitterHandle($handle) |
|
131
|
|
|
{ |
|
132
|
3 |
|
if ($handle != '' && substr($handle, 0, 1) !== '@') { |
|
133
|
1 |
|
$handle = '@'.$handle; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
3 |
|
return $handle; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @param string $url |
|
141
|
|
|
* |
|
142
|
|
|
* @return string |
|
143
|
|
|
*/ |
|
144
|
3 |
|
private function formatWebsite($url) |
|
145
|
|
|
{ |
|
146
|
3 |
|
if ($url != '' && substr($url, 0, 4) !== 'http') { |
|
147
|
1 |
|
$url = 'http://'.$url; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
3 |
|
return $url; |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.