src/Communibase/Connector.php
public function getByRef(array $ref, array $parentEntity = [])
    {
        $document = $parentEntity;
        if (strpos($ref['rootDocumentEntityType'], 'parent') === false) {
            if (empty($document['_id']) || $document['_id'] !== $ref['rootDocumentId']) {
                $document = $this->getById($ref['rootDocumentEntityType'], $ref['rootDocumentId']);
            }

            if (count($document) === 0) {
                throw new Exception('Invalid document reference (document cannot be found by Id)');
            }
        }

        $container = $document;
        foreach ($ref['path'] as $pathInDocument) {
            if (!array_key_exists($pathInDocument['field'], $container)) {
                throw new Exception('Could not find the path in document');
            }
            $container = $container[$pathInDocument['field']];
            if (empty($pathInDocument['objectId'])) {
                continue;
            }

            if (!is_array($container)) {
                throw new Exception('Invalid value for path in document');
            }
            $result = array_filter($container, function ($item) use ($pathInDocument) {
                return $item['_id'] === $pathInDocument['objectId'];
            });
            if (count($result) === 0) {
                throw new Exception('Empty result of reference');
            }
            $container = reset($result);
        }
        return $container;
    }
public function getByRef(array $ref, array $parentEntity = [])
    {
        $document = $parentEntity;
        if (strpos($ref['rootDocumentEntityType'], 'parent') === false) {
            if (empty($document['_id']) || $document['_id'] !== $ref['rootDocumentId']) {
                $document = $this->getById($ref['rootDocumentEntityType'], $ref['rootDocumentId']);
            }

            if (count($document) === 0) {
                throw new Exception('Invalid document reference (document cannot be found by Id)');
            }
        }

        $container = $document;
        foreach ($ref['path'] as $pathInDocument) {
            if (!array_key_exists($pathInDocument['field'], $container)) {
                throw new Exception('Could not find the path in document');
            }
            $container = $container[$pathInDocument['field']];
            if (empty($pathInDocument['objectId'])) {
                continue;
            }

            if (!is_array($container)) {
                throw new Exception('Invalid value for path in document');
            }
            $result = array_filter($container, function ($item) use ($pathInDocument) {
                return $item['_id'] === $pathInDocument['objectId'];
            });
            if (count($result) === 0) {
                throw new Exception('Empty result of reference');
            }
            $container = reset($result);
        }
        return $container;
    }
public function getByRef ( array $ ref , array $ parentEntity = [ ] ) { $ document = $ parentEntity ; if ( strpos ( $ ref [ 'rootDocumentEntityType' ] , 'parent' ) === false ) { if ( empty ( $ document [ '_id' ] ) || $ document [ '_id' ] !== $ ref [ 'rootDocumentId' ] ) { $ document = $ this -> getById ( $ ref [ 'rootDocumentEntityType' ] , $ ref [ 'rootDocumentId' ] ) ; } if ( count ( $ document ) === 0 ) { throw new Exception ( 'Invalid document reference (document cannot be found by Id)' ) ; } } $ container = $ document ; foreach ( $ ref [ 'path' ] as $ pathInDocument ) { if ( ! array_key_exists ( $ pathInDocument [ 'field' ] , $ container ) ) { throw new Exception ( 'Could not find the path in document' ) ; } $ container = $ container [ $ pathInDocument [ 'field' ] ] ; if ( empty ( $ pathInDocument [ 'objectId' ] ) ) { continue ; } if ( ! is_array ( $ container ) ) { throw new Exception ( 'Invalid value for path in document' ) ; } $ result = array_filter ( $ container , function ( $ item ) use ( $ pathInDocument ) { return $ item [ '_id' ] === $ pathInDocument [ 'objectId' ] ; } ) ; if ( count ( $ result ) === 0 ) { throw new Exception ( 'Empty result of reference' ) ; } $ container = reset ( $ result ) ; } return $ container ; }
Get a single Entity by a ref-string

@todo if the ref is a parent.parent.parent the code would need further improvement.

@param array $ref
@param array $parentEntity (optional)

@return array the referred Entity data

@throws Exception
Get a single Entity by a ref - string