/var/www/(Del)gepgroup.hk/php-activerecord/lib/Serialization.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
<?php
/**
 * @package ActiveRecord
 */
namespace ActiveRecord;
use 
XmlWriter;

/**
 * Base class for Model serializers.
 *
 * All serializers support the following options:
 *
 * <ul>
 * <li><b>only:</b> a string or array of attributes to be included.</li>
 * <li><b>except:</b> a string or array of attributes to be excluded.</li>
 * <li><b>methods:</b> a string or array of methods to invoke. The method's name will be used as a key for the final attributes array
 * along with the method's returned value</li>
 * <li><b>include:</b> a string or array of associated models to include in the final serialized product.</li>
 * <li><b>only_method:</b> a method that's called and only the resulting array is serialized
 * <li><b>skip_instruct:</b> set to true to skip the <?xml ...?> declaration.</li>
 * </ul>
 *
 * Example usage:
 *
 * <code>
 * # include the attributes id and name
 * # run $model->encoded_description() and include its return value
 * # include the comments association
 * # include posts association with its own options (nested)
 * $model->to_json(array(
 *   'only' => array('id','name', 'encoded_description'),
 *   'methods' => array('encoded_description'),
 *   'include' => array('comments', 'posts' => array('only' => 'id'))
 * ));
 *
 * # except the password field from being included
 * $model->to_xml(array('except' => 'password')));
 * </code>
 *
 * @package ActiveRecord
 * @link http://www.phpactiverecord.org/guides/utilities#topic-serialization
 */
abstract class Serialization
{
    protected 
$model;
    protected 
$options;
    protected 
$attributes;

    
/**
     * The default format to serialize DateTime objects to.
     *
     * @see DateTime
     */
    
public static $DATETIME_FORMAT 'iso8601';

    
/**
     * Set this to true if the serializer needs to create a nested array keyed
     * on the name of the included classes such as for xml serialization.
     *
     * Setting this to true will produce the following attributes array when
     * the include option was used:
     *
     * <code>
     * $user = array('id' => 1, 'name' => 'Tito',
     *   'permissions' => array(
     *     'permission' => array(
     *       array('id' => 100, 'name' => 'admin'),
     *       array('id' => 101, 'name' => 'normal')
     *     )
     *   )
     * );
     * </code>
     *
     * Setting to false will produce this:
     *
     * <code>
     * $user = array('id' => 1, 'name' => 'Tito',
     *   'permissions' => array(
     *     array('id' => 100, 'name' => 'admin'),
     *     array('id' => 101, 'name' => 'normal')
     *   )
     * );
     * </code>
     *
     * @var boolean
     */
    
protected $includes_with_class_name_element false;

    
/**
     * Constructs a {@link Serialization} object.
     *
     * @param Model $model The model to serialize
     * @param array &$options Options for serialization
     * @return Serialization
     */
    
public function __construct(Model $model, &$options)
    {
        
$this->model $model;
        
$this->options $options;
        
$this->attributes $model->attributes();
        
$this->parse_options();
    }

    private function 
parse_options()
    {
        
$this->check_only();
        
$this->check_except();
        
$this->check_methods();
        
$this->check_include();
        
$this->check_only_method();        
    }

    private function 
check_only()
    {
        if (isset(
$this->options['only']))
        {
            
$this->options_to_a('only');

            
$exclude array_diff(array_keys($this->attributes),$this->options['only']);
            
$this->attributes array_diff_key($this->attributes,array_flip($exclude));
        }
    }

    private function 
check_except()
    {
        if (isset(
$this->options['except']) && !isset($this->options['only']))
        {
            
$this->options_to_a('except');
            
$this->attributes array_diff_key($this->attributes,array_flip($this->options['except']));
        }
    }

    private function 
check_methods()
    {
        if (isset(
$this->options['methods']))
        {
            
$this->options_to_a('methods');

            foreach (
$this->options['methods'] as $method)
            {
                if (
method_exists($this->model$method))
                    
$this->attributes[$method] = $this->model->$method();
            }
        }
    }
    
    private function 
check_only_method()
    {
        if (isset(
$this->options['only_method']))
        {
            
$method $this->options['only_method'];
            if (
method_exists($this->model$method))
                
$this->attributes $this->model->$method();
        }
    }

