protected function _reformatContain($associations, $original)
    {
        $result = $original;

        foreach ((array)$associations as $table => $options) {
            $pointer =& $result;
            if (is_int($table)) {
                $table = $options;
                $options = [];
            }

            if ($options instanceof EagerLoadable) {
                $options = $options->asContainArray();
                $table = key($options);
                $options = current($options);
            }

            if (isset($this->_containOptions[$table])) {
                $pointer[$table] = $options;
                continue;
            }

            if (strpos($table, '.')) {
                $path = explode('.', $table);
                $table = array_pop($path);
                foreach ($path as $t) {
                    $pointer += [$t => []];
                    $pointer =& $pointer[$t];
                }
            }

            if (is_array($options)) {
                $options = isset($options['config']) ?
                    $options['config'] + $options['associations'] :
                    $options;
                $options = $this->_reformatContain(
                    $options,
                    isset($pointer[$table]) ? $pointer[$table] : []
                );
            }

            if ($options instanceof Closure) {
                $options = ['queryBuilder' => $options];
            }

            $pointer += [$table => []];

            if (isset($options['queryBuilder'], $pointer[$table]['queryBuilder'])) {
                $first = $pointer[$table]['queryBuilder'];
                $second = $options['queryBuilder'];
                $options['queryBuilder'] = function ($query) use ($first, $second) {
                    return $second($first($query));
                };
            }

            if (!is_array($options)) {
                $options = [$options => []];
            }

            $pointer[$table] = $options + $pointer[$table];
        }

        return $result;
    }
protected function _reformatContain($associations, $original)
    {
        $result = $original;

        foreach ((array)$associations as $table => $options) {
            $pointer =& $result;
            if (is_int($table)) {
                $table = $options;
                $options = [];
            }

            if ($options instanceof EagerLoadable) {
                $options = $options->asContainArray();
                $table = key($options);
                $options = current($options);
            }

            if (isset($this->_containOptions[$table])) {
                $pointer[$table] = $options;
                continue;
            }

            if (strpos($table, '.')) {
                $path = explode('.', $table);
                $table = array_pop($path);
                foreach ($path as $t) {
                    $pointer += [$t => []];
                    $pointer =& $pointer[$t];
                }
            }

            if (is_array($options)) {
                $options = isset($options['config']) ?
                    $options['config'] + $options['associations'] :
                    $options;
                $options = $this->_reformatContain(
                    $options,
                    isset($pointer[$table]) ? $pointer[$table] : []
                );
            }

            if ($options instanceof Closure) {
                $options = ['queryBuilder' => $options];
            }

            $pointer += [$table => []];

            if (isset($options['queryBuilder'], $pointer[$table]['queryBuilder'])) {
                $first = $pointer[$table]['queryBuilder'];
                $second = $options['queryBuilder'];
                $options['queryBuilder'] = function ($query) use ($first, $second) {
                    return $second($first($query));
                };
            }

            if (!is_array($options)) {
                $options = [$options => []];
            }

            $pointer[$table] = $options + $pointer[$table];
        }

        return $result;
    }
protected function _reformatContain ( $ associations , $ original ) { $ result = $ original ; foreach ( ( array ) $ associations as $ table => $ options ) { $ pointer = & $ result ; if ( is_int ( $ table ) ) { $ table = $ options ; $ options = [ ] ; } if ( $ options instanceof EagerLoadable ) { $ options = $ options -> asContainArray ( ) ; $ table = key ( $ options ) ; $ options = current ( $ options ) ; } if ( isset ( $ this -> _containOptions [ $ table ] ) ) { $ pointer [ $ table ] = $ options ; continue ; } if ( strpos ( $ table , '.' ) ) { $ path = explode ( '.' , $ table ) ; $ table = array_pop ( $ path ) ; foreach ( $ path as $ t ) { $ pointer += [ $ t => [ ] ] ; $ pointer = & $ pointer [ $ t ] ; } } if ( is_array ( $ options ) ) { $ options = isset ( $ options [ 'config' ] ) ? $ options [ 'config' ] + $ options [ 'associations' ] : $ options ; $ options = $ this -> _reformatContain ( $ options , isset ( $ pointer [ $ table ] ) ? $ pointer [ $ table ] : [ ] ) ; } if ( $ options instanceof Closure ) { $ options = [ 'queryBuilder' => $ options ] ; } $ pointer += [ $ table => [ ] ] ; if ( isset ( $ options [ 'queryBuilder' ] , $ pointer [ $ table ] [ 'queryBuilder' ] ) ) { $ first = $ pointer [ $ table ] [ 'queryBuilder' ] ; $ second = $ options [ 'queryBuilder' ] ; $ options [ 'queryBuilder' ] = function ( $ query ) use ( $ first , $ second ) { return $ second ( $ first ( $ query ) ) ; } ; } if ( ! is_array ( $ options ) ) { $ options = [ $ options => [ ] ] ; } $ pointer [ $ table ] = $ options + $ pointer [ $ table ] ; } return $ result ; }
Formats the containments array so that associations are always set as keys
in the array. This function merges the original associations array with
the new associations provided

@param array $associations user provided containments array
@param array $original The original containments array to merge
with the new one
@return array
Formats the containments array so that associations are always set as keys in the array . This function merges the original associations array with the new associations provided