| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Metricool\Http\Metricool\Traits; |
| 6 |
|
| 7 |
trait IsSummable |
| 8 |
{ |
| 9 |
/** |
| 10 |
* Abstract method to be implemented by classes using this trait. It should |
| 11 |
* return an array of data. When {@see sum()} is called, this method will |
| 12 |
* be used to retrieve the data to sum. |
| 13 |
*/ |
| 14 |
abstract public function get(): array; |
| 15 |
|
| 16 |
/** |
| 17 |
* Method just returns the sum of the items in the array returned by the |
| 18 |
* get() method. Operation type methods should always return an array. |
| 19 |
*/ |
| 20 |
public function sum(): array |
| 21 |
{ |
| 22 |
$numericValues = array_filter($this->get(), 'is_numeric'); |
| 23 |
return [ |
| 24 |
array_sum($numericValues), |
| 25 |
]; |
| 26 |
} |
| 27 |
} |
| 28 |
|