Completed
Push — master ( 6097a5...d36649 )
by Alexander
07:23 queued 03:45
created

HttpJsonRequest::check()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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