Completed
Push — master ( a22879...811e69 )
by Alexander
02:12
created

Document::toArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * Ember Db - An embeddable document database for php.
4
 * Copyright (C) 2016 Alexander During
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18
 *
19
 * @link      http://github.com/alexanderduring/php-ember-db
20
 * @copyright Copyright (C) 2016 Alexander During
21
 * @license   http://www.gnu.org/licenses GNU General Public License v3.0
22
 */
23
24
namespace EmberDb;
25
26
class Document
27
{
28
    private $data;
29
30
31
32
    /**
33
     * @param array $data
34
     */
35
    public function __construct($data = array())
36
    {
37
        $this->data = $data;
38
    }
39
40
41
42
    /**
43
     * @param string $path 'comma.separated.path.to.value'
44
     * @return boolean
45
     */
46
    public function has($path)
47
    {
48
        $exists = true;
49
50
        $subtree = $this->data;
51
        $segments = $this->getPathSegments($path);
52
53
        foreach ($segments as $key) {
54
            if (!array_key_exists($key, $subtree)) {
55
                $exists = false;
56
            } else {
57
                $subtree = $subtree[$key];
58
            }
59
        }
60
61
        return $exists;
62
    }
63
64
65
66
    /**
67
     * @param string $path 'comma.separated.path.to.value'
68
     * @return array|mixed
69
     */
70
    public function get($path)
71
    {
72
        $subtree = $this->data;
73
        $segments = $this->getPathSegments($path);
74
75
        foreach ($segments as $key) {
76
            $subtree = $subtree[$key];
77
        }
78
79
        return $subtree;
80
    }
81
82
83
84
    /**
85
     * This method allows you to set a value in the document by using this syntax:
86
     *
87
     * set('path.to.level', 'myValue')
88
     *
89
     * ... does nothing else than ...
90
     *
91
     * document[path][to][level] = 'myValue'
92
     *
93
     * Because the path is translated into an array structure in interations
94
     * we need to store the reference to each intermediate array field
95
     * for usage in the next iteration.
96
     *
97
     * @param string $path 'comma.separated.path.to.value'
98
     * @param mixed $value The value to store
99
     * @return boolean
100
     */
101
    public function set($path, $value)
102
    {
103
        $subtree = &$this->data;
104
        $segments = $this->getPathSegments($path);
105
        $lastKey = array_pop($segments);
106
107
        foreach ($segments as $key) {
108
            if (array_key_exists($key, $subtree)) {
109
                if (!is_array($subtree[$key])) {
110
                    return false;
111
                }
112
                $subtree = &$subtree[$key];
113
            } else {
114
                $subtree[$key] = array();
115
                $subtree = &$subtree[$key];
116
            }
117
        }
118
119
        $subtree[$lastKey] = $value;
120
    }
121
122
123
124
    public function toJson()
125
    {
126
        return json_encode($this->data);
127
    }
128
129
130
131
    public function toArray()
132
    {
133
        return $this->data;
134
    }
135
136
137
    /**
138
     * @param string $path
139
     * @return array
140
     */
141
    private function getPathSegments($path)
142
    {
143
        trim($path, '.');
144
        $segments = explode('.', $path);
145
146
        return $segments;
147
    }
148
}
149