Completed
Push — master ( 7485cd...ed0392 )
by Nazar
04:24
created

Data   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 8
c 4
b 0
f 1
lcom 1
cbo 0
dl 0
loc 49
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A init_data() 0 7 3
B data() 0 13 5
1
<?php
2
/**
3
 * @package   CleverStyle CMS
4
 * @author    Nazar Mokrynskyi <[email protected]>
5
 * @copyright Copyright (c) 2016, Nazar Mokrynskyi
6
 * @license   MIT License, see license.txt
7
 */
8
namespace cs\Request;
9
10
trait Data {
11
	/**
12
	 * Data array, similar to `$_POST`
13
	 *
14
	 * @var array
15
	 */
16
	public $data;
17
	/**
18
	 * Data stream resource, similar to `fopen('php://input', 'br')`
19
	 *
20
	 * Make sure you're controlling position in stream where you read something, if code in some other place might seek on this stream
21
	 *
22
	 * Stream is read-only
23
	 *
24
	 * @var null|resource
25
	 */
26
	public $data_stream;
27
	/**
28
	 * @param array                $data        Typically `$_POST`
29
	 * @param null|resource|string $data_stream String, like `php://input` or resource, like `fopen('php://input', 'br')`
30
	 */
31
	function init_data ($data = [], $data_stream = null) {
32
		if (is_resource($this->data_stream)) {
33
			fclose($this->data_stream);
34
		}
35
		$this->data        = $data;
36
		$this->data_stream = is_string($data_stream) ? fopen($data_stream, 'br') : $data_stream;
37
	}
38
	/**
39
	 * Get data item by name
40
	 *
41
	 * @param string|string[] $name
42
	 *
43
	 * @return false|mixed|mixed[] Data if exists or `false` otherwise
44
	 */
45
	function data ($name) {
46
		if (is_array($name)) {
47
			foreach ($name as &$n) {
48
				if (!isset($this->data[$n])) {
49
					return false;
50
				}
51
				$n = $this->data[$n];
52
			}
53
			return $name;
54
		}
55
		/** @noinspection OffsetOperationsInspection */
56
		return isset($this->data[$name]) ? $this->data[$name] : false;
57
	}
58
}
59