src/Helper/SerializationHelper.php
private static function toArray($object)
    {
        $json = array();
        $props = self::getProperties($object);
        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 (!method_exists($object, $getter)) {
                continue;
            }
            $key = self::toSnakeCase($key);
            $value = $object->$getter();
            if (self::isNull($value)) {
                continue;
            }
            if ($value instanceof \Iterator) {
                $value = iterator_to_array($value);
            }
            if (is_object($value)) {
                $json[$key] = self::toArray($value);
            } else {
                if (is_array($value)) {
                    $json[$key] = array();
                    if (ArrayHelper::isAssoc($value)) {
                        foreach ($value as $k => $anObject) {
                            if (is_object($anObject)) {
                                $json[$key][$k] = self::toArray($anObject);
                            } else {
                                $json[$key][$k] = $anObject;
                            }
                        }
                    } else {
                        foreach ($value as $anObject) {
                            if (is_object($anObject)) {
                                $json[$key][] = self::toArray($anObject);
                            } else {
                                $json[$key][] = $anObject;
                            }
                        }
                    }
                } else {
                    $json[$key] = $value;
                }
            }
        }

        return $json;
    }
private static function toArray($object)
    {
        $json = array();
        $props = self::getProperties($object);
        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 (!method_exists($object, $getter)) {
                continue;
            }
            $key = self::toSnakeCase($key);
            $value = $object->$getter();
            if (self::isNull($value)) {
                continue;
            }
            if ($value instanceof \Iterator) {
                $value = iterator_to_array($value);
            }
            if (is_object($value)) {
                $json[$key] = self::toArray($value);
            } else {
                if (is_array($value)) {
                    $json[$key] = array();
                    if (ArrayHelper::isAssoc($value)) {
                        foreach ($value as $k => $anObject) {
                            if (is_object($anObject)) {
                                $json[$key][$k] = self::toArray($anObject);
                            } else {
                                $json[$key][$k] = $anObject;
                            }
                        }
                    } else {
                        foreach ($value as $anObject) {
                            if (is_object($anObject)) {
                                $json[$key][] = self::toArray($anObject);
                            } else {
                                $json[$key][] = $anObject;
                            }
                        }
                    }
                } else {
                    $json[$key] = $value;
                }
            }
        }

        return $json;
    }
private static function toArray ( $ object ) { $ json = array ( ) ; $ props = self :: getProperties ( $ object ) ; 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 ( ! method_exists ( $ object , $ getter ) ) { continue ; } $ key = self :: toSnakeCase ( $ key ) ; $ value = $ object -> $ getter ( ) ; if ( self :: isNull ( $ value ) ) { continue ; } if ( $ value instanceof \ Iterator ) { $ value = iterator_to_array ( $ value ) ; } if ( is_object ( $ value ) ) { $ json [ $ key ] = self :: toArray ( $ value ) ; } else { if ( is_array ( $ value ) ) { $ json [ $ key ] = array ( ) ; if ( ArrayHelper :: isAssoc ( $ value ) ) { foreach ( $ value as $ k => $ anObject ) { if ( is_object ( $ anObject ) ) { $ json [ $ key ] [ $ k ] = self :: toArray ( $ anObject ) ; } else { $ json [ $ key ] [ $ k ] = $ anObject ; } } } else { foreach ( $ value as $ anObject ) { if ( is_object ( $ anObject ) ) { $ json [ $ key ] [ ] = self :: toArray ( $ anObject ) ; } else { $ json [ $ key ] [ ] = $ anObject ; } } } } else { $ json [ $ key ] = $ value ; } } } return $ json ; }
Serializes the given object to JSON using a snake-case naming convention.
Arrays and objects can both be passed normally.

@param $object
@return array
Serializes the given object to JSON using a snake - case naming convention . Arrays and objects can both be passed normally .