Aggregate

@param array $options
array columns
array where
array groupby
array pk
@return array
173376
public static function aggregateStatic(array $options) : array {
		$query = self::queryBuilderStatic()->select();
		if (!empty($options['columns'])) {
			$query->columns($options['columns']);
		}
		if (!empty($options['where'])) {
			$query->whereMultiple('AND', $options['where']);
		}
		if (!empty($options['groupby'])) {
			$query->groupby($options['groupby']);
		}
		$result = $query->query($options['pk'] ?? null);
		return $result['rows'];
	}
1174363
public function getList(array $options = array())
    {
        $options += array('index' => 'product_bundle_id');

        $result = &gplcart_static(gplcart_array_hash(array('product.bundle.list' => $options)));

        if (isset($result)) {
            return $result;
        }

        $this->hook->attach('product.bundle.list.before', $options, $result, $this);

        if (isset($result)) {
            return $result;
        }

        $conditions = array();

        $sql = 'SELECT pb.*, p.store_id
                FROM product_bundle pb
                LEFT JOIN product p ON(pb.product_id = p.product_id)
                LEFT JOIN product p2 ON(pb.item_product_id = p2.product_id)';

        if (isset($options['product_bundle_id'])) {
            $sql .= ' WHERE pb.product_bundle_id = ?';
            $conditions[] = $options['product_bundle_id'];
        } else {
            $sql .= ' WHERE pb.product_bundle_id IS NOT NULL';
        }

        if (isset($options['store_id'])) {
            $sql .= ' AND p.store_id = ? AND p2.store_id = ?';
            $conditions[] = $options['store_id'];
            $conditions[] = $options['store_id'];
        }

        if (isset($options['status'])) {
            $sql .= ' AND p.status = ? AND p2.status = ?';
            $conditions[] = (int) $options['status'];
            $conditions[] = (int) $options['status'];
        }

        if (isset($options['product_id'])) {
            $sql .= ' AND pb.product_id = ?';
            $conditions[] = $options['product_id'];
        }

        if (!empty($options['limit'])) {
            $sql .= ' LIMIT ' . implode(',', array_map('intval', $options['limit']));
        }

        $result = $this->db->fetchAll($sql, $conditions, array('index' => $options['index']));
        $this->hook->attach('product.bundle.list.after', $options, $result, $this);
        return $result;
    }
670181
public function getCountForPagination($columns = ['*'])
    {
        $this->backupFieldsForCount();

        $this->aggregate = ['function' => 'count', 'columns' => $this->clearSelectAliases($columns)];

        $results = $this->get();

        $this->aggregate = null;

        $this->restoreFieldsForCount();
        if (isset($this->groups)) {
            return count($results);
        }

        if (! isset($results[0])) {
            return 0;
        }

        $item = $results[0];

        if (is_object($item)) {
            return (int) $item->aggregate;
        }

        return (int) array_change_key_case((array) $item)['aggregate'];
    }
102558
protected function _groupResult($functionName, $alias, $options = [])
    {
        $builder = clone $this;

        if ($options['column']) {
            $groupColumn = $options['column'];
        } else {
            $groupColumn = '*';
        }

        if ($distinct = $options['distinct']) {
            $columns = $functionName . '(DISTINCT ' . $groupColumn . ') AS ' . $alias;
        } else {
            if ($groups = $builder->getGroupBy()) {
                if (!is_array($groups)) {
                    $groups = [$groups];
                }
                $columns = implode(', ', $groups) . ', ' . $functionName . '(' . $groupColumn . ') AS ' . $alias;
            } else {
                $columns = $functionName . '(' . $groupColumn . ') AS ' . $alias;
            }
        }
        $builder->columns($columns);
        $builder->orderBy(null);

        $query = $builder->getQuery();
        $resultset = $query->execute();

        if ($builder->getGroupBy()) {
            return $resultset;
        }

        $firstRow = $resultset->getFirst();

        return $firstRow->{$alias};
    }
414124
public static function getMapArrayFromFlatArray(array $options): array
    {
        $keyValueMap = [];
        $valueFromKeyNode = -1;
        foreach($options as $key => $value) {
            $isKeyNode = (($key % 2) == 0);
            if ($isKeyNode) {
                $valueFromKeyNode = $value;
            } else {
                if($valueFromKeyNode == -1) {
                    throw new \UnexpectedValueException('No optionValue before count value');
                }
                //we have a countNode
                $keyValueMap[$valueFromKeyNode] = $value;
            }
        }

        return $keyValueMap;
    }
