made some refactoring
This commit is contained in:
39
resources/view/patterns/abstract-factory.php
Normal file
39
resources/view/patterns/abstract-factory.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package: patterns
|
||||
* @author: Yevhen Odynets
|
||||
* @date: 2025-07-03
|
||||
* @time: 20:20
|
||||
*/
|
||||
|
||||
declare(strict_types = 1);
|
||||
|
||||
use Pattern\Creational\AbstractFactory\{ParcelSender, ProvidersEnum as PostProvider};
|
||||
|
||||
/**
|
||||
* @param array $parcels
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function doDeliver(array $parcels): void
|
||||
{
|
||||
foreach ($parcels as $parcel) {
|
||||
['provider' => $provider, 'destination_address' => $address] = $parcel;
|
||||
$result = (new ParcelSender($provider, $address))->send();
|
||||
|
||||
echo $result ? "✔️ Sent successfully\n" : "❌ Sending failed\n";
|
||||
}
|
||||
}
|
||||
|
||||
$parcels = [
|
||||
['provider' => PostProvider::NovaPost, 'destination_address' => "02020,\r\nм. Щастя, р-н ХТЗ"],
|
||||
[
|
||||
'provider' => PostProvider::UkrPost,
|
||||
'destination_address' => "Голобородько Семен Юхимович,\r\nn02020, Львівська обл.,\r\nм. Городок, вул. Головна, буд. 1, кв. 50",
|
||||
],
|
||||
['provider' => PostProvider::Meest, 'destination_address' => "Адреса 3"],
|
||||
['provider' => PostProvider::Justin, 'destination_address' => "Відділення 777"],
|
||||
];
|
||||
|
||||
doDeliver($parcels);
|
||||
62
resources/view/patterns/adapter.php
Normal file
62
resources/view/patterns/adapter.php
Normal file
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package: patterns
|
||||
* @author: Yevhen Odynets
|
||||
* @date: 2025-07-03
|
||||
* @time: 21:21
|
||||
*/
|
||||
|
||||
declare(strict_types = 1);
|
||||
|
||||
use Pattern\Structural\Adapter\JsonReport;
|
||||
use Pattern\Structural\Adapter\JsonToPHPArrayReportAdapter;
|
||||
use Pattern\Structural\Adapter\PHPArrayReport;
|
||||
use Pattern\Structural\Adapter\PHPArrayReportInterfaceAdapter;
|
||||
use Pattern\Structural\Adapter\SerializedReport;
|
||||
use Pattern\Structural\Adapter\SerializedToPHPArrayReportAdapter;
|
||||
use Pattern\Structural\Adapter\XMLReport;
|
||||
use Pattern\Structural\Adapter\XMLToPHPArrayReportAdapter;
|
||||
use Pattern\Structural\Adapter\YamlReport;
|
||||
use Pattern\Structural\Adapter\YamlToPHPArrayReportAdapter;
|
||||
|
||||
$reports = [
|
||||
// new YamlReport(),
|
||||
new PHPArrayReport(),
|
||||
new XMLReport(),
|
||||
new SerializedReport(),
|
||||
new JsonReport(),
|
||||
];
|
||||
|
||||
function client(array $reports): void
|
||||
{
|
||||
foreach ($reports as $report) {
|
||||
$adapter = null;
|
||||
|
||||
if ($report instanceof PHPArrayReport) {
|
||||
$adapter = $report;
|
||||
} elseif ($report instanceof JsonReport) {
|
||||
$adapter = new JsonToPHPArrayReportAdapter($report);
|
||||
} elseif ($report instanceof SerializedReport) {
|
||||
$adapter = new SerializedToPHPArrayReportAdapter($report);
|
||||
} elseif ($report instanceof XMLReport) {
|
||||
$adapter = new XMLToPHPArrayReportAdapter($report);
|
||||
} elseif ($report instanceof YamlReport) {
|
||||
$adapter = new YamlToPHPArrayReportAdapter($report);
|
||||
}
|
||||
|
||||
if (! is_null($adapter)) {
|
||||
echo $adapter::class;
|
||||
renderView($adapter);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function renderView(PHPArrayReportInterfaceAdapter $adapter): void
|
||||
{
|
||||
/** @noinspection ForgottenDebugOutputInspection */
|
||||
dump($adapter->getData());
|
||||
}
|
||||
|
||||
?><img class="diagram" src="/assets/img/diagrams/adapter.png" alt="Adapter DEsign Pattern Diagram"><?php
|
||||
client($reports);
|
||||
43
resources/view/patterns/builder.php
Normal file
43
resources/view/patterns/builder.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package: patterns
|
||||
* @author: Yevhen Odynets
|
||||
* @date: 2025-07-04
|
||||
* @time: 00:19
|
||||
*/
|
||||
|
||||
declare(strict_types = 1);
|
||||
|
||||
use Pattern\Creational\Builder\BigMacBuilder;
|
||||
use Pattern\Creational\Builder\Director;
|
||||
|
||||
/**
|
||||
* The client code creates a builder object, passes it to the director and then
|
||||
* initiates the construction process. The end result is retrieved from the
|
||||
* builder object.
|
||||
*
|
||||
* @noinspection ForgottenDebugOutputInspection*/
|
||||
function clientCode(Director $director): void
|
||||
{
|
||||
$builder = new BigMacBuilder();
|
||||
$director->setBuilder($builder);
|
||||
|
||||
echo 'Preparing full featured Big Mac...';
|
||||
$director->regularBigMac();
|
||||
dump($builder->getBigMac()->getIngredients());
|
||||
|
||||
echo 'Preparing vegan Big Mac...';
|
||||
$director->veganBigMac();
|
||||
dump($builder->getBigMac()->getIngredients());
|
||||
|
||||
// The Builder pattern can be used without a Director class.
|
||||
echo 'Preparing a custom snack...';
|
||||
$builder->produceBun();
|
||||
$builder->produceMeat();
|
||||
$builder->produceSauce();
|
||||
dump($builder->getBigMac()->getIngredients());
|
||||
}
|
||||
|
||||
|
||||
clientCode(new Director());
|
||||
17
resources/view/patterns/decorator.php
Normal file
17
resources/view/patterns/decorator.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package: patterns
|
||||
* @author: Yevhen Odynets
|
||||
* @date: 2025-07-04
|
||||
* @time: 10:39
|
||||
*/
|
||||
|
||||
declare(strict_types = 1);
|
||||
|
||||
use Pattern\Structural\Decorator\{BasicInspection, OilChange, TireRotation};
|
||||
|
||||
$service = new TireRotation(new OilChange(new BasicInspection()));
|
||||
|
||||
echo $service->getDescription() . ': $';
|
||||
echo $service->getCost();
|
||||
58
resources/view/patterns/factory-method.php
Normal file
58
resources/view/patterns/factory-method.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package: patterns
|
||||
* @author: Yevhen Odynets
|
||||
* @date: 2025-07-03
|
||||
* @time: 12:16
|
||||
*/
|
||||
|
||||
declare(strict_types = 1);
|
||||
|
||||
use Pattern\Creational\FactoryMethod\{CashlessPayment, CashPayment, Order, PaymentHelper};
|
||||
use Random\RandomException;
|
||||
|
||||
function execute(string $scope): void
|
||||
{
|
||||
echo '<hr/>' . ucfirst($scope) . ' App Payments:<hr/>' . PHP_EOL;
|
||||
|
||||
try {
|
||||
$orderData = [
|
||||
[
|
||||
'order' => new Order(getFloatRange()),
|
||||
'paymentType' => 'privat',
|
||||
],
|
||||
[
|
||||
'order' => new Order(getFloatRange()),
|
||||
'paymentType' => 'raiffeisen',
|
||||
],
|
||||
[
|
||||
'order' => new Order(getFloatRange()),
|
||||
'paymentType' => 'ing',
|
||||
],
|
||||
[
|
||||
'order' => new Order(getFloatRange()),
|
||||
'paymentType' => 'otp',
|
||||
]
|
||||
];
|
||||
|
||||
if ($scope === 'web') {
|
||||
$orderData[] = [
|
||||
'order' => new Order(getFloatRange()),
|
||||
'paymentType' => 'cash',
|
||||
];
|
||||
}
|
||||
|
||||
foreach ($orderData as $orderDataItem) {
|
||||
['order' => $order, 'paymentType' => $type] = $orderDataItem;
|
||||
|
||||
$payment = PaymentHelper::getPaymentFactory($type)->createPayment();
|
||||
$payment->pay($order);
|
||||
}
|
||||
} catch (RandomException $e) {
|
||||
echo $e->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
execute('web');
|
||||
execute('mobile');
|
||||
34
resources/view/patterns/observer.php
Normal file
34
resources/view/patterns/observer.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package: patterns
|
||||
* @author: Yevhen Odynets
|
||||
* @date: 2025-07-06
|
||||
* @time: 15:35
|
||||
*/
|
||||
|
||||
declare(strict_types = 1);
|
||||
|
||||
use Pattern\Behavioral\Observer\User;
|
||||
use Pattern\Behavioral\Observer\UserObserver;
|
||||
|
||||
$user = new User();
|
||||
|
||||
/** These names will be skipped and not be added to the log of names */
|
||||
$user->setName("Just a name");
|
||||
$user->setName("Some new name");
|
||||
|
||||
$userObserver = new UserObserver();
|
||||
$user->attach($userObserver);
|
||||
|
||||
$user->setName("Another new name");
|
||||
$user->setName("Once again new name");
|
||||
$user->setName("Possibly other new name");
|
||||
|
||||
/** @noinspection ForgottenDebugOutputInspection */
|
||||
dump(
|
||||
getenv('APP_ENV'),
|
||||
["Another new name", "Once again new name", "Possibly other new name"],
|
||||
$userObserver->getNamesLog(),
|
||||
["Another new name", "Once again new name", "Possibly other new name"] === $userObserver->getNamesLog()
|
||||
);
|
||||
40
resources/view/patterns/prototype.php
Normal file
40
resources/view/patterns/prototype.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package: patterns
|
||||
* @author: Yevhen Odynets
|
||||
* @date: 2025-07-04
|
||||
* @time: 08:13
|
||||
*/
|
||||
|
||||
declare(strict_types = 1);
|
||||
|
||||
use Pattern\Creational\Prototype\Author;
|
||||
use Pattern\Creational\Prototype\Page;
|
||||
|
||||
function client(): void
|
||||
{
|
||||
$author = new Author("Джордж Орвелл");
|
||||
$page = new Page(
|
||||
"Колгосп тварин",
|
||||
"Притча, сповнена гіркої іронії і сарказму.
|
||||
Трагікомічна історія спільноти тварин, що зважилися позбутися пригноблення людьми,
|
||||
і потрапила під бузувірську владу свиней.",
|
||||
$author
|
||||
);
|
||||
|
||||
// ...
|
||||
|
||||
$page->addComment("Nice book!");
|
||||
|
||||
// ...
|
||||
|
||||
$draft = clone $page;
|
||||
|
||||
echo "Dump of the clone. Note that the author is now referencing two objects.\n\n";
|
||||
|
||||
/** @noinspection ForgottenDebugOutputInspection */
|
||||
dump($draft);
|
||||
}
|
||||
?><img class="diagram" src="/assets/img/diagrams/prototype.png" alt="Adapter DEsign Pattern Diagram"><?php
|
||||
client();
|
||||
49
resources/view/patterns/singleton.php
Normal file
49
resources/view/patterns/singleton.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package: patterns
|
||||
* @author: Yevhen Odynets
|
||||
* @date: 2025-07-03
|
||||
* @time: 08:39
|
||||
*/
|
||||
|
||||
declare(strict_types = 1);
|
||||
|
||||
use Pattern\Creational\{Singleton\Single, Singleton\Singleton};
|
||||
|
||||
/**
|
||||
* The client code.
|
||||
*
|
||||
* @noinspection ForgottenDebugOutputInspection
|
||||
*/
|
||||
function client(): void
|
||||
{
|
||||
$output = [
|
||||
'where' => trace(),
|
||||
's1' => Singleton::getInstance('s1'),
|
||||
's2' => Singleton::getInstance('s2'),
|
||||
];
|
||||
|
||||
if ($output['s1'] === $output['s2']) {
|
||||
$output['message'] = "Singleton works, both variables contain the same instance.";
|
||||
} else {
|
||||
$output['message'] = "Singleton failed, variables contain different instances.";
|
||||
}
|
||||
|
||||
$output['s1']->setValue('value set from s1@Singleton::class');
|
||||
dump($output['s1']->getValue());
|
||||
$output['s2']->setValue('value set from s2@Singleton::class');
|
||||
dump($output, $output['s1']->getValue(), $output['s2']->getValue());
|
||||
}
|
||||
|
||||
?><img class="diagram" src="/assets/img/diagrams/singleton.png" alt="Adapter DEsign Pattern Diagram"><?php
|
||||
client();
|
||||
|
||||
$single = Single::getInstance('from subclass');
|
||||
$single->setValue('value set from Single::class')->childish();
|
||||
|
||||
|
||||
$singleton = Singleton::getInstance('from Singleton');
|
||||
|
||||
/** @noinspection ForgottenDebugOutputInspection */
|
||||
dump($single->getValue(), $singleton->getValue());
|
||||
Reference in New Issue
Block a user