-
Notifications
You must be signed in to change notification settings - Fork 1
/
to-serializable.php
74 lines (55 loc) · 2.2 KB
/
to-serializable.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
<?php
function posttypebuilder_camelcase($string){
return preg_replace_callback('/_([a-z])/', function($c){return strtoupper($c[1]);}, $string);
}
function posttypebuilder_to_serializable($object){
if(is_array($object)){
$array = array();
foreach($object as $element){
$array[] = posttypebuilder_to_serializable($element);
}
return $array;
}
if(is_object($object) == false){
if(is_numeric($object)){
return $object * 1;
} else {
return stripslashes_deep($object);
}
}
$serialized_object = new stdClass;
foreach(get_class_vars(get_class($object)) as $property_name => $null_value){
$camel_cased_property_name = posttypebuilder_camelcase($property_name);
$serialized_object->$camel_cased_property_name = posttypebuilder_to_serializable($object->$property_name);
if(substr($property_name, strlen($property_name) - 3) == "_id"){
$property_name = substr($property_name, 0, strlen($property_name) - 3);
$camel_cased_property_name = posttypebuilder_camelcase($property_name);
$serialized_object->$camel_cased_property_name = posttypebuilder_to_serializable($object->$property_name);
} else if(substr($property_name, strlen($property_name) - 4) == "_ids"){
$property_name = substr($property_name, 0, strlen($property_name) - 4) . "s";
$camel_cased_property_name = posttypebuilder_camelcase($property_name);
$serialized_object->$camel_cased_property_name = posttypebuilder_to_serializable($object->$property_name);
}
}
if(isset($object->post_object)){
$serialized_object->id = $object->post_object->ID;
if(isset($serialized_object->name) == false){
$serialized_object->name = $object->post_object->post_title;
}
unset($serialized_object->postObject);
}
$is_post_object = isset($object->ID) && isset($object->post_title);
if($is_post_object){
$serialized_object->id = $object->ID;
$serialized_object->name = $object->post_title;
if($object->post_type == "attachment"){
$serialized_object->url = $object->guid;
$imagesize = wp_get_attachment_image_src($object->ID, "full");
if($imagesize){
$serialized_object->width = $imagesize[1];
$serialized_object->height = $imagesize[2];
}
}
}
return $serialized_object;
}