Skip to content

Commit 4ee1ca0

Browse files
authored
Merge pull request #20 from worksolutions/group-aggregators
Group and Aggregation support
2 parents 34488cc + e593ea5 commit 4ee1ca0

File tree

23 files changed

+2384
-0
lines changed

23 files changed

+2384
-0
lines changed

README.md

Lines changed: 400 additions & 0 deletions
Large diffs are not rendered by default.

doc/README.ru.md

Lines changed: 409 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
/**
3+
* @author Igor Pomiluyko <pomiluyko@worksolutions.ru>
4+
*/
5+
6+
namespace WS\Utils\Collections\Functions\Group\Aggregator;
7+
8+
use WS\Utils\Collections\Functions\ObjectFunctions;
9+
10+
abstract class AbstractFieldAggregator
11+
{
12+
13+
private $fieldName;
14+
15+
public function __construct(string $fieldName)
16+
{
17+
$this->fieldName = $fieldName;
18+
}
19+
20+
protected function getValue($element)
21+
{
22+
return ObjectFunctions::getPropertyValue($element, $this->fieldName);
23+
}
24+
25+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
/**
3+
* @author Igor Pomiluyko <pomiluyko@worksolutions.ru>
4+
*/
5+
6+
namespace WS\Utils\Collections\Functions\Group\Aggregator;
7+
8+
use WS\Utils\Collections\Collection;
9+
use WS\Utils\Collections\HashSet;
10+
11+
class AddToSet extends AbstractFieldAggregator implements Aggregator
12+
{
13+
14+
public function __invoke(Collection $collection)
15+
{
16+
$set = new HashSet();
17+
foreach ($collection as $element) {
18+
$set->add($this->getValue($element));
19+
}
20+
return $set->toArray();
21+
}
22+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
/**
3+
* @author Igor Pomiluyko <pomiluyko@worksolutions.ru>
4+
*/
5+
6+
namespace WS\Utils\Collections\Functions\Group\Aggregator;
7+
8+
use WS\Utils\Collections\Collection;
9+
10+
interface Aggregator
11+
{
12+
public function __invoke(Collection $collection);
13+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
/**
3+
* @author Igor Pomiluyko <pomiluyko@worksolutions.ru>
4+
*/
5+
6+
namespace WS\Utils\Collections\Functions\Group\Aggregator;
7+
8+
use WS\Utils\Collections\Collection;
9+
10+
class Avg extends AbstractFieldAggregator implements Aggregator
11+
{
12+
13+
public function __invoke(Collection $collection)
14+
{
15+
$acc = null;
16+
$cnt = 0;
17+
foreach ($collection as $element) {
18+
$acc += $this->getValue($element);
19+
$cnt++;
20+
}
21+
if (!$cnt) {
22+
return null;
23+
}
24+
return $acc / $cnt;
25+
}
26+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
/**
3+
* @author Igor Pomiluyko <pomiluyko@worksolutions.ru>
4+
*/
5+
6+
namespace WS\Utils\Collections\Functions\Group\Aggregator;
7+
8+
use WS\Utils\Collections\Collection;
9+
10+
class Count implements Aggregator
11+
{
12+
13+
public function __invoke(Collection $collection)
14+
{
15+
return $collection->size();
16+
}
17+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
/**
3+
* @author Igor Pomiluyko <pomiluyko@worksolutions.ru>
4+
*/
5+
6+
namespace WS\Utils\Collections\Functions\Group\Aggregator;
7+
8+
use WS\Utils\Collections\Collection;
9+
10+
class First extends AbstractFieldAggregator implements Aggregator
11+
{
12+
13+
public function __invoke(Collection $collection)
14+
{
15+
if (!$first = $collection->stream()->findFirst()) {
16+
return null;
17+
}
18+
19+
return $this->getValue($first);
20+
}
21+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
/**
3+
* @author Igor Pomiluyko <pomiluyko@worksolutions.ru>
4+
*/
5+
6+
namespace WS\Utils\Collections\Functions\Group\Aggregator;
7+
8+
use WS\Utils\Collections\Collection;
9+
10+
class Last extends AbstractFieldAggregator implements Aggregator
11+
{
12+
13+
public function __invoke(Collection $collection)
14+
{
15+
if (!$last = $collection->stream()->findLast()) {
16+
return null;
17+
}
18+
return $this->getValue($last);
19+
}
20+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
/**
3+
* @author Igor Pomiluyko <pomiluyko@worksolutions.ru>
4+
*/
5+
6+
namespace WS\Utils\Collections\Functions\Group\Aggregator;
7+
8+
use WS\Utils\Collections\Collection;
9+
10+
class Max extends AbstractFieldAggregator implements Aggregator
11+
{
12+
13+
public function __invoke(Collection $collection)
14+
{
15+
$max = null;
16+
foreach ($collection as $element) {
17+
$value = $this->getValue($element);
18+
if ($max === null || $max < $value) {
19+
$max = $value;
20+
}
21+
}
22+
return $max;
23+
}
24+
}

0 commit comments

Comments
 (0)