291050
protected function buildArguments(array $options = array())
    {
        $arguments = array();

        foreach ($options as $key => $value) {
            // Validate value and format it
            if ($value === false or is_int($key)) {
                continue;
            }

            $arguments[] = "--$key";

            if (is_array($value)) {
                $arguments = array_merge($arguments, $this->buildArrayArguments($value));
            } elseif ($value !== true) {
                $arguments[] = $value;
            }
        }

        return $arguments;
    }
473583
private static function parseOptions(array $options)
    {
        $optionValues = array_filter($options, function ($key) {
            return $key % 2;
        }, ARRAY_FILTER_USE_KEY);

        $optionKeys = array_filter($options, function ($key) {
            return !($key % 2);
        }, ARRAY_FILTER_USE_KEY);

        if (\count($optionKeys) !== \count($optionValues)) {
            throw new \InvalidArgumentException('The options given has to be an even array with key and value.');
        }
        $combined = array_combine($optionKeys, $optionValues);
        if (false === $combined) {
            // returns false, if both are empty (or not equal length, but that's handled above)
            // phpstan complained
            return [];
        }

        return $combined;
    }
810351
public function aggregate(array $pipeline, array $options = array())
    {
        $command = array(
            'aggregate' => $this->getCollectionName(),
            'pipeline' => $pipeline
        );
        $result = $this->command($command, $options);

        return $result['result'];
    }
456640
public function groupBy($array, $columns)
    {
        // ensure $columns parameter is array
        $columns = (array) $columns;

        // get first group-by column
        $col = array_shift($columns);

        $result = array();
        foreach ($array as $row) {
            $key = (string) is_object($row) ? $row->$col : $row[$col];
            if (!isset($result[$key])) {
                $result[$key] = array();
            }

            $result[$key][] = $row;
        }

        // do subsequent group by calls
        if (count($columns)) {
            foreach (array_keys($result) as $key) {
                $result[$key] = $this->groupBy($result[$key], $columns);
            }
        }

        return $result;
    }
119115
public function aggregate($options, $extra)
    {
        list($fun, $field) = $extra;
        $pipeline = [
            ['$match' => (object)$this->parseWhere($options['where'])],
            ['$group' => ['_id' => null, 'aggregate' => ['$' . $fun => '$' . $field]]],
        ];
        $cmd = [
            'aggregate' => $options['table'],
            'allowDiskUse' => true,
            'pipeline' => $pipeline,
        ];

        foreach (['explain', 'collation', 'bypassDocumentValidation', 'readConcern'] as $option) {
            if (isset($options[$option])) {
                $cmd[$option] = $options[$option];
            }
        }
        $command = new Command($cmd);
        $this->log('aggregate', $cmd);
        return $command;
    }
969744
protected function initArrayCallbackOption($options)
    {
        if (!isset($options['callback']) && !isset($options['groups']) && \is_callable($options)) {
            $options = ['callback' => $options];
        }

        return $options;
    }
98773
private function getCollectionOptionsHash(array $options)
    {
        ksort($options);
        $str = '';
        foreach ($options as $option) {
            if (!$option instanceof  Serializable) {
                // All valid Collection options are BSON serializable
                continue;
            }
            $str .= serialize($option->bsonSerialize());
        }

        return md5($str);
    }
319566
public function getActivityCount(array $options = [])
    {
        $query = $this->getActivityCountQuery($options);

        return $this->getCount($query->execute()->fetch());
    }
802132
public function getPopular(array $options = [], array $headers = [])
    {
        return $this->getFactory()->createResultCollection(
            $this->getApi()->getPopular($options, $headers)
        );
    }
282317
public function getGroupList($options=array()){

        $data = array();
        

        $data = array_merge($data, $options);

        return $this->request($this->groupGetlistUrl, $data);
    }
1012603
protected function _dynamicSelectStructure(array $options) : array
    {
        $result = [];
        foreach ($options as $k => $v) {
            $result[$v['label']] = !empty($v['children']) ? $this->_dynamicSelectStructure($v['children']) : [];
        }

        return $result;
    }
255971
public function aggregate($function, $columns = ['*'])
    {
        $results = $this->cloneWithout(['columns'])
            ->cloneWithoutBindings(['select'])
            ->setAggregate($function, $columns)
            ->get($columns);

        if (!$results->isEmpty()) {
            return array_change_key_case((array) $results[0])['aggregate'];
        }
    }