Passed
Push — develop ( 03421b...c9efed )
by steve
39:12 queued 25:46
created

PropertiesTrait   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Test Coverage

Coverage 91.11%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 19
eloc 38
dl 0
loc 101
rs 10
c 1
b 1
f 0
ccs 41
cts 45
cp 0.9111

8 Methods

Rating   Name   Duplication   Size   Complexity  
A jsonEncode() 0 14 2
A getDefaultProperties() 0 4 1
A resolveProperty() 0 6 3
A jsonSerialize() 0 4 1
A valueToArray() 0 12 6
A toArray() 0 12 4
A getClass() 0 3 1
A toJson() 0 10 1
1
<?php
2
namespace neon\core\traits;
3
4
use neon\core\helpers\Arr;
5
use neon\core\interfaces\IArrayable;
6
use neon\core\interfaces\IProperties;
7
use \yii\base\ArrayableTrait;
8
use yii\helpers\Json;
9
use yii\web\JsExpression;
10
11
/**
12
 * @link http://www.newicon.net/neon
13
 * @copyright Copyright (c) 2017 Newicon Ltd
14
 * @license http://www.newicon.net/neon/license/
15
 * @author Steve O'Brien <[email protected]> 07/01/2018
16
 * @package neon
17
 */
18
trait PropertiesTrait
19
{
20 12
	public function toArray($except=[])
21
	{
22 12
		$array= [];
23 12
		$properties = $this->getProperties();
0 ignored issues
show
Bug introduced by
It seems like getProperties() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

23
		/** @scrutinizer ignore-call */ 
24
  $properties = $this->getProperties();
Loading history...
24 12
		foreach($properties as $key => $value) {
25 12
			if (in_array($value, $except)) { continue; }
26 12
			list($prop, $value) = $this->resolveProperty($key, $value);
27 12
			$array[$prop] = $this->valueToArray($value);
28
		}
29 12
		if (!isset($array['class']))
30
			$array['class'] = get_called_class();
31 12
		return $array;
32
	}
33
34
	/**
35
	 * Resolves properties
36
	 * @param $key
37
	 * @param $value
38
	 * @return array
39
	 */
40 12
	public function resolveProperty($key, $value)
41
	{
42 12
		if (is_int($key)) $key = $value;
43 12
		$value = is_string($value) ? $this->$value : call_user_func($value, $this, $key);
44
45 12
		return [$key, $value];
46
	}
47
48 12
	public function valueToArray($value)
49
	{
50 12
		if (is_array($value)) {
51 12
			foreach($value as $key => $val) {
52 12
				$value[$key] = $this->valueToArray($val);
53
			}
54 12
		} else if (is_object($value) && $value instanceof IArrayable) {
55 12
			$value = $value->toArray();
56 12
		} else if ($value instanceof JsExpression) {
57
			//dd($value);
58
		}
59 12
		return $value;
60
	}
61
62
	public function getDefaultProperties()
63
	{
64
		$reflectionClass = new \ReflectionClass(get_class($this));
65
		return $reflectionClass->getDefaultProperties();
66
	}
67
68
	/**
69
	 * Implementation of JsonSerializable
70
	 * Serializes all the properties into property_key => property_value
71
	 * __NOTE:__ this returns an array as is expected by the interface. The name is a little misleading
72
	 * @return array
73
	 */
74 46
	public function jsonSerialize()
75
	{
76 46
		$array = $this->toArray();
77 46
		return $array;
78
	}
79
80
	/**
81
	 * @inheritdoc
82
	 */
83 2
	public function toJson()
84
	{
85 2
		profile_begin('IProperties: toJson');
86
		// call the serialise first. This is so that any exceptions thrown
87
		// will display properly. If left to json_encode, it captures the
88
		// exceptions and unhelpfully says it couldn't serialise but not why
89 2
		$serialise = $this->jsonSerialize();
90 2
		$json = $this->jsonEncode($serialise);
91 2
		profile_end('IProperties: toJson');
92 2
		return $json;
93
	}
94
95
	/**
96
	 * Encode JSON correctly
97
	 * @param $properties
98
	 * @return string
99
	 */
100 2
	public function jsonEncode($properties)
101
	{
102
		// by using JSON_FORCE_OBJECT - we make php and javascript encoding more compatible
103
		// php arrays will always be represented as javascript objects.
104
		// this makes associative php arrays passable - indexed arrays will be output like:
105
		// {"0":"value at index 0", "1":"item at index 2"}
106 2
		$options = JSON_HEX_QUOT 
107 2
		| JSON_HEX_APOS 
108 2
		| JSON_UNESCAPED_UNICODE 
109 2
		| JSON_UNESCAPED_SLASHES 
110 2
		| JSON_NUMERIC_CHECK 
111 2
		| JSON_FORCE_OBJECT 
112 2
	 	| (neon()->isDevMode() ? JSON_PRETTY_PRINT : 0);
113 2
		return Json::encode($properties, $options);
114
	}
115
116 44
	public function getClass()
117
	{
118 44
		return get_class($this);
119
	}
120
121
}
122