made some refactoring

This commit is contained in:
2025-07-09 05:28:39 +03:00
parent f76eb08fe0
commit 38676bc9fd
69 changed files with 3075 additions and 355 deletions

View File

@@ -0,0 +1,33 @@
<?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);
}
}

View File

@@ -0,0 +1,25 @@
<?php
/**
* @package: patterns
* @author: Yevhen Odynets
* @date: 2025-07-07
* @time: 05:32
*/
declare(strict_types = 1);
namespace Pattern\SOLID\OpenClosed\Figures;
class Circle implements ShapeInterface
{
public function __construct(public int|float $radius) {}
/**
* @return int|float
*/
public function area(): int|float
{
return M_PI * ($this->radius ** 2);
}
}

View File

@@ -0,0 +1,17 @@
<?php
/**
* @package: patterns
* @author: Yevhen Odynets
* @date: 2025-07-07
* @time: 05:55
*/
declare(strict_types = 1);
namespace Pattern\SOLID\OpenClosed\Figures;
interface ShapeInterface
{
public function area(): int|float;
}

View File

@@ -0,0 +1,25 @@
<?php
/**
* @package: patterns
* @author: Yevhen Odynets
* @date: 2025-07-07
* @time: 05:27
*/
declare(strict_types = 1);
namespace Pattern\SOLID\OpenClosed\Figures;
class Square implements ShapeInterface
{
public function __construct(public int|float $width, public int|float $height) {}
/**
* @return int|float
*/
public function area(): int|float
{
return $this->width * $this->height;
}
}

View File

@@ -0,0 +1,20 @@
<?php
/**
* @package: patterns
* @author: Yevhen Odynets
* @date: 2025-07-07
* @time: 06:20
*/
declare(strict_types = 1);
namespace Pattern\SOLID\OpenClosed\Payments;
class CashPaymentMethod implements PaymentMethodInterface
{
public function acceptPayment($receipt)
{
// TODO: Implement acceptPayment() method.
}
}

View File

@@ -0,0 +1,20 @@
<?php
/**
* @package: patterns
* @author: Yevhen Odynets
* @date: 2025-07-07
* @time: 06:19
*/
declare(strict_types = 1);
namespace Pattern\SOLID\OpenClosed\Payments;
class Checkout
{
public function begin(Receipt $receipt, PaymentMethodInterface $payment): void
{
$payment->acceptPayment($receipt);
}
}

View File

@@ -0,0 +1,17 @@
<?php
/**
* @package: patterns
* @author: Yevhen Odynets
* @date: 2025-07-07
* @time: 06:20
*/
declare(strict_types = 1);
namespace Pattern\SOLID\OpenClosed\Payments;
interface PaymentMethodInterface
{
public function acceptPayment($receipt);
}

View File

@@ -0,0 +1,14 @@
<?php
/**
* @package: patterns
* @author: Yevhen Odynets
* @date: 2025-07-07
* @time: 06:24
*/
declare(strict_types = 1);
namespace Pattern\SOLID\OpenClosed\Payments;
class Receipt {}