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();
}
}

View 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;
}

View 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);*/

View File

@@ -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
{

View 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;
}

View File

@@ -9,7 +9,7 @@
declare(strict_types = 1);
namespace Pattern\Creational;
namespace Pattern\Creational\Singleton;
use RuntimeException;