lithium\util\Set::combine PHP Method

combine() public static method

Creates an associative array using a $path1 as the path to build its keys, and optionally $path2 as path to get the values. If $path2 is not specified, all values will be initialized to null (useful for Set::merge()). You can optionally group the values by what is obtained when following the path specified in $groupPath.
public static combine ( array $data, mixed $path1 = null, mixed $path2 = null, string $groupPath = null ) : array
$data array Array from where to extract keys and values.
$path1 mixed As an array, or as a dot-delimited string.
$path2 mixed As an array, or as a dot-delimited string.
$groupPath string As an array, or as a dot-delimited string.
return array Combined array.
    public static function combine($data, $path1 = null, $path2 = null, $groupPath = null)
    {
        if (!$data) {
            return array();
        }
        if (is_object($data)) {
            $data = get_object_vars($data);
        }
        if (is_array($path1)) {
            $format = array_shift($path1);
            $keys = static::format($data, $format, $path1);
        } else {
            $keys = static::extract($data, $path1);
        }
        $vals = array();
        if (!empty($path2) && is_array($path2)) {
            $format = array_shift($path2);
            $vals = static::format($data, $format, $path2);
        } elseif (!empty($path2)) {
            $vals = static::extract($data, $path2);
        }
        $valCount = count($vals);
        $count = count($keys);
        for ($i = $valCount; $i < $count; $i++) {
            $vals[$i] = null;
        }
        if ($groupPath) {
            $group = static::extract($data, $groupPath);
            if (!empty($group)) {
                $c = count($keys);
                for ($i = 0; $i < $c; $i++) {
                    if (!isset($group[$i])) {
                        $group[$i] = 0;
                    }
                    if (!isset($out[$group[$i]])) {
                        $out[$group[$i]] = array();
                    }
                    $out[$group[$i]][$keys[$i]] = $vals[$i];
                }
                return $out;
            }
        }
        return array_combine($keys, $vals);
    }

Usage Example

Beispiel #1
0
 public function loadMetadataForClass($className, ClassMetadataInfo $metadata)
 {
     if (!$metadata->reflClass instanceof SchemaReflection) {
         $metadata->reflClass = new SchemaReflection(get_class($metadata));
     }
     $metadata->primaryTable['name'] = $className::meta('source');
     $primaryKey = $className::meta('key');
     $bindings = static::bindings($className);
     $relations = array();
     if (!empty($bindings)) {
         foreach ($bindings as $type => $set) {
             foreach ($set as $key => $relation) {
                 $mapping = array('fetch' => \Doctrine\ORM\Mapping\AssociationMapping::FETCH_EAGER, 'fieldName' => $relation['fieldName'], 'sourceEntity' => $className, 'targetEntity' => $relation['class'], 'mappedBy' => null, 'cascade' => !empty($relation['dependent']) ? array('remove') : array(), 'optional' => $type != 'belongsTo');
                 if (in_array($type, array('hasOne', 'hasMany'))) {
                     $inverse = $type == 'belongsTo';
                     $mapping['joinColumns'][] = array('fieldName' => !$inverse ? $relation['key'] : $relation['fieldName'], 'name' => !$inverse ? $relation['fieldName'] : $relation['key'], 'referencedColumnName' => $relation['class']::meta('key'));
                 }
                 if (in_array($type, array('belongsTo', 'hasOne', 'hasMany'))) {
                     $mapping['mappedBy'] = static::_fieldName($mapping);
                 }
                 $relations[$type][$key] = $mapping;
             }
         }
     }
     $schema = (array) $className::schema();
     $metadata->reflClass->setRelations($relations);
     $metadata->reflClass->setSchema($schema);
     $belongsToFields = !empty($bindings['belongsTo']) ? Set::combine(array_values($bindings['belongsTo']), '/key', '/fieldName') : array();
     foreach ($schema as $field => $column) {
         $mapping = array_merge(array('id' => $field == $primaryKey, 'fieldName' => !empty($belongsToFields[$field]) ? $belongsToFields[$field] : $field, 'columnName' => $field), (array) $column);
         $metadata->mapField($mapping);
         if ($mapping['id']) {
             $metadata->setIdGeneratorType(\Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_AUTO);
         }
     }
     foreach ($relations as $type => $set) {
         foreach ($set as $key => $mapping) {
             $metadata->{static::$_bindingMapping[$type]}($mapping);
             $mapping = $metadata->associationMappings[$mapping['fieldName']];
         }
     }
 }
All Usage Examples Of lithium\util\Set::combine