Factory Method

This commit is contained in:
2025-07-03 19:35:54 +03:00
parent 91f8ded888
commit 0f4618edf5
24 changed files with 409 additions and 31 deletions

View File

@@ -0,0 +1,21 @@
<?php
/**
* @package: patterns
* @author: Yevhen Odynets
* @date: 2025-07-03
* @time: 12:52
*/
declare(strict_types = 1);
namespace Pattern\Creational\FactoryMethod;
class CashPayment implements PaymentInterface
{
public function pay(Order $order): void
{
/** @noinspection ForgottenDebugOutputInspection */
dump("Cash payment success, amount: {$order->getSum()}");
}
}

View File

@@ -0,0 +1,20 @@
<?php
/**
* @package: patterns
* @author: Yevhen Odynets
* @date: 2025-07-03
* @time: 17:55
*/
declare(strict_types = 1);
namespace Pattern\Creational\FactoryMethod;
class CashPaymentFactory implements PaymentFactoryInterface
{
public static function createPayment(): PaymentInterface
{
return new CashPayment();
}
}

View File

@@ -0,0 +1,21 @@
<?php
/**
* @package: patterns
* @author: Yevhen Odynets
* @date: 2025-07-03
* @time: 19:22
*/
declare(strict_types = 1);
namespace Pattern\Creational\FactoryMethod;
class IngPayment implements PaymentInterface
{
public function pay(Order $order): void
{
/** @noinspection ForgottenDebugOutputInspection */
dump("ING Bank payment success, amount: {$order->getSum()}");
}
}

View File

@@ -0,0 +1,20 @@
<?php
/**
* @package: patterns
* @author: Yevhen Odynets
* @date: 2025-07-03
* @time: 18:01
*/
declare(strict_types = 1);
namespace Pattern\Creational\FactoryMethod;
class IngPaymentFactory implements PaymentFactoryInterface
{
public static function createPayment(): PaymentInterface
{
return new IngPayment();
}
}

View File

@@ -0,0 +1,22 @@
<?php
/**
* @package: patterns
* @author: Yevhen Odynets
* @date: 2025-07-03
* @time: 12:08
*/
declare(strict_types = 1);
namespace Pattern\Creational\FactoryMethod;
final readonly class Order
{
public function __construct(private float $sum) {}
public function getSum(): float
{
return $this->sum;
}
}

View File

@@ -0,0 +1,21 @@
<?php
/**
* @package: patterns
* @author: Yevhen Odynets
* @date: 2025-07-03
* @time: 19:21
*/
declare(strict_types = 1);
namespace Pattern\Creational\FactoryMethod;
class OtpPayment implements PaymentInterface
{
public function pay(Order $order): void
{
/** @noinspection ForgottenDebugOutputInspection */
dump("Otp Bank payment success, amount: {$order->getSum()}");
}
}

View File

@@ -0,0 +1,20 @@
<?php
/**
* @package: patterns
* @author: Yevhen Odynets
* @date: 2025-07-03
* @time: 18:02
*/
declare(strict_types = 1);
namespace Pattern\Creational\FactoryMethod;
class OtpPaymentFactory implements PaymentFactoryInterface
{
public static function createPayment(): PaymentInterface
{
return new OtpPayment();
}
}

View File

@@ -0,0 +1,18 @@
<?php
/**
* @package: patterns
* @author: Yevhen Odynets
* @date: 2025-07-03
* @time: 14:17
*/
//phpcs:ignore
declare(strict_types = 1);
namespace Pattern\Creational\FactoryMethod;
interface PaymentFactoryInterface
{
public static function createPayment(): PaymentInterface;
}

View File

@@ -0,0 +1,34 @@
<?php
/**
* @package: patterns
* @author: Yevhen Odynets
* @date: 2025-07-03
* @time: 18:04
*/
declare(strict_types = 1);
namespace Pattern\Creational\FactoryMethod;
use RuntimeException;
class PaymentHelper
{
/**
* @param string $paymentType
*
* @return PaymentFactoryInterface
*/
public static function getPaymentFactory(string $paymentType): PaymentFactoryInterface
{
return match ($paymentType) {
'privat' => new PrivatPaymentFactory(),
'raiffeisen' => new RaiffeisenPaymentFactory(),
'ing' => new IngPaymentFactory(),
'otp' => new OtpPaymentFactory(),
'cash' => new CashPaymentFactory(),
default => throw new RuntimeException("Payment type '{$paymentType}' is unknown")
};
}
}

View File

@@ -0,0 +1,17 @@
<?php
/**
* @package: patterns
* @author: Yevhen Odynets
* @date: 2025-07-03
* @time: 16:57
*/
declare(strict_types = 1);
namespace Pattern\Creational\FactoryMethod;
interface PaymentInterface
{
public function pay(Order $order): void;
}

View File

@@ -0,0 +1,21 @@
<?php
/**
* @package: patterns
* @author: Yevhen Odynets
* @date: 2025-07-03
* @time: 19:18
*/
declare(strict_types = 1);
namespace Pattern\Creational\FactoryMethod;
class PrivatPayment implements PaymentInterface
{
public function pay(Order $order): void
{
/** @noinspection ForgottenDebugOutputInspection */
dump("Privatbank payment success, amount: {$order->getSum()}");
}
}

View File

@@ -0,0 +1,20 @@
<?php
/**
* @package: patterns
* @author: Yevhen Odynets
* @date: 2025-07-03
* @time: 17:57
*/
declare(strict_types = 1);
namespace Pattern\Creational\FactoryMethod;
class PrivatPaymentFactory implements PaymentFactoryInterface
{
public static function createPayment(): PaymentInterface
{
return new PrivatPayment();
}
}

View File

@@ -0,0 +1,21 @@
<?php
/**
* @package: patterns
* @author: Yevhen Odynets
* @date: 2025-07-03
* @time: 19:12
*/
declare(strict_types = 1);
namespace Pattern\Creational\FactoryMethod;
class RaiffeisenPayment implements PaymentInterface
{
public function pay(Order $order): void
{
/** @noinspection ForgottenDebugOutputInspection */
dump("Raiffeisen Bank payment success, amount: {$order->getSum()}");
}
}

View File

@@ -0,0 +1,20 @@
<?php
/**
* @package: patterns
* @author: Yevhen Odynets
* @date: 2025-07-03
* @time: 17:59
*/
declare(strict_types = 1);
namespace Pattern\Creational\FactoryMethod;
class RaiffeisenPaymentFactory implements PaymentFactoryInterface
{
public static function createPayment(): PaymentInterface
{
return new RaiffeisenPayment();
}
}