Completed
Push — master ( ad2906...7100f5 )
by Alexander
06:10 queued 03:09
created

HttpJsonSchemaRequest::getBody()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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 5.6+
10
 *
11
 * @author    Yancharuk Alexander <alex at itvault dot info>
12
 * @copyright © 2012-2017 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
	public function getBody()
35
	{
36
		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
	public function check($definitions)
47
	{
48
		$raw_data  = $this->getBody();
49
		$schema    = json_decode($definitions);
50
		$validator = $this->getValidator();
51
52
		$validator->check($raw_data, $schema);
53
54
		if (!$validator->isValid()) {
55
			throw new UnprocessableException($validator->getErrors());
56
		}
57
58
		$this->setData((array) $raw_data);
59
	}
60
}
61