forked from ushahidi/platform
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathData.php
More file actions
119 lines (98 loc) · 2.97 KB
/
Copy pathData.php
File metadata and controls
119 lines (98 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
/**
* Ushahidi Platform Data Input for Use Cases
*
* Works very similar to "struct" objects in other languages (eg Ruby),
* but requires that all possible properties are pre-defined, to ensure
* a highly predictable object state.
*
* @author Ushahidi Team <[email protected]>
* @package Ushahidi\Platform
* @copyright 2014 Ushahidi
* @license https://www.gnu.org/licenses/agpl-3.0.html GNU Affero General Public License Version 3 (AGPL3)
*/
namespace Ushahidi\Core;
use Ushahidi\Core\Traits\ArrayExchange;
abstract class Data
{
use ArrayExchange {
asArray as private asArraySimple;
}
/**
* @var Array allowed keys that were in the input data
*/
private static $defined_input_keys = [];
/**
* Stores what (allowed) keys were defined by input data.
* @param Array $data raw input
* @return void
*/
public function __construct(Array $data)
{
// Get the (empty) array that currently exists, this tells us what
// properties are allowed in this object.
$allowed = $this->asArraySimple();
// Using the allowed properties, determine which of them are present
// in the given input data...
$defined = array_keys(array_intersect_key($data, $allowed));
// ... and store those values for delta comparison.
self::$defined_input_keys[$this->getObjectId()] = $defined;
$this->setData($data);
}
/**
* Clears what (allowed) keys were defined by input data.
* @return void
*/
public function __destruct()
{
unset(self::$defined_input_keys[$this->getObjectId()]);
}
/**
* Get the unique identity of the object instance.
* @return string
*/
final private function getObjectId()
{
return spl_object_hash($this);
}
/**
* Get all values in the current object, reducing the result to defined input.
* @return Array
*/
public function asArray()
{
// Get defined properties, flipping the list of keys into an associative array...
$defined = array_flip(self::$defined_input_keys[$this->getObjectId()]);
// ... and use it to reduce the values to what was actually input.
$values = array_intersect_key($this->asArraySimple(), $defined);
return $values;
}
/**
* Compare with some existing data and get the delta between the two.
* Only values that were present in the input data will be returned!
* @param Array $compare existing data
* @return Data
*/
public function getDifferent(Array $compare)
{
// Get the difference of current data and comparison.
// This will allow null values in $compare to be overridden,
// and new values not present in $compare to be added.
$delta = [];
foreach ($this->asArray() as $key => $value) {
$delta_value = $value;
$exists_in_compare = array_key_exists($key, $compare);
if ($delta_value === null && $exists_in_compare) {
$delta_value = $compare[$key];
}
if ($delta_value === null) {
continue;
}
if ($exists_in_compare && $compare[$key] == $delta_value) {
continue;
}
$delta[$key] = $delta_value;
}
return new static($delta);
}
}