    private function 
check_include()
    {
        if (isset(
$this->options['include']))
        {
            
$this->options_to_a('include');

            
$serializer_class get_class($this);

            foreach (
$this->options['include'] as $association => $options)
            {
                if (!
is_array($options))
                {
                    
$association $options;
                    
$options = array();
                }

                try {
                    
$assoc $this->model->$association;

                    if (!
is_array($assoc))
                    {
                        
$serialized = new $serializer_class($assoc$options);
                        
$this->attributes[$association] = $serialized->to_a();;
                    }
                    else
                    {
                        
$includes = array();

                        foreach (
$assoc as $a)
                        {
                            
$serialized = new $serializer_class($a$options);

                            if (
$this->includes_with_class_name_element)
                                
$includes[strtolower(get_class($a))][] = $serialized->to_a();
                            else
                                
$includes[] = $serialized->to_a();
                        }

                        
$this->attributes[$association] = $includes;
                    }

                } catch (
UndefinedPropertyException $e) {
                    ;
//move along
                
}
            }
        }
    }

    final protected function 
options_to_a($key)
    {
        if (!
is_array($this->options[$key]))
            
$this->options[$key] = array($this->options[$key]);
    }

    
/**
     * Returns the attributes array.
     * @return array
     */
    
final public function to_a()
    {
        foreach (
$this->attributes as &$value)
        {
            if (
$value instanceof \DateTime)
                
$value $value->format(self::$DATETIME_FORMAT);
        }
        return 
$this->attributes;
    }

    
/**
     * Returns the serialized object as a string.
     * @see to_s
     * @return string
     */
    
final public function __toString()
    {
        return 
$this->to_s();
    }

    
/**
     * Performs the serialization.
     * @return string
     */
    
abstract public function to_s();
};

/**
 * Array serializer.
 *
 * @package ActiveRecord
 */
class ArraySerializer extends Serialization
{
    public static 
$include_root false;

    public function 
to_s()
    {
        return 
self::$include_root ? array(strtolower(get_class($this->model)) => $this->to_a()) : $this->to_a();
    }
}

/**
 * JSON serializer.
 *
 * @package ActiveRecord
 */
class JsonSerializer extends ArraySerializer
{
    public static 
$include_root false;

    public function 
to_s()
    {
        
parent::$include_root self::$include_root;
        return 
json_encode(parent::to_s());
    }
}

/**
 * XML serializer.
 *
 * @package ActiveRecord
 */
class XmlSerializer extends Serialization
{
    private 
$writer;

    public function 
__construct(Model $model, &$options)
    {
        
$this->includes_with_class_name_element true;
        
parent::__construct($model,$options);
    }

    public function 
to_s()
    {
        return 
$this->xml_encode();
    }

    private function 
xml_encode()
    {
        
$this->writer = new XmlWriter();
        
$this->writer->openMemory();
        
$this->writer->startDocument('1.0''UTF-8');
        
$this->writer->startElement(strtolower(denamespace(($this->model))));
        
$this->write($this->to_a());
        
$this->writer->endElement();
        
$this->writer->endDocument();
        
$xml $this->writer->outputMemory(true);

        if (@
$this->options['skip_instruct'] == true)
            
$xml preg_replace('/<\?xml version.*?\?>/','',$xml);

        return 
$xml;
    }

    private function 
write($data$tag=null)
    {
        foreach (
$data as $attr => $value)
        {
            if (
$tag != null)
                
$attr $tag;

            if (
is_array($value) || is_object($value))
            {
                if (!
is_int(key($value)))
                {
                    
$this->writer->startElement($attr);
                    
$this->write($value);
                    
$this->writer->endElement();
                }
                else
                    
$this->write($value$attr);

                continue;
            }

            
$this->writer->writeElement($attr$value);
        }
    }
}

/**
 * CSV serializer.
 *
 * @package ActiveRecord
 */
class CsvSerializer extends Serialization
{
  public static 
$delimiter ',';
  public static 
$enclosure '"';

  public function 
to_s()
  {
    if (@
$this->options['only_header'] == true) return $this->header();
    return 
$this->row();
  }

  private function 
header()
  {
    return 
$this->to_csv(array_keys($this->to_a()));
  }

  private function 
row()
  {
    return 
$this->to_csv($this->to_a());
  }

  private function 
to_csv($arr)
  {
    
$outstream fopen('php://temp''w');
    
fputcsv($outstream$arrself::$delimiterself::$enclosure);
    
rewind($outstream);
    
$buffer trim(stream_get_contents($outstream));
    
fclose($outstream);
    return 
$buffer;
  }
}
?>