|
1
|
|
|
<?php |
|
2
|
|
|
namespace Staticall\Petrovich\Petrovich; |
|
3
|
|
|
|
|
4
|
|
|
use Symfony\Component\Yaml\Yaml; |
|
5
|
|
|
|
|
6
|
|
|
class Loader |
|
7
|
|
|
{ |
|
8
|
|
|
const FILE_TYPE_JSON = 'json'; |
|
9
|
|
|
const FILE_TYPE_YML = 'yml'; |
|
10
|
|
|
const DEFAULT_FILE_TYPE = self::FILE_TYPE_JSON; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Loads provided file |
|
14
|
|
|
* |
|
15
|
|
|
* @param string $filePath |
|
16
|
|
|
* @param string|null $fileType |
|
17
|
|
|
* @param bool $shouldValidate |
|
18
|
|
|
* |
|
19
|
|
|
* @return Ruleset |
|
20
|
|
|
* @throws IOException |
|
21
|
|
|
* @throws RuntimeException |
|
22
|
|
|
* @throws ValidationException |
|
23
|
|
|
*/ |
|
24
|
|
|
public static function load(string $filePath, ?string $fileType = null, bool $shouldValidate = false) : Ruleset |
|
25
|
|
|
{ |
|
26
|
|
|
if ($fileType === null) { |
|
27
|
|
|
$fileType = static::determineFileType($filePath); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
switch ($fileType) { |
|
31
|
|
|
case static::FILE_TYPE_JSON: |
|
32
|
|
|
return static::loadJson($filePath, $shouldValidate); |
|
33
|
|
|
case static::FILE_TYPE_YML: |
|
34
|
|
|
return static::loadYml($filePath, $shouldValidate); |
|
35
|
|
|
default: |
|
36
|
|
|
throw new RuntimeException('File has invalid format'); |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Loads rules from JSON |
|
42
|
|
|
* |
|
43
|
|
|
* @param string $filePath |
|
44
|
|
|
* @param bool $shouldValidate |
|
45
|
|
|
* |
|
46
|
|
|
* @return Ruleset |
|
47
|
|
|
* |
|
48
|
|
|
* @throws IOException |
|
49
|
|
|
* @throws ValidationException |
|
50
|
|
|
*/ |
|
51
|
|
|
public static function loadJson(string $filePath, bool $shouldValidate = false) : Ruleset |
|
52
|
|
|
{ |
|
53
|
|
|
/** @var array $rules */ |
|
54
|
|
|
$rules = \json_decode(static::loadFile($filePath), true); |
|
55
|
|
|
|
|
56
|
|
|
return new Ruleset($rules, $shouldValidate); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Loads rules from Yaml |
|
61
|
|
|
* |
|
62
|
|
|
* @param string $filePath |
|
63
|
|
|
* @param bool $shouldValidate |
|
64
|
|
|
* |
|
65
|
|
|
* @return Ruleset |
|
66
|
|
|
* |
|
67
|
|
|
* @throws ValidationException |
|
68
|
|
|
*/ |
|
69
|
|
|
public static function loadYml(string $filePath, bool $shouldValidate = false) : Ruleset |
|
70
|
|
|
{ |
|
71
|
|
|
$rules = Yaml::parseFile($filePath); |
|
72
|
|
|
|
|
73
|
|
|
return new Ruleset($rules, $shouldValidate); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Loads file content into a string |
|
78
|
|
|
* |
|
79
|
|
|
* @param string $filePath |
|
80
|
|
|
* |
|
81
|
|
|
* @return string |
|
82
|
|
|
* |
|
83
|
|
|
* @throws IOException |
|
84
|
|
|
*/ |
|
85
|
|
|
public static function loadFile(string $filePath) : string |
|
86
|
|
|
{ |
|
87
|
|
|
if (\is_readable($filePath) === false) { |
|
88
|
|
|
throw new IOException('File "' . $filePath . '" doesn\'t exist or is not readable'); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return \file_get_contents($filePath); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Determines file type from passed file path |
|
96
|
|
|
* |
|
97
|
|
|
* @param string $filePath |
|
98
|
|
|
* |
|
99
|
|
|
* @return string |
|
100
|
|
|
*/ |
|
101
|
|
|
public static function determineFileType(string $filePath) : string |
|
102
|
|
|
{ |
|
103
|
|
|
$extension = \pathinfo($filePath, \PATHINFO_EXTENSION); |
|
104
|
|
|
|
|
105
|
|
|
if ($extension === '') { |
|
106
|
|
|
return static::DEFAULT_FILE_TYPE; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
if (\in_array($extension, static::getAvailableFileTypes(), true) === false) { |
|
110
|
|
|
return static::DEFAULT_FILE_TYPE; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return $extension; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Returns available file types |
|
118
|
|
|
* |
|
119
|
|
|
* @return array |
|
120
|
|
|
*/ |
|
121
|
|
|
public static function getAvailableFileTypes() : array |
|
122
|
|
|
{ |
|
123
|
|
|
return [ |
|
124
|
|
|
static::FILE_TYPE_JSON, |
|
125
|
|
|
static::FILE_TYPE_YML, |
|
126
|
|
|
]; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
public static function getVendorRulesFilePath(string $type = self::FILE_TYPE_JSON) |
|
130
|
|
|
{ |
|
131
|
|
|
return __DIR__ . '/../../vendor/cloudloyalty/petrovich-rules/rules.' . $type; |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|