forked from jarves/jarves
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProperties.php
100 lines (82 loc) · 1.78 KB
/
Properties.php
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
<?php
/**
* This file is part of Jarves.
*
* (c) Marc J. Schmidt <marc@marcjschmidt.de>
*
* J.A.R.V.E.S - Just A Rather Very Easy [content management] System.
*
* http://jarves.io
*
* To get the full copyright and license information, please view the
* LICENSE file, that was distributed with this source code.
*/
namespace Jarves;
/**
*
* Properties class to store array information in the database field from type OBJECT.
*
*/
class Properties
{
/**
* @var array
*/
public $data = array();
public function __construct($data)
{
if (is_string($data)) {
$this->data = json_decode($data, true);
} else {
$this->data = $data;
}
}
/**
* Returns the data as array.
*
* @return array
*/
public function toArray()
{
return $this->data;
}
/**
* Gets the value of $path
*
* @param string $path slash delimited string
*
* @return mixed
*/
public function getByPath($path)
{
$path2 = explode('/', $path);
$data = $this->data;
foreach ($path2 as $node) {
if (!isset($data[$node])) {
return false;
}
$data = $data[$node];
}
return $data;
}
/**
* Sets the value of $path
*
* @param string $path slash delimited string
* @param mixed $data
*/
public function setByPath($path, $data)
{
$path2 = explode('/', $path);
$data2 =& $this->data;
foreach ($path2 as $node) {
if (!$data2[$node]) {
$data2[$node] = array();
}
$data2 =& $data2[$node];
}
if ($data2) {
$data2 = $data;
}
}
}