HttpJsonSchemaRequest::check()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 13
ccs 8
cts 8
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * Processing HTTP-request with JSON body
4
 *
5
 * This class intended for use with JSON-Schema validator adapter
6
 *
7
 * @file      HttpJsonSchemaRequest.php
8
 *
9
 * PHP version 8.0+
10
 *
11
 * @author    Yancharuk Alexander <alex at itvault dot info>
12
 * @copyright © 2012-2021 Alexander Yancharuk <alex at itvault at info>
13
 * @date      2017-01-18 10:20
14
 * @license   The BSD 3-Clause License
15
 *            <https://tldrlegal.com/license/bsd-3-clause-license-(revised)>
16
 */
17
18
namespace Veles\Request;
19
20
use Veles\Exceptions\Http\UnprocessableException;
21
22
/**
23
 * Class   HttpJsonSchemaRequest
24
 *
25
 * @author Yancharuk Alexander <alex at itvault at info>
26
 */
27
class HttpJsonSchemaRequest extends HttpRequestAbstract
28
{
29
	/**
30
	 * Getting http-request body
31
	 *
32
	 * @return array
33
	 */
34 1
	public function getBody()
35
	{
36 1
		return json_decode(file_get_contents($this->stream));
37
	}
38
39
	/**
40
	 * Check input according definitions
41
	 *
42
	 * @param mixed $definitions
43
	 *
44
	 * @throws UnprocessableException
45
	 */
46 2
	public function check($definitions)
47
	{
48 2
		$raw_data  = $this->getBody();
49 2
		$schema    = json_decode($definitions);
50 2
		$validator = $this->getValidator();
51
52 2
		$validator->check($raw_data, $schema);
53
54 2
		if (!$validator->isValid()) {
55 1
			throw new UnprocessableException($validator->getErrors());
56
		}
57
58 1
		$this->setData((array) $raw_data);
59
	}
60
}
61