|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Validation of config |
|
5
|
|
|
* |
|
6
|
|
|
* @category NFePHP |
|
7
|
|
|
* @package NFePHP\NFe\Common\Config |
|
8
|
|
|
* @copyright NFePHP Copyright (c) 2008-2019 |
|
9
|
|
|
* @license http://www.gnu.org/licenses/lgpl.txt LGPLv3+ |
|
10
|
|
|
* @license https://opensource.org/licenses/MIT MIT |
|
11
|
|
|
* @license http://www.gnu.org/licenses/gpl.txt GPLv3+ |
|
12
|
|
|
* @author Roberto L. Machado <linux.rlm at gmail dot com> |
|
13
|
|
|
* @link http://github.com/nfephp-org/sped-nfe for the canonical source repository |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace NFePHP\NFe\Common; |
|
17
|
|
|
|
|
18
|
|
|
use JsonSchema\Validator as JsonValid; |
|
19
|
|
|
use NFePHP\NFe\Exception\DocumentsException; |
|
20
|
|
|
|
|
21
|
|
|
class Config |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* Validate method |
|
25
|
|
|
* @param string $content config.json |
|
26
|
|
|
* @return \stdClass |
|
27
|
|
|
*/ |
|
28
|
76 |
|
public static function validate($content) |
|
29
|
|
|
{ |
|
30
|
76 |
|
if (!is_string($content)) { |
|
|
|
|
|
|
31
|
1 |
|
throw DocumentsException::wrongDocument(8, "Não foi passado um json."); |
|
32
|
|
|
} |
|
33
|
75 |
|
$std = json_decode($content); |
|
34
|
75 |
|
if (!is_object($std)) { |
|
35
|
1 |
|
throw DocumentsException::wrongDocument(8, "Não foi passado um json valido."); |
|
36
|
|
|
} |
|
37
|
74 |
|
self::validInputData($std); |
|
38
|
68 |
|
return $std; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Validation with JsonValid::class |
|
43
|
|
|
* @param object $data |
|
44
|
|
|
* @return boolean |
|
45
|
|
|
* @throws DocumentsException |
|
46
|
|
|
*/ |
|
47
|
74 |
|
protected static function validInputData($data) |
|
48
|
|
|
{ |
|
49
|
74 |
|
$filejsonschema = __DIR__ . "/../../storage/config.schema"; |
|
50
|
74 |
|
$validator = new JsonValid(); |
|
51
|
74 |
|
$validator->check($data, (object)['$ref' => 'file://' . $filejsonschema]); |
|
52
|
74 |
|
if (!$validator->isValid()) { |
|
53
|
6 |
|
$msg = ""; |
|
54
|
6 |
|
foreach ($validator->getErrors() as $error) { |
|
55
|
6 |
|
$msg .= sprintf("[%s] %s\n", $error['property'], $error['message']); |
|
56
|
|
|
} |
|
57
|
6 |
|
throw DocumentsException::wrongDocument(8, $msg); |
|
58
|
|
|
} |
|
59
|
68 |
|
return true; |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|