private static function mapDataToEntity($entityName, \stdClass $data)
    {
        $entity = new $entityName();

        if (!($entity instanceof Entity)) {
            throw new \RuntimeException(
                sprintf(
                    'Object "%s" must extend Entity',
                    $entityName
                )
            );
        }

        $foreignKeyEntityMap = $entity->foreignKeyEntityMap();

        $properties = array_keys(get_object_vars($data));

        foreach ($properties as $propertyName) {
            if (!property_exists($entity, $propertyName)) {
                throw new \UnexpectedValueException(
                    sprintf(
                        'Property %s->%s does not exist',
                        get_class($entity),
                        $propertyName
                    )
                );
            }

            if (isset($foreignKeyEntityMap[$propertyName])) {
                $entity->$propertyName = self::mapDataToEntity(
                    $foreignKeyEntityMap[$propertyName],
                    $data->$propertyName
                );
            } elseif (is_string($data->$propertyName) && preg_match('/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/', $data->$propertyName)) {
                $entity->$propertyName = new \DateTime($data->$propertyName);
            } else {
                $entity->$propertyName = json_decode(json_encode($data->$propertyName), true);
            }
        }

        return $entity;
    }
private static function mapDataToEntity($entityName, \stdClass $data)
    {
        $entity = new $entityName();

        if (!($entity instanceof Entity)) {
            throw new \RuntimeException(
                sprintf(
                    'Object "%s" must extend Entity',
                    $entityName
                )
            );
        }

        $foreignKeyEntityMap = $entity->foreignKeyEntityMap();

        $properties = array_keys(get_object_vars($data));

        foreach ($properties as $propertyName) {
            if (!property_exists($entity, $propertyName)) {
                throw new \UnexpectedValueException(
                    sprintf(
                        'Property %s->%s does not exist',
                        get_class($entity),
                        $propertyName
                    )
                );
            }

            if (isset($foreignKeyEntityMap[$propertyName])) {
                $entity->$propertyName = self::mapDataToEntity(
                    $foreignKeyEntityMap[$propertyName],
                    $data->$propertyName
                );
            } elseif (is_string($data->$propertyName) && preg_match('/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/', $data->$propertyName)) {
                $entity->$propertyName = new \DateTime($data->$propertyName);
            } else {
                $entity->$propertyName = json_decode(json_encode($data->$propertyName), true);
            }
        }

        return $entity;
    }
private static function mapDataToEntity ( $ entityName , \ stdClass $ data ) { $ entity = new $ entityName ( ) ; if ( ! ( $ entity instanceof Entity ) ) { throw new \ RuntimeException ( sprintf ( 'Object "%s" must extend Entity' , $ entityName ) ) ; } $ foreignKeyEntityMap = $ entity -> foreignKeyEntityMap ( ) ; $ properties = array_keys ( get_object_vars ( $ data ) ) ; foreach ( $ properties as $ propertyName ) { if ( ! property_exists ( $ entity , $ propertyName ) ) { throw new \ UnexpectedValueException ( sprintf ( 'Property %s->%s does not exist' , get_class ( $ entity ) , $ propertyName ) ) ; } if ( isset ( $ foreignKeyEntityMap [ $ propertyName ] ) ) { $ entity -> $ propertyName = self :: mapDataToEntity ( $ foreignKeyEntityMap [ $ propertyName ] , $ data -> $ propertyName ) ; } elseif ( is_string ( $ data -> $ propertyName ) && preg_match ( '/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/' , $ data -> $ propertyName ) ) { $ entity -> $ propertyName = new \ DateTime ( $ data -> $ propertyName ) ; } else { $ entity -> $ propertyName = json_decode ( json_encode ( $ data -> $ propertyName ) , true ) ; } } return $ entity ; }
Map array of data to an entity
@param mixed $entityName
@param mixed $data
@return Entity
Map array of data to an entity