src/Rehyved/Utilities/Mapper/ObjectMapper.php
public function mapObjectToArray($object, string $prefix = "")
    {
        if (!is_object($object)) {
            return $object;
        }
        if (get_class($object) === \stdClass::class) {
            return (array)$object;
        }

        $objectClass = new \ReflectionClass($object);

        $objectProperties = $this->getObjectProperties($objectClass);

        $array = array();
        foreach ($objectProperties as $property) {
            if ($property->getGetter() == null && !$property->isPublic()) {
                continue;
            }

            $value = $this->getValue($object, $property);
            if ($value === null) {
                continue;
            }

            $propertyName = $property->getName();
            $annotations = $property->getAnnotations();

            $propertyKey = empty($prefix) ? $propertyName : $prefix . self::PATH_DELIMITER . $propertyName;

            if (is_array($value)
                && array_key_exists(TypeValidator::ANNOTATION, $annotations)
                && TypeHelper::isTypedArrayType($annotations[TypeValidator::ANNOTATION])
            ) {
                $items = array();
                foreach ($value as $index => $item) {
                    $items[] = $this->mapObjectToArray($item);
                }
                $array[$propertyKey] = $items;
            } else {
                $array[$propertyKey] = $this->mapObjectToArray($value);
            }
        }

        return $array;
    }
public function mapObjectToArray($object, string $prefix = "")
    {
        if (!is_object($object)) {
            return $object;
        }
        if (get_class($object) === \stdClass::class) {
            return (array)$object;
        }

        $objectClass = new \ReflectionClass($object);

        $objectProperties = $this->getObjectProperties($objectClass);

        $array = array();
        foreach ($objectProperties as $property) {
            if ($property->getGetter() == null && !$property->isPublic()) {
                continue;
            }

            $value = $this->getValue($object, $property);
            if ($value === null) {
                continue;
            }

            $propertyName = $property->getName();
            $annotations = $property->getAnnotations();

            $propertyKey = empty($prefix) ? $propertyName : $prefix . self::PATH_DELIMITER . $propertyName;

            if (is_array($value)
                && array_key_exists(TypeValidator::ANNOTATION, $annotations)
                && TypeHelper::isTypedArrayType($annotations[TypeValidator::ANNOTATION])
            ) {
                $items = array();
                foreach ($value as $index => $item) {
                    $items[] = $this->mapObjectToArray($item);
                }
                $array[$propertyKey] = $items;
            } else {
                $array[$propertyKey] = $this->mapObjectToArray($value);
            }
        }

        return $array;
    }
public function mapObjectToArray ( $ object , string $ prefix = "" ) { if ( ! is_object ( $ object ) ) { return $ object ; } if ( get_class ( $ object ) === \ stdClass :: class ) { return ( array ) $ object ; } $ objectClass = new \ ReflectionClass ( $ object ) ; $ objectProperties = $ this -> getObjectProperties ( $ objectClass ) ; $ array = array ( ) ; foreach ( $ objectProperties as $ property ) { if ( $ property -> getGetter ( ) == null && ! $ property -> isPublic ( ) ) { continue ; } $ value = $ this -> getValue ( $ object , $ property ) ; if ( $ value === null ) { continue ; } $ propertyName = $ property -> getName ( ) ; $ annotations = $ property -> getAnnotations ( ) ; $ propertyKey = empty ( $ prefix ) ? $ propertyName : $ prefix . self :: PATH_DELIMITER . $ propertyName ; if ( is_array ( $ value ) && array_key_exists ( TypeValidator :: ANNOTATION , $ annotations ) && TypeHelper :: isTypedArrayType ( $ annotations [ TypeValidator :: ANNOTATION ] ) ) { $ items = array ( ) ; foreach ( $ value as $ index => $ item ) { $ items [ ] = $ this -> mapObjectToArray ( $ item ) ; } $ array [ $ propertyKey ] = $ items ; } else { $ array [ $ propertyKey ] = $ this -> mapObjectToArray ( $ value ) ; } } return $ array ; }
Maps the provided object to an array or returns the provided object if it was of a built-in primitive type

By using the 'arrayOf' annotation in the object's class for properties of type array the mapper will map these to
arrays recursively.

@param mixed $object The object to map to an array
@param string $prefix with which the array keys should be prefixed i.e. the name of the object/variable passed
@return mixed either an array containing the properties of the object or the provided object if it had a built-in
primitive type
Maps the provided object to an array or returns the provided object if it was of a built - in primitive type