Group typed objects by their type

@param Typed[] $objects
@return Typed[][] map indexed by types and lists of objects as values
4917
public static function groupByType(array $objects)
    {
        $perType = [];
        foreach ($objects as $object) {
            $type = $object->getType();
            if (!isset($perType[$type])) {
                $perType[$type] = [];
            }
            $perType[$type][] = $object;
        }
        return $perType;
    }
668543
private function serializeTypes(TypeCollection $types)
    {
        $typesData = [];
        /** @var Type $type */
        foreach ($types as $type) {
            $typesData[] = $type->getType();
        }

        return $typesData;
    }
974453
private function getObjectsFromClasses($classes)
    {
        $objects = array_map(function ($class) {
            try {
                return new $class;
            } catch (\Throwable $ex) {
                print "Can not add command from class $class:\n";
                print $ex->getMessage() . "\n";
            }
        }, $classes);
        $objects = array_filter($objects, function($o){return !is_null($o);} );
        return $objects;
    }
955174
public function fromArray($objects)
    {
        $this->clear();
        foreach ($objects as $objectData) {
            $this->add($objectData);
        }
    }
1022471
protected function map($fields)
    {
        $list = [];
        if (count($this->columns) !== count($fields)) {
            throw new \RuntimeException('Column mismatch on line ' . $this->line);
        }
        foreach ($this->columns as $column) {
            $list[$column] = array_shift($fields);
        }
        return $list;
    }
101324
public function loadFromObjects(array $objects) : IDataTable
    {
        InvalidArgumentException::verifyAllInstanceOf(__METHOD__, 'objects', $objects, $this->definition->getClass()->getClassName());
        $objectProperties = $this->loadObjectProperties($objects);

        $rows = $this->mapObjectsToRows($this->definition, $objectProperties, $objects);

        return new DataTable($this->structure, [new TableSection($this->structure, null, $rows)]);
    }
346210
public function mutate($objects)
	{
		$objects = call_user_func_array(array($this, 'flattenArguments'), func_get_args());
		$collect = [];

		foreach ($objects as $obj) {
			if ($obj instanceof AbstractGenerator) {
				array_push($collect, $obj);
				continue;
			}

			throw new InvalidArgumentException(sprintf('Mutable objects must be instances of %s.', AbstractGenerator::class));
		}
		
		$this->mutates = array_merge(static::objectDiffInArrays($this->mutates, $collect), $collect);
		
		return $this;
	}
733448
private function getObjectStateGroupsIds(array $groups): array
    {
        $groupsIds = array_column($groups, 'id');

        return array_combine($groupsIds, array_fill_keys($groupsIds, false));
    }
591889
public function getList(array $types = [])
    {
        if (!$types || $types == StorageEntity::getTypes()) {
            return $this->getEntityManager()->createQuery('
                SELECT
                    s
                FROM
                    AnimeDbCatalogBundle:Storage s
                ORDER BY
                    s.id DESC
            ')->getResult();
        }

        return $this->getEntityManager()->createQuery('
            SELECT
                s
            FROM
                AnimeDbCatalogBundle:Storage s
            WHERE
                s.type IN (:types)
            ORDER BY
                s.id DESC
        ')
            ->setParameter(':types', $types)
            ->getResult();
    }
1239208
public static function createFromObjects(array $objects = null)
    {
        if ($objects === null) {
            return null;
        }

        $collection = new static();

        foreach ($objects as $v) {
            if (!($v instanceof Contact) && ($v instanceof stdClass)) {
                $part = new Contact();
                $part->updateFromObject($v);
            }

            $collection->add($v);
        }

        return $collection;
    }
403251
private function buildSubjects(array $objects)
    {
        $subjects = [];

        foreach ($objects as $object) {
            $subject = SubjectIdentity::fromObject($object);
            $id = SharingUtils::getCacheId($subject);

            if (!\array_key_exists($id, $this->cacheSubjectSharing)
                    && $this->hasIdentityPermissible()
                    && $this->hasSharingVisibility($subject)
                    && $this->hasSubjectConfig($subject->getType())) {
                $subjects[$id] = $subject;
                $this->cacheSubjectSharing[$id] = false;
            }
        }

        return $subjects;
    }
841989
public static function getTypeMap(...$types): array
    {
        $map = [];
        foreach ($types as $type) {
            $map[$type] = self::getTypeDescription($type);
        }

        return $map;
    }
1363717
public static function groupElementsByValueKeys($array, $groupKeys) {

        if (!is_array($groupKeys)) {
            $groupKeys = array($groupKeys);
        }

        $newArray = new AssociativeArray();
        foreach ($array as $element) {

            $applyObject = $newArray;
            foreach ($groupKeys as $index => $groupKey) {
                if ($index < sizeof($groupKeys) - 1) {
                    $subArray = $applyObject[$element[$groupKeys[$index]]];
                    if (!$subArray) {
                        $subArray = new AssociativeArray();
                        $applyObject[$element[$groupKeys[$index]]] = $subArray;
                    }
                    $applyObject = $subArray;
                } else {
                    if (!is_array($applyObject[$element[$groupKeys[$index]]])) {
                        $applyObject[$element[$groupKeys[$index]]] = array();
                    }
                    $applyObject[$element[$groupKeys[$index]]][] = $element;

                }
            }

        }

        
        return $newArray->toArray();
    }
889486
public function groupBy($fields)
    {
        if (!is_array($fields)) {
            $fields = $this->splitParts($fields);
        }

        foreach ($fields as $k => $field) {
            $fields[$k] = $this->db->quoteColumn($field);
        }

        $this->query['group'] = implode(',', $fields);

        return $this;
    }
479231
public function groupBy(...$keys)
    {
        $data = [];

        while ($row = $this->getNextRow()) {

            $position = &$data;

            foreach ($keys as $key) {
                if (is_callable($key)) {
                    $value = $key($row);
                } else {
                    $value = $row[$key];
                }

                if (!array_key_exists($value, $position)) {
                    $position[$value] = [];
                }
                $position = &$position[$value];
            }

            $position[] = $row;
            unset($position);
        }

        return $data;
    }
241925
public function saveMultiple(array $objects)
    {
        array_walk($objects, [$this, 'assertObjectInstance']);

        return $this->manager
            ->getDriver()
            ->inTransaction(
                function (Driver $driver, array $objects) {
                    return array_map([$this, 'save'], $objects);
                },
                $objects
            );
    }
1307486
public static function map_in_groups($groups = null)
    {
        $groupIDList = array();

        if ($groups instanceof SS_List) {
            foreach ($groups as $group) {
                $groupIDList[] = $group->ID;
            }
        } elseif (is_array($groups)) {
            $groupIDList = $groups;
        } elseif ($groups) {
            $groupIDList[] = $groups;
        }

        // No groups, return all Members
        if (!$groupIDList) {
            return static::get()->sort(array('Surname' => 'ASC', 'FirstName' => 'ASC'))->map();
        }

        $membersList = new ArrayList();
        // This is a bit ineffective, but follow the ORM style
        /** @var Group $group */
        foreach (Group::get()->byIDs($groupIDList) as $group) {
            $membersList->merge($group->Members());
        }

        $membersList->removeDuplicates('ID');

        return $membersList->map();
    }