src/ObjectMapper.php
public function mapJson($json, $targetClass)
    {
        // Check if the JSON is valid
        if (!is_array($data = json_decode($json, true))) {
            throw new InvalidJsonException();
        }

        // Pre initialize the result
        $result = null;

        // Check if the target object is a collection of type X
        if (substr($targetClass, -2) == '[]') {
            $result = [];
            foreach ($data as $key => $entryData) {
                // Map the data recursive
                $result[] = $this->mapDataToObject($entryData, substr($targetClass, 0, -2));
            }
        } else {
            // Map the data recursive
            $result = $this->mapDataToObject($data, $targetClass);
        }

        return $result;
    }
public function mapJson($json, $targetClass)
    {
        // Check if the JSON is valid
        if (!is_array($data = json_decode($json, true))) {
            throw new InvalidJsonException();
        }

        // Pre initialize the result
        $result = null;

        // Check if the target object is a collection of type X
        if (substr($targetClass, -2) == '[]') {
            $result = [];
            foreach ($data as $key => $entryData) {
                // Map the data recursive
                $result[] = $this->mapDataToObject($entryData, substr($targetClass, 0, -2));
            }
        } else {
            // Map the data recursive
            $result = $this->mapDataToObject($data, $targetClass);
        }

        return $result;
    }
public function mapJson ( $ json , $ targetClass ) { // Check if the JSON is valid if ( ! is_array ( $ data = json_decode ( $ json , true ) ) ) { throw new InvalidJsonException ( ) ; } // Pre initialize the result $ result = null ; // Check if the target object is a collection of type X if ( substr ( $ targetClass , - 2 ) == '[]' ) { $ result = [ ] ; foreach ( $ data as $ key => $ entryData ) { // Map the data recursive $ result [ ] = $ this -> mapDataToObject ( $ entryData , substr ( $ targetClass , 0 , - 2 ) ) ; } } else { // Map the data recursive $ result = $ this -> mapDataToObject ( $ data , $ targetClass ) ; } return $ result ; }
Maps a JSON string to a object

@param string $json The JSON string
@param string $targetClass The target object class

@return mixed The mapped object

@throws InvalidJsonException If the JSON is not valid
@throws ClassNotFoundException If the target class does not exist
@throws PropertyNotAccessibleException If the class property has no public access an no set-Method
@throws TypeMismatchException If The type in the JSON does not match the type in the class
@throws \ReflectionException If the target class does not exist
Maps a JSON string to a object