34 lines
613 B
PHP
34 lines
613 B
PHP
<?php
|
|
|
|
/**
|
|
* @package: patterns
|
|
* @author: Yevhen Odynets
|
|
* @date: 2025-07-07
|
|
* @time: 05:28
|
|
*/
|
|
|
|
declare(strict_types = 1);
|
|
|
|
namespace Pattern\SOLID\OpenClosed\Figures;
|
|
|
|
class AreaCalculator
|
|
{
|
|
/**
|
|
* @param array $shapes
|
|
*
|
|
* @return int|float
|
|
*/
|
|
public function calculate(array $shapes, int $round_precision = 2): int|float
|
|
{
|
|
$area = [];
|
|
|
|
foreach ($shapes as $shape) {
|
|
$area[] = $shape->area();
|
|
}
|
|
|
|
$result = array_sum($area);
|
|
|
|
return is_int($result) ? $result : round($result, $round_precision, PHP_ROUND_HALF_UP);
|
|
}
|
|
}
|