Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
75.76% covered (success)
75.76%
25 / 33
50.00% covered (warning)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Collection
75.76% covered (success)
75.76%
25 / 33
50.00% covered (warning)
50.00%
1 / 2
27.28
0.00% covered (danger)
0.00%
0 / 1
 getItems
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 toArray
75.00% covered (success)
75.00%
24 / 32
0.00% covered (danger)
0.00%
0 / 1
26.25
1<?php
2/**
3 * Pop PHP Framework (https://www.popphp.org/)
4 *
5 * @link       https://github.com/popphp/popphp-framework
6 * @author     Nick Sagona, III <dev@noladev.com>
7 * @copyright  Copyright (c) 2009-2026 NOLA Interactive, LLC.
8 * @license    https://www.popphp.org/license     New BSD License
9 */
10
11/**
12 * @namespace
13 */
14namespace Pop\Db\Record;
15
16use Pop\Utils;
17
18/**
19 * Record collection class
20 *
21 * @category   Pop
22 * @package    Pop\Db
23 * @author     Nick Sagona, III <dev@noladev.com>
24 * @copyright  Copyright (c) 2009-2026 NOLA Interactive, LLC.
25 * @license    https://www.popphp.org/license     New BSD License
26 * @version    6.8.0
27 */
28class Collection extends Utils\Collection
29{
30
31    /**
32     * Method to get collection object items
33     *
34     * @return array
35     */
36    public function getItems(): array
37    {
38        return $this->data;
39    }
40
41    /**
42     * Method to get collection object as an array
43     *
44     * @param  mixed $options
45     * @return array
46     */
47    public function toArray(mixed $options = null): array
48    {
49        $items       = $this->data;
50        $primaryKeys = null;
51
52        foreach ($items as $key => $value) {
53            if ($value instanceof AbstractRecord) {
54                if ($primaryKeys === null) {
55                    $primaryKeys = $value->getPrimaryKeys();
56                }
57                $items[$key] = $value->toArray();
58                if ($value->hasRelationships()) {
59                    $relationships = $value->getRelationships();
60                    foreach ($relationships as $name => $relationship) {
61                        $items[$key][$name] = (is_object($relationship) && method_exists($relationship, 'toArray')) ?
62                            $relationship->toArray() : $relationship;
63                    }
64                }
65            }
66        }
67
68        if (!empty($options)) {
69            if (is_array($options)) {
70                if (array_key_exists('column', $options) && !empty($options['column'])) {
71                    // return simple array of one column
72                    $items = array_column($items, $options['column']);
73                } else if (array_key_exists('key', $options)) {
74                    if (array_key_exists('isUnique', $options) && $options['isUnique'] == true) {
75                        // return associative array sorted by unique column
76                        $items = array_reduce($items, function($accumulator, $item) use ($options) {
77                            $accumulator[$item[$options['key']]] = $item;
78                            return $accumulator;
79                        });
80                    } else {
81                        // return associative array of arrays sorted by non-unique column
82                        $items = array_reduce($items, function($accumulator, $item) use ($options, $items) {
83                            $accumulator[$item[$options['key']]][] = $item;
84                            return $accumulator;
85                        });
86                    }
87                }
88            // return array with the primary IDs as the array keys
89            } else if (is_string($options) && !empty($primaryKeys) &&
90                (count($primaryKeys) == 1) && in_array($options, $primaryKeys)) {
91                $items = array_combine(array_column($items, $options), $items);
92            }
93        }
94
95        if ($items === null) {
96            $items = [];
97        }
98
99        return $items;
100    }
101
102}