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