Collection   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 159
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 16
dl 0
loc 159
ccs 32
cts 32
cp 1
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A last() 0 3 2
A valid() 0 3 1
A rewind() 0 3 1
A next() 0 3 1
A toArray() 0 18 4
A first() 0 3 2
A key() 0 3 1
A __construct() 0 3 1
A size() 0 3 1
A current() 0 3 1
A toJson() 0 3 1
1
<?php
2
3
namespace Trucker\Responses;
4
5
/**
6
 * Collection class returned from CollectionFinder when
7
 * a colleciton of results is requested.
8
 *
9
 * @author Alessandro Manno <[email protected]>
10
 */
11
class Collection implements \Iterator
12
{
13
    /**
14
     * Var to hold the actual source array collection.
15
     *
16
     * @var array
17
     */
18
    private $collection;
19
20
    /**
21
     * Associative array of metadata related to the
22
     * collection.
23
     *
24
     * @var array
25
     */
26
    public $metaData = [];
27
28
    /**
29
     * Constructor for the collection.
30
     *
31
     * @param array $givenArray array of objects
32
     */
33 14
    public function __construct($givenArray)
34
    {
35 14
        $this->collection = $givenArray;
36 14
    }
37
38
    /**
39
     * Function to conform with Iterator interface.
40
     *
41
     * @see  Iterator
42
     *
43
     * @return \Trucker\Resource\Model
44
     */
45 1
    public function rewind()
46
    {
47 1
        return reset($this->collection);
48
    }
49
50
    /**
51
     * Function to conform with Iterator interface.
52
     *
53
     * @see  Iterator
54
     *
55
     * @return \Trucker\Resource\Model
56
     */
57 1
    public function current()
58
    {
59 1
        return current($this->collection);
60
    }
61
62
    /**
63
     * Function to conform with Iterator interface.
64
     *
65
     * @see  Iterator
66
     *
67
     * @return \Trucker\Resource\Model
68
     */
69 1
    public function key()
70
    {
71 1
        return key($this->collection);
0 ignored issues
show
Bug Best Practice introduced by
The expression return key($this->collection) also could return the type integer|string which is incompatible with the documented return type Trucker\Resource\Model.
Loading history...
72
    }
73
74
    /**
75
     * Function to conform with Iterator interface.
76
     *
77
     * @see  Iterator
78
     *
79
     * @return \Trucker\Resource\Model
80
     */
81 3
    public function next()
82
    {
83 3
        return next($this->collection);
84
    }
85
86
    /**
87
     * Function to conform with Iterator interface.
88
     *
89
     * @see  Iterator
90
     *
91
     * @return bool
92
     */
93 1
    public function valid()
94
    {
95 1
        return null !== key($this->collection);
96
    }
97
98
    /**
99
     * Function to return the size of the collection.
100
     *
101
     * @return int size of collection
102
     */
103 5
    public function size()
104
    {
105 5
        return count($this->collection);
106
    }
107
108
    /**
109
     * Function to return the first item of the collection.
110
     *
111
     * @return \Trucker\Resource\Model
112
     */
113 5
    public function first()
114
    {
115 5
        return empty($this->collection) ? null : $this->collection[0];
116
    }
117
118
    /**
119
     * Function to return the last item of the collection.
120
     *
121
     * @return \Trucker\Resource\Model
122
     */
123 1
    public function last()
124
    {
125 1
        return empty($this->collection) ? null : $this->collection[count($this->collection) - 1];
126
    }
127
128
    /**
129
     * Function to convert the collection to an array using
130
     * each collection elements attributes.
131
     *
132
     * @param string $collectionKey
133
     * @param string $metaKey
134
     *
135
     * @return array
136
     */
137 3
    public function toArray($collectionKey = null, $metaKey = 'meta')
138
    {
139 3
        $entities = [];
140 3
        foreach ($this->collection as $entity) {
141 3
            $entities[] = $entity->attributes();
142
        }
143
144 3
        $col = $entities;
145 3
        if ($collectionKey) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $collectionKey of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
146 3
            $col = [$collectionKey => $entities];
147
        }
148
149 3
        $met = [];
150 3
        if ($this->metaData) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->metaData of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
151 2
            $met = [$metaKey => $this->metaData];
152
        }
153
154 3
        return array_merge($col, $met);
155
    }
156
157
    /**
158
     * Function to convert the collection to json using
159
     * each collection elements attributes as an array then
160
     * encoding the array to json.
161
     *
162
     * @param string $collectionKey
163
     * @param string $metaKey
164
     *
165
     * @return array
166
     */
167 1
    public function toJson($collectionKey = null, $metaKey = 'meta')
168
    {
169 1
        return json_encode($this->toArray($collectionKey, $metaKey));
0 ignored issues
show
Bug Best Practice introduced by
The expression return json_encode($this...llectionKey, $metaKey)) returns the type string which is incompatible with the documented return type array.
Loading history...
170
    }
171
}//end class
172