-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclass.cb_serializer.php
146 lines (134 loc) · 5.02 KB
/
class.cb_serializer.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
/* This file is part of cbserialize.
* Copyright © 2011-2012 stiftung kulturserver.de ggmbh <[email protected]>
*
* 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/>.
*/
/**
* Array serializer for propel objects. It works recursively and only serializes
* the fields given in the description. It can also work with virtual fields
* or virtual "joins" (e.g. CbFilmFilm.CbFilmGenres).
*/
class CbSerializer {
function __call($name, $arguments)
{
$class_name = 'CbSerialize'.CbCaseConverter::camelize($name);
if (!class_exists($class_name)) {
require_once 'class.'.CbCaseConverter::snakeify($class_name).'.php';
}
$class = new ReflectionClass($class_name);
return $class->newInstanceArgs($arguments);
}
/**
* Serializes a PropelObjectCollection.
* @param PropelObjectCollection $collection The objects to be serialized.
* @param array $fields Array of fields to be serialized. Only fields
* given here will be serialized. You can use relations here. In that case
* use the Propel method name without "get" as key and a further array of
* fields as value, e.g.:
* array('Firstname', 'Cb3Taccounts' => array('Account', 'Email'))
* Furthermore, if the second array has only a single member you can
* specify that literally, as string, e.g.:
* array('Firstname', 'Cb3Taccounts' => 'Account')
* @return array Nested array with the given fields as keys and the
* corresponding members of objects from the given collection as values.
*/
static function collectionToArray($collection, $fields)
{
$result = array();
if (!is_array($fields)) $fields = array($fields);
foreach ($collection as $object) {
$result[] = self::objectToArray($object, $fields);
}
return $result;
}
/**
* Same as @see collectionToArray , only with a single Propel object.
* @param BaseObject $object The object to be serialized.
* @param array $fields Fields to be serialized.
* @return array Requested fields from the given object.
*/
static function objectToArray($object, $fields)
{
if (!$object) return array();
$result = array();
if (!is_array($fields)) $fields = array($fields);
foreach ($fields as $name => $value) {
if (is_numeric($name)) {
$name = $value;
$value = null;
}
$children = '';
$to_parent = false;
if (is_object($value)) {
$children = $value->value($object, $name);
$name = $value->name($object, $name);
if (method_exists($value, "toParent")) {
$to_parent = $value->toParent();
}
} else {
$children = self::childrenToArray(self::getObjectMember($object, $name), $value);
}
if (!is_object($children)) { //don't serialize if nothing is specified
if ($to_parent) {
if (is_array($children)) {
$result = array_merge($result, $children);
} else {
$result = $children;
}
} else {
$result[$name] = $children;
}
}
}
return $result;
}
static function childrenToArray($children, $fields)
{
if ($fields) {
if ($children instanceof PropelCollection || is_array($children)) {
return self::collectionToArray($children, $fields);
} else if ($children instanceof BaseObject) {
return self::objectToArray($children, $fields);
}
} else {
return $children;
}
}
static function getObjectMember($object, $name, $args = array())
{
if (!is_array($args)) $args = array($args);
if (false === strpos($name, "::")) {
$method = 'get' . $name;
} else {
$methods = explode("::", $name, 2);
$method = 'get' . $methods[0];
$recursion = $methods[1];
}
$returnValue = call_user_func_array(array($object, $method), $args);
if (is_object($returnValue) && !empty($recursion)) {
$returnValue = self::getObjectMember($returnValue, $recursion, array());
}
return $returnValue;
}
/**
* TODO: implement
* Updates the given object with values from an array.
* @param BaseObject $object Object to be updated.
* @param array $data Data to be inserted.
*/
static function objectFromArray($object, $data)
{
}
}