Skip to content

Commit 7bb9db4

Browse files
Merge pull request #1 from guillermoandrae/0.2.x
0.2.0
2 parents 4b665cf + be07697 commit 7bb9db4

File tree

4 files changed

+89
-6
lines changed

4 files changed

+89
-6
lines changed

src/Common/AbstractAggregate.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,17 @@ final public function __construct(array $items = [])
2525
$this->items = $items;
2626
}
2727

28+
/**
29+
* {@inheritDoc}
30+
*/
2831
final public function has($key): bool
2932
{
3033
return $this->offsetExists($key);
3134
}
3235

36+
/**
37+
* {@inheritDoc}
38+
*/
3339
final public function get($key, $default = null)
3440
{
3541
if ($this->offsetExists($key)) {
@@ -38,36 +44,57 @@ final public function get($key, $default = null)
3844
return $default;
3945
}
4046

47+
/**
48+
* {@inheritDoc}
49+
*/
4150
final public function set($key, $value)
4251
{
4352
$this->offsetSet($key, $value);
4453
}
4554

55+
/**
56+
* {@inheritDoc}
57+
*/
4658
public function remove($key)
4759
{
4860
$this->offsetUnset($key);
4961
}
5062

63+
/**
64+
* {@inheritDoc}
65+
*/
5166
final public function offsetExists($key)
5267
{
5368
return array_key_exists($key, $this->items);
5469
}
5570

71+
/**
72+
* {@inheritDoc}
73+
*/
5674
final public function offsetGet($key)
5775
{
5876
return $this->items[$key];
5977
}
6078

79+
/**
80+
* {@inheritDoc}
81+
*/
6182
final public function offsetSet($key, $value)
6283
{
6384
$this->items[$key] = $value;
6485
}
6586

87+
/**
88+
* {@inheritDoc}
89+
*/
6690
final public function offsetUnset($key)
6791
{
6892
unset($this->items[$key]);
6993
}
7094

95+
/**
96+
* {@inheritDoc}
97+
*/
7198
final public function getIterator(): ArrayIterator
7299
{
73100
return new ArrayIterator($this->items);

src/Common/Collection.php

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,86 +4,134 @@
44

55
final class Collection extends AbstractAggregate implements CollectionInterface
66
{
7+
/**
8+
* {@inheritDoc}
9+
*/
710
public static function make(array $items = []): CollectionInterface
811
{
912
return new static($items);
1013
}
1114

15+
/**
16+
* {@inheritDoc}
17+
*/
1218
public function all(): array
1319
{
1420
return $this->items;
1521
}
1622

23+
/**
24+
* {@inheritDoc}
25+
*/
1726
public function first()
1827
{
1928
return $this->items[0];
2029
}
2130

31+
/**
32+
* {@inheritDoc}
33+
*/
2234
public function last()
2335
{
24-
$array = array_reverse($this->items);
25-
return $array[0];
36+
$count = count($this->items);
37+
return $this->items[$count-1];
2638
}
2739

40+
/**
41+
* {@inheritDoc}
42+
*/
2843
public function random()
2944
{
3045
$key = array_rand($this->items);
3146
return $this->get($key);
3247
}
3348

34-
public function sortBy(string $fieldName): CollectionInterface
49+
/**
50+
* {@inheritDoc}
51+
*/
52+
public function sortBy(string $fieldName, bool $reverse = false): CollectionInterface
3553
{
3654
$results = $this->items;
3755
usort($results, function ($item1, $item2) use ($fieldName) {
3856
return $item1[$fieldName] <=> $item2[$fieldName];
3957
});
58+
if ($reverse) {
59+
$results = array_reverse($results);
60+
}
4061
return new static($results);
4162
}
4263

64+
/**
65+
* {@inheritDoc}
66+
*/
4367
public function count(): int
4468
{
4569
return count($this->items);
4670
}
4771

72+
/**
73+
* {@inheritDoc}
74+
*/
4875
public function isEmpty(): bool
4976
{
5077
return empty($this->items);
5178
}
5279

80+
/**
81+
* {@inheritDoc}
82+
*/
5383
public function shuffle(): CollectionInterface
5484
{
5585
$items = $this->items;
5686
shuffle($items);
5787
return new static($items);
5888
}
5989

90+
/**
91+
* {@inheritDoc}
92+
*/
6093
public function limit(int $offset = 0, int $limit = null): CollectionInterface
6194
{
6295
return new static(array_slice($this->items, $offset, $limit));
6396
}
6497

98+
/**
99+
* {@inheritDoc}
100+
*/
65101
public function filter(callable $callback): CollectionInterface
66102
{
67103
return new static(array_filter($this->items, $callback));
68104
}
69105

106+
/**
107+
* {@inheritDoc}
108+
*/
70109
public function map(callable $callback): CollectionInterface
71110
{
72111
return new static(array_map($callback, $this->items));
73112
}
74113

114+
/**
115+
* {@inheritDoc}
116+
*/
75117
public function toArray(): array
76118
{
77119
return array_map(function ($value) {
78120
return $value instanceof ArrayableInterface ? $value->toArray() : $value;
79121
}, $this->items);
80122
}
81123

124+
/**
125+
* {@inheritDoc}
126+
*/
82127
public function toJson(): string
83128
{
84129
return json_encode($this->toArray());
85130
}
86131

132+
/**
133+
* {@inheritDoc}
134+
*/
87135
public function __toString(): string
88136
{
89137
return $this->toJson();

src/Common/CollectionInterface.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,14 @@ public function shuffle(): CollectionInterface;
7272
public function limit(int $offset = 0, int $limit = null): CollectionInterface;
7373

7474
/**
75-
* Returns a collection sorted by the provided field name.
75+
* Returns a collection sorted by the provided field name. Optionally returns the
76+
* sorted collection in reverse order.
7677
*
7778
* @param string $fieldName The field name to use when grouping results.
79+
* @param boolean $reverse OPTIONAL Whether or not to return the sort in reverse order.
7880
* @return CollectionInterface
7981
*/
80-
public function sortBy(string $fieldName): CollectionInterface;
82+
public function sortBy(string $fieldName, bool $reverse = false): CollectionInterface;
8183

8284
/**
8385
* Returns a collection filtered by the provided callback.

tests/Common/CollectionTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace Test\Common;
44

5+
use PHPUnit\Framework\TestCase;
56
use Guillermoandrae\Common\Collection;
67
use Guillermoandrae\Common\CollectionInterface;
7-
use PHPUnit\Framework\TestCase;
88

99
class CollectionTest extends TestCase
1010
{
@@ -108,6 +108,12 @@ public function testSortBy()
108108
$this->assertTrue($sorted->first()['age'] < $sorted->last()['age']);
109109
}
110110

111+
public function testSortByReverse()
112+
{
113+
$sorted = $this->collection->sortBy('age', true);
114+
$this->assertTrue($sorted->first()['age'] > $sorted->last()['age']);
115+
}
116+
111117
public function testFilter()
112118
{
113119
$collection = new Collection([1, 2, 3]);

0 commit comments

Comments
 (0)