-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSerialize.php
More file actions
55 lines (47 loc) · 1.77 KB
/
Serialize.php
File metadata and controls
55 lines (47 loc) · 1.77 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
<?php
/**
* Created by PhpStorm.
* User: Theophilus Omoregbee <theo4u@ymail.com>
* Date: 10/25/16
* Time: 12:11 AM
* This class serialize php object class to an instance compatible with json ecode
*/
class SerializeMe
{
/**
* This serialize our object to a normal single object
* @param $object
* @param array $option this is used to handle the number of inner iteration
* to load or serialize
* @return array
*/
public static function serialize($object, $option = array("number_of_iteration"=>null, "return"=>"id"))
{
$reflect = new ReflectionClass($object);
$props = $reflect->getDefaultProperties();
foreach ($props as $key => $value) {
$check_references = explode("_", $key);
$getter = "";
if (count($check_references) > 0) {
foreach ($check_references as $reference)
$getter .= ucfirst($reference);
} else
$getter = ucfirst($key);
$getter = "get" . $getter;
if (is_object($object->$getter()))
$props[$key] = SerializeMe::serialize($object->$getter());//continue serialization, till all object is free from serialization
else
if (is_array($object->$getter())) {
$props[$key] = array();
foreach ($object->$getter() as $anObject)
if (is_object($anObject))
$props[$key][] = SerializeMe::serialize($anObject);//continue serialization till we return normal value
else
$props[$key][] = $anObject;
} else
$props[$key] = $object->$getter();
}
return $props;
}
}
?>