1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* tubee.io |
7
|
|
|
* |
8
|
|
|
* @copyright Copryright (c) 2017-2019 gyselroth GmbH (https://gyselroth.com) |
9
|
|
|
* @license GPL-3.0 https://opensource.org/licenses/GPL-3.0 |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Tubee\Resource; |
13
|
|
|
|
14
|
|
|
use InvalidArgumentException; |
15
|
|
|
|
16
|
|
|
class Validator |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Validate resource. |
20
|
|
|
*/ |
21
|
16 |
|
public static function validate(array $resource): array |
22
|
|
|
{ |
23
|
|
|
$defaults = [ |
24
|
16 |
|
'data' => [], |
25
|
|
|
]; |
26
|
|
|
|
27
|
16 |
|
$resource = array_replace_recursive($defaults, $resource); |
28
|
|
|
|
29
|
16 |
|
if (!isset($resource['name']) || !is_string($resource['name'])) { |
30
|
|
|
throw new InvalidArgumentException('name as string must be provided'); |
31
|
|
|
} |
32
|
|
|
|
33
|
16 |
|
if (preg_match('/[^a-z\.\-\_0-9]/', $resource['name'])) { |
34
|
|
|
throw new InvalidArgumentException('resoure name can only consists from lower case alphanumeric characters and . or _ or -'); |
35
|
|
|
} |
36
|
|
|
|
37
|
16 |
|
if (isset($resource['description']) && !is_string($resource['description'])) { |
38
|
|
|
throw new InvalidArgumentException('description must be a string'); |
39
|
|
|
} |
40
|
|
|
|
41
|
16 |
View Code Duplication |
if (isset($resource['data']) && !is_array($resource['data'])) { |
|
|
|
|
42
|
|
|
throw new InvalidArgumentException('data must be an array'); |
43
|
|
|
} |
44
|
|
|
|
45
|
16 |
|
if (isset($resource['secrets'])) { |
46
|
|
|
if (!is_array($resource['secrets'])) { |
47
|
|
|
throw new InvalidArgumentException('secrets must be an array'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
self::validateSecrets($resource['secrets']); |
51
|
|
|
} |
52
|
|
|
|
53
|
16 |
|
$resource = array_intersect_key($resource, array_flip(['name', 'data', 'description', 'secrets', 'kind'])); |
54
|
|
|
|
55
|
16 |
|
return $resource; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Validate secrets. |
60
|
|
|
*/ |
61
|
|
View Code Duplication |
protected static function validateSecrets(array $secrets): array |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
foreach ($secrets as $key => $value) { |
64
|
|
|
if (['secret', 'key', 'to'] != array_keys($value)) { |
65
|
|
|
throw new InvalidArgumentException('secret in secrets must contain secret, key and to as strings'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if (array_filter($value, 'is_string') != $value) { |
69
|
|
|
throw new InvalidArgumentException('secret in secrets must contain secret, key and to as strings'); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $secrets; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.