Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
80.77% covered (success)
80.77%
21 / 26
50.00% covered (warning)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Collection
80.77% covered (success)
80.77%
21 / 26
50.00% covered (warning)
50.00%
1 / 2
16.60
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
80.00% covered (success)
80.00%
20 / 25
0.00% covered (danger)
0.00%
0 / 1
15.57
1<?php
2/**
3 * Pop PHP Framework (http://www.popphp.org/)
4 *
5 * @link       https://github.com/popphp/popphp-framework
6 * @author     Nick Sagona, III <dev@nolainteractive.com>
7 * @copyright  Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
8 * @license    http://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@nolainteractive.com>
24 * @copyright  Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
25 * @license    http://www.popphp.org/license     New BSD License
26 * @version    6.5.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  array $options
45     * @return array
46     */
47    public function toArray(array $options = []): array
48    {
49        $items = $this->data;
50
51        foreach ($items as $key => $value) {
52            if ($value instanceof AbstractRecord) {
53                $items[$key] = $value->toArray();
54                if ($value->hasRelationships()) {
55                    $relationships = $value->getRelationships();
56                    foreach ($relationships as $name => $relationship) {
57                        $items[$key][$name] = (is_object($relationship) && method_exists($relationship, 'toArray')) ?
58                            $relationship->toArray() : $relationship;
59                    }
60                }
61            }
62        }
63
64        if (!empty($options)) {
65            if (array_key_exists('column', $options) && !empty($options['column'])) {
66                // return simple array of one column
67                $items = array_column($items, $options['column']);
68            } else if (array_key_exists('key', $options)) {
69                if (array_key_exists('isUnique', $options) && $options['isUnique'] == true) {
70                    // return associative array sorted by unique column
71                    $items = array_reduce($items, function($accumulator, $item) use ($options) {
72                        $accumulator[$item[$options['key']]] = $item;
73                        return $accumulator;
74                    });
75                } else {
76                    // return associative array of arrays sorted by non-unique column
77                    $items = array_reduce($items, function($accumulator, $item) use ($options, $items) {
78                        $accumulator[$item[$options['key']]][] = $item;
79                        return $accumulator;
80                    });
81                }
82            }
83        }
84
85        if ($items === null) {
86            $items = [];
87        }
88
89        return $items;
90    }
91
92}