Factory Method
This commit is contained in:
21
src/Pattern/Creational/FactoryMethod/CashPayment.php
Normal file
21
src/Pattern/Creational/FactoryMethod/CashPayment.php
Normal 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()} ₴");
|
||||
}
|
||||
}
|
||||
20
src/Pattern/Creational/FactoryMethod/CashPaymentFactory.php
Normal file
20
src/Pattern/Creational/FactoryMethod/CashPaymentFactory.php
Normal 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();
|
||||
}
|
||||
}
|
||||
21
src/Pattern/Creational/FactoryMethod/IngPayment.php
Normal file
21
src/Pattern/Creational/FactoryMethod/IngPayment.php
Normal 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()} ₴");
|
||||
}
|
||||
}
|
||||
20
src/Pattern/Creational/FactoryMethod/IngPaymentFactory.php
Normal file
20
src/Pattern/Creational/FactoryMethod/IngPaymentFactory.php
Normal 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();
|
||||
}
|
||||
}
|
||||
22
src/Pattern/Creational/FactoryMethod/Order.php
Normal file
22
src/Pattern/Creational/FactoryMethod/Order.php
Normal 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;
|
||||
}
|
||||
}
|
||||
21
src/Pattern/Creational/FactoryMethod/OtpPayment.php
Normal file
21
src/Pattern/Creational/FactoryMethod/OtpPayment.php
Normal 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()} ₴");
|
||||
}
|
||||
}
|
||||
20
src/Pattern/Creational/FactoryMethod/OtpPaymentFactory.php
Normal file
20
src/Pattern/Creational/FactoryMethod/OtpPaymentFactory.php
Normal 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();
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
34
src/Pattern/Creational/FactoryMethod/PaymentHelper.php
Normal file
34
src/Pattern/Creational/FactoryMethod/PaymentHelper.php
Normal 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")
|
||||
};
|
||||
}
|
||||
}
|
||||
17
src/Pattern/Creational/FactoryMethod/PaymentInterface.php
Normal file
17
src/Pattern/Creational/FactoryMethod/PaymentInterface.php
Normal 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;
|
||||
}
|
||||
21
src/Pattern/Creational/FactoryMethod/PrivatPayment.php
Normal file
21
src/Pattern/Creational/FactoryMethod/PrivatPayment.php
Normal 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()} ₴");
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
21
src/Pattern/Creational/FactoryMethod/RaiffeisenPayment.php
Normal file
21
src/Pattern/Creational/FactoryMethod/RaiffeisenPayment.php
Normal 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()} ₴");
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
17
src/Pattern/Creational/Singleton/Loggable.php
Normal file
17
src/Pattern/Creational/Singleton/Loggable.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package: patterns
|
||||
* @author: Yevhen Odynets
|
||||
* @date: 2025-07-03
|
||||
* @time: 07:36
|
||||
*/
|
||||
|
||||
declare(strict_types = 1);
|
||||
|
||||
namespace Pattern\Creational\Singleton;
|
||||
|
||||
interface Loggable
|
||||
{
|
||||
public static function getInstance(): static;
|
||||
}
|
||||
134
src/Pattern/Creational/Singleton/Logger.php
Normal file
134
src/Pattern/Creational/Singleton/Logger.php
Normal file
@@ -0,0 +1,134 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package: patterns
|
||||
*
|
||||
* @author: Yevhen Odynets
|
||||
*
|
||||
* @date: 2025-07-02
|
||||
*
|
||||
* @time: 22:30
|
||||
*/
|
||||
|
||||
declare(strict_types = 1);
|
||||
|
||||
namespace Pattern\Creational\Singleton;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
use function in_array;
|
||||
use function sprintf;
|
||||
|
||||
use const FILE_APPEND;
|
||||
use const PHP_EOL;
|
||||
|
||||
class Logger implements Loggable
|
||||
{
|
||||
public const string LOG_FILE = '../storage/logs/main.txt';
|
||||
public const array ALLOWED_LOG_TYPES
|
||||
= [
|
||||
'file',
|
||||
'error_log',
|
||||
];
|
||||
private static ?Logger $instance = null;
|
||||
private ?string $logType = null;
|
||||
private ?string $dateFormat = null;
|
||||
private ?string $prefix = null;
|
||||
|
||||
private function __construct() {}
|
||||
|
||||
public static function getInstance(): static
|
||||
{
|
||||
if (self::$instance) {
|
||||
self::$instance = new static();
|
||||
}
|
||||
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
public function getLogType(): string
|
||||
{
|
||||
if (! $this->logType) {
|
||||
throw new RuntimeException('log type is null');
|
||||
}
|
||||
|
||||
return $this->logType;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function setLogType(string $logType): static
|
||||
{
|
||||
if (! in_array($logType, self::ALLOWED_LOG_TYPES)) {
|
||||
throw new RuntimeException('undefined log type: ' . $logType);
|
||||
}
|
||||
|
||||
$this->logType = $logType;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function log(string $data): void
|
||||
{
|
||||
$prefix = $this->getPrefix();
|
||||
|
||||
$message = sprintf(
|
||||
'%s: %s%s',
|
||||
date($this->getDateFormat()),
|
||||
empty($prefix) ? '' : $prefix . ' ',
|
||||
$data . PHP_EOL
|
||||
);
|
||||
|
||||
if ('file' === $this->logType) {
|
||||
file_put_contents(self::LOG_FILE, $message, FILE_APPEND);
|
||||
} elseif ('error_log' === $this->logType) {
|
||||
/* @noinspection ForgottenDebugOutputInspection */
|
||||
error_log($message, 3, self::LOG_FILE);
|
||||
}
|
||||
}
|
||||
|
||||
public function getPrefix(): ?string
|
||||
{
|
||||
return $this->prefix;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function setPrefix(?string $prefix): static
|
||||
{
|
||||
$this->prefix = $prefix;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getDateFormat(): string
|
||||
{
|
||||
if (! $this->dateFormat) {
|
||||
throw new RuntimeException('date format is null');
|
||||
}
|
||||
|
||||
return $this->dateFormat;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function setDateFormat(string $dateFormat): static
|
||||
{
|
||||
$this->dateFormat = $dateFormat;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
private function __clone() {}
|
||||
}
|
||||
|
||||
|
||||
///*$message = 'Message from delivery service';
|
||||
//
|
||||
//$logger = new Logger();
|
||||
//$dateFormat = 'Y-m-d H:i:s' . (preg_match('/^win/i', PHP_OS) ? '' : '.u');
|
||||
//$logger->setDateFormat($dateFormat)->setLogType('error_log');
|
||||
//$logger->log($message);*/
|
||||
@@ -9,10 +9,7 @@
|
||||
|
||||
//phpcs:ignore
|
||||
declare(strict_types = 1);
|
||||
namespace Pattern\Creational\Sub;
|
||||
|
||||
use Contracts\SingleInterface;
|
||||
use Pattern\Creational\Singleton;
|
||||
namespace Pattern\Creational\Singleton;
|
||||
|
||||
class Single extends Singleton implements SingleInterface
|
||||
{
|
||||
17
src/Pattern/Creational/Singleton/SingleInterface.php
Normal file
17
src/Pattern/Creational/Singleton/SingleInterface.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package: patterns
|
||||
* @author: Yevhen Odynets
|
||||
* @date: 2025-07-03
|
||||
* @time: 10:58
|
||||
*/
|
||||
|
||||
declare(strict_types = 1);
|
||||
|
||||
namespace Pattern\Creational\Singleton;
|
||||
|
||||
interface SingleInterface
|
||||
{
|
||||
public function childish(): void;
|
||||
}
|
||||
@@ -9,7 +9,7 @@
|
||||
|
||||
declare(strict_types = 1);
|
||||
|
||||
namespace Pattern\Creational;
|
||||
namespace Pattern\Creational\Singleton;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
Reference in New Issue
Block a user