-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclass.cb_serialize_flatten.php
More file actions
90 lines (84 loc) · 3 KB
/
class.cb_serialize_flatten.php
File metadata and controls
90 lines (84 loc) · 3 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
<?php
/* This file is part of cbserialize.
* Copyright © 2011-2012 stiftung kulturserver.de ggmbh <github@culturebase.org>
*
* cbserialize is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* cbserialize is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with cbserialize. If not, see <http://www.gnu.org/licenses/>.
*/
require_once 'class.cb_serialize_base.php';
/**
* Flatten some levels of arrays in the value, optionally discarding the keys. e.g.:
*
* array("genres" => array(
* array("genre" => "genre_1"),
* array("genre" => "genre_5"),
* array("genre" => "genre_12")
* )
*
* becomes:
*
* array("genres" => array("genre_1", "genre_5", "genre_12"))
*/
class CbSerializeFlatten extends CbSerializeBase {
protected $num_levels;
protected $strip;
const STRIP_NONE = 0;
const STRIP_KEYS = 1;
const STRIP_LAST_LEVEL = 2;
const STRIP_BOTH = 3;
/**
* Create a flattening serializer.
* @param num_levels How many levels of arrays shall be stripped.
* @param strip Strip mode:
* STRIP_KEYS means all array keys are dropped.
* STRIP_LAST_LEVEL means on the last level values are added up instead of
* array-merged. This is useful for values where the last
* array consists of only one value.
*/
function __construct($num_levels = 1, $strip = self::STRIP_KEYS,
$fields = array(), $args = array()) {
parent::__construct($fields, $args);
$this->num_levels = $num_levels;
$this->strip = $strip;
}
function value($object, $name) {
$result = parent::value($object, $name);
for ($level = 0; $level < $this->num_levels; ++$level) {
$old_result = $result;
$do_strip = ($level == $this->num_levels - 1 &&
($this->strip & self::STRIP_LAST_LEVEL) != 0);
$result = $do_strip ? NULL : array();
if ($old_result === NULL) continue;
foreach ($old_result as $key => $obj) {
if ($do_strip) {
if ($result === NULL) {
$result = $obj;
} else {
$result += $obj;
}
} else {
if (is_int($key)) {
if (is_array($obj)) {
$result = array_merge($result,
($this->strip & self::STRIP_KEYS) != 0 ?
array_values($obj) : $obj);
}
} else {
$result[] = $obj;
}
}
}
}
return $result;
}
}