1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Halfpastfour\PHPChartJS\Delegate; |
4
|
|
|
|
5
|
|
|
use Zend\Json\Expr; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class ArraySerializable |
9
|
|
|
* |
10
|
|
|
* @package Halfpastfour\PHPChartJS\Model |
11
|
|
|
*/ |
12
|
|
|
trait ArraySerializable |
13
|
|
|
{ |
14
|
|
|
<<<<<<< HEAD |
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Will loop through class properties and try and assign their values to an array of data that will be returned. |
17
|
|
|
* |
18
|
|
|
* @return array |
19
|
|
|
* @throws \ReflectionException |
20
|
|
|
*/ |
21
|
|
|
public function getArrayCopy() |
22
|
|
|
{ |
23
|
|
|
$data = []; |
24
|
|
|
$properties = get_object_vars($this); |
25
|
|
|
$reflectionClass = new \Zend_Reflection_Class($this); |
26
|
|
|
foreach ($properties as $property => $value) { |
27
|
|
|
// Skip property if it is not accessible |
28
|
|
|
if (! $reflectionClass->hasProperty($property)) { |
29
|
|
|
continue; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
// Only process properties that aren't null |
33
|
|
|
if (! is_null($value)) { |
34
|
|
|
$object = (is_object($value) && ! $this->$property instanceof Expr); |
35
|
|
|
|
36
|
|
|
// Prepend 'get' to the getter method. |
37
|
|
|
$getter = 'get' . ucfirst($property); |
38
|
|
|
if (! method_exists($this, $getter)) { |
39
|
|
|
// If 'getSomething' doesn't exist, try to use 'isSomething' |
40
|
|
|
$getter = 'is' . ucfirst($property); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
// Finally simply use the property |
44
|
|
|
if (! method_exists($this, $getter)) { |
45
|
|
|
$getter = $property; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
// Abort if none of the above methods exit |
49
|
|
|
if (! method_exists($this, $getter)) { |
50
|
|
|
continue; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
// Assign the contents of the property to the data array |
54
|
|
|
$data[$property] = $object ? $this->$getter()->getArrayCopy() : $this->$getter(); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $data; |
59
|
|
|
} |
60
|
|
|
<<<<<<< HEAD |
61
|
|
|
} |
62
|
|
|
======= |
63
|
|
|
/** |
64
|
|
|
* Will loop through class properties and try and assign their values to an array of data that will be returned. |
65
|
|
|
* |
66
|
|
|
* @return array |
67
|
|
|
*/ |
68
|
|
|
public function getArrayCopy() |
69
|
|
|
{ |
70
|
|
|
$data = []; |
71
|
|
|
$properties = get_object_vars( $this ); |
72
|
|
|
$reflectionClass = new \Zend_Reflection_Class( $this ); |
73
|
|
|
foreach( $properties as $property => $value ) { |
74
|
|
|
// Skip property if it is not accessible |
75
|
|
|
if( !$reflectionClass->hasProperty( $property ) ) continue; |
76
|
|
|
|
77
|
|
|
// Only process properties that aren't null |
78
|
|
|
if( !is_null( $value ) ) { |
79
|
|
|
$object = ( is_object( $value ) && !$this->$property instanceof Expr ); |
80
|
|
|
|
81
|
|
|
// Prepend 'get' to the getter method. |
82
|
|
|
$getter = 'get' . ucfirst( $property ); |
83
|
|
|
if ( !method_exists($this, $getter) ) { |
84
|
|
|
// If 'getSomething' doesn't exist, try to use 'isSomething' |
85
|
|
|
$getter = 'is' . ucfirst( $property ); |
86
|
|
|
} |
87
|
|
|
// Finally simply use the property |
88
|
|
|
if( !method_exists($this, $getter) ) { |
89
|
|
|
$getter = $property; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
// Abort if none of the above methods exit |
93
|
|
|
if( !method_exists( $this, $getter ) ) continue; |
94
|
|
|
|
95
|
|
|
// Assign the contents of the property to the data array |
96
|
|
|
$data[ $property ] = $object ? $this->$getter()->getArrayCopy() : $this->$getter(); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $data; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
>>>>>>> 6d743af... Simplified getArrayCopy method |
104
|
|
|
======= |
105
|
|
|
} |
106
|
|
|
>>>>>>> e69d8b3... Add unit test making sure the ArraySerializable trait is working well. |
107
|
|
|
|