made some refactoring
This commit is contained in:
33
src/Pattern/SOLID/OpenClosed/Figures/AreaCalculator.php
Normal file
33
src/Pattern/SOLID/OpenClosed/Figures/AreaCalculator.php
Normal 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);
|
||||
}
|
||||
}
|
||||
25
src/Pattern/SOLID/OpenClosed/Figures/Circle.php
Normal file
25
src/Pattern/SOLID/OpenClosed/Figures/Circle.php
Normal 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);
|
||||
}
|
||||
}
|
||||
17
src/Pattern/SOLID/OpenClosed/Figures/ShapeInterface.php
Normal file
17
src/Pattern/SOLID/OpenClosed/Figures/ShapeInterface.php
Normal 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;
|
||||
}
|
||||
25
src/Pattern/SOLID/OpenClosed/Figures/Square.php
Normal file
25
src/Pattern/SOLID/OpenClosed/Figures/Square.php
Normal 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;
|
||||
}
|
||||
}
|
||||
20
src/Pattern/SOLID/OpenClosed/Payments/CashPaymentMethod.php
Normal file
20
src/Pattern/SOLID/OpenClosed/Payments/CashPaymentMethod.php
Normal 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.
|
||||
}
|
||||
}
|
||||
20
src/Pattern/SOLID/OpenClosed/Payments/Checkout.php
Normal file
20
src/Pattern/SOLID/OpenClosed/Payments/Checkout.php
Normal 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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
14
src/Pattern/SOLID/OpenClosed/Payments/Receipt.php
Normal file
14
src/Pattern/SOLID/OpenClosed/Payments/Receipt.php
Normal 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 {}
|
||||
Reference in New Issue
Block a user