1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tomaj\NetteApi\Params; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use JsonSchema\Validator; |
7
|
|
|
use Nette\Application\UI\Form; |
8
|
|
|
use Nette\Forms\Controls\BaseControl; |
9
|
|
|
use Tomaj\NetteApi\ValidationResult\ValidationResult; |
10
|
|
|
use Tomaj\NetteApi\ValidationResult\ValidationResultInterface; |
11
|
|
|
|
12
|
|
|
class JsonInputParam extends InputParam |
13
|
|
|
{ |
14
|
|
|
protected $type = self::TYPE_POST_JSON; |
15
|
|
|
|
16
|
|
|
private $schema; |
17
|
|
|
|
18
|
9 |
|
public function __construct(string $key, string $schema) |
19
|
|
|
{ |
20
|
9 |
|
parent::__construct($key); |
21
|
9 |
|
$this->schema = $schema; |
22
|
9 |
|
} |
23
|
|
|
|
24
|
3 |
|
public function setMulti(): InputParam |
25
|
|
|
{ |
26
|
3 |
|
throw new Exception('Cannot use multi json input param'); |
27
|
|
|
} |
28
|
|
|
|
29
|
3 |
|
public function getValue() |
30
|
|
|
{ |
31
|
3 |
|
$input = file_get_contents("php://input") ?: $this->default; |
32
|
3 |
|
return json_decode($input, true); |
33
|
|
|
} |
34
|
|
|
|
35
|
3 |
|
public function validate(): ValidationResultInterface |
36
|
|
|
{ |
37
|
3 |
|
$schemaValidator = new Validator(); |
38
|
3 |
|
$value = $this->getValue(); |
39
|
3 |
|
if (json_last_error()) { |
40
|
3 |
|
return new ValidationResult(ValidationResult::STATUS_ERROR, [json_last_error_msg()]); |
41
|
|
|
} |
42
|
|
|
|
43
|
3 |
|
if (!$value && $this->isRequired() === self::OPTIONAL) { |
44
|
3 |
|
return new ValidationResult(ValidationResult::STATUS_OK); |
45
|
|
|
} |
46
|
|
|
|
47
|
3 |
|
$value = json_decode(json_encode($value)); |
48
|
3 |
|
$schemaValidator->validate($value, json_decode($this->schema)); |
49
|
|
|
|
50
|
3 |
|
if ($schemaValidator->isValid()) { |
51
|
|
|
return new ValidationResult(ValidationResult::STATUS_OK); |
52
|
|
|
} |
53
|
|
|
|
54
|
3 |
|
$errors = []; |
55
|
3 |
|
foreach ($schemaValidator->getErrors() as $error) { |
56
|
3 |
|
$errorMessage = ''; |
57
|
3 |
|
if ($error['property']) { |
58
|
|
|
$errorMessage .= '[Property ' . $error['property'] . '] '; |
59
|
|
|
} |
60
|
3 |
|
$errorMessage .= $error['message']; |
61
|
3 |
|
$errors[] = $errorMessage; |
62
|
|
|
} |
63
|
3 |
|
return new ValidationResult(ValidationResult::STATUS_ERROR, $errors); |
64
|
|
|
} |
65
|
|
|
|
66
|
3 |
|
protected function addFormInput(Form $form, string $key): BaseControl |
67
|
|
|
{ |
68
|
3 |
|
$this->description .= '<div id="show_schema_link"><a href="#" onclick="document.getElementById(\'json_schema\').style.display = \'block\'; document.getElementById(\'show_schema_link\').style.display = \'none\'; return false;">Show schema</a></div> |
69
|
|
|
<div id="json_schema" style="display: none;"> |
70
|
|
|
<div><a href="#" onclick="document.getElementById(\'show_schema_link\').style.display = \'block\'; document.getElementById(\'json_schema\').style.display = \'none\'; return false;">Hide schema</a></div>' |
71
|
3 |
|
. nl2br(str_replace(' ', ' ', json_encode(json_decode($this->schema), JSON_PRETTY_PRINT))) . '</div>'; |
72
|
|
|
|
73
|
3 |
|
return $form->addTextArea('post_raw', $this->getParamLabel()) |
74
|
3 |
|
->setHtmlAttribute('rows', 10); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|