This commit is contained in:
2025-07-03 23:46:08 +03:00
parent 7fe2402ff3
commit ab4499a305
39 changed files with 453 additions and 56 deletions

View File

@@ -0,0 +1,46 @@
<?php
/**
* @package: patterns
* @author: Yevhen Odynets
* @date: 2025-07-03
* @time: 21:59
*/
declare(strict_types = 1);
namespace Pattern\Structural\Adapter;
use JsonException;
class JsonReport
{
/**
* @throws JsonException
*/
public function buildJson(): string
{
return json_encode([
[
'name' => 'Mikrotik RB4011iGS+RM',
'price' => 9094,
'count' => 27,
],
[
'name' => 'Cisco ISR4331-VSEC/K9',
'price' => 167955,
'count' => 8,
],
[
'name' => 'TP-Link ER605 (TL-R605)',
'price' => 2499,
'count' => 895,
],
[
'name' => 'D-Link DSL-2500U/BRU/D',
'price' => 420,
'count' => 112,
],
], JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
}
}

View File

@@ -0,0 +1,30 @@
<?php
/**
* @package: patterns
* @author: Yevhen Odynets
* @date: 2025-07-03
* @time: 22:38
*/
declare(strict_types = 1);
namespace Pattern\Structural\Adapter;
use JsonException;
final readonly class JsonToPHPArrayReportAdapter implements PHPArrayReportInterfaceAdapter
{
public function __construct(private JsonReport $report) {}
/**
* @throws JsonException
*/
public function getData(): array
{
$data = $this->report->buildJson();
return json_decode($data, true, 512, JSON_THROW_ON_ERROR);
}
}

View File

@@ -0,0 +1,36 @@
<?php
/**
* @package: patterns
* @author: Yevhen Odynets
* @date: 2025-07-03
* @time: 21:47
*/
declare(strict_types = 1);
namespace Pattern\Structural\Adapter;
class PHPArrayReport implements PHPArrayReportInterfaceAdapter
{
public function getData(): array
{
return [
[
'name' => 'USB HDD Transcend StoreJet25M3 2 TB Iron Gray (TS2TSJ25M3S)',
'price' => 4589,
'count' => 536,
],
[
'name' => 'HDD Seagate IronWolf 4 TB (ST4000VN006)',
'price' => 5069,
'count' => 346,
],
[
'name' => 'HDD WD Purple 4 TB (WD43PURZ)',
'price' => 4109,
'count' => 142,
],
];
}
}

View File

@@ -0,0 +1,17 @@
<?php
/**
* @package: patterns
* @author: Yevhen Odynets
* @date: 2025-07-03
* @time: 22:37
*/
declare(strict_types = 1);
namespace Pattern\Structural\Adapter;
interface PHPArrayReportInterfaceAdapter
{
public function getData(): array;
}

View File

@@ -0,0 +1,46 @@
<?php
/**
* @package: patterns
* @author: Yevhen Odynets
* @date: 2025-07-03
* @time: 22:07
*/
declare(strict_types = 1);
namespace Pattern\Structural\Adapter;
class SerializedReport
{
public function getData(): string
{
return serialize([
[
'name' => 'Makita 4329',
'price' => 3479,
'count' => 395,
],
[
'name' => 'Bosch PST 650 (06033A0721)',
'price' => 2103,
'count' => 983,
],
[
'name' => 'Dnipro-M JS-65LX (98609000)',
'price' => 1920,
'count' => 674,
],
[
'name' => 'RZTK AJ 650 (252143876)',
'price' => 699,
'count' => 1265,
],
[
'name' => 'AEG STEP 70 (4935412900)',
'price' => 3950,
'count' => 765,
],
]);
}
}

View File

@@ -0,0 +1,23 @@
<?php
/**
* @package: patterns
* @author: Yevhen Odynets
* @date: 2025-07-03
* @time: 22:38
*/
declare(strict_types = 1);
namespace Pattern\Structural\Adapter;
final readonly class SerializedToPHPArrayReportAdapter implements PHPArrayReportInterfaceAdapter
{
public function __construct(private SerializedReport $report) {}
public function getData(): array
{
return unserialize($this->report->getData(), ["allowed_classes" => false]);
}
}

View File

@@ -0,0 +1,37 @@
<?php
/**
* @package: patterns
* @author: Yevhen Odynets
* @date: 2025-07-03
* @time: 22:26
*/
declare(strict_types = 1);
namespace Pattern\Structural\Adapter;
class XMLReport
{
public function buildXML(): string
{
return '<?xml version="1.0" encoding="UTF-8"?>
<root>
<element0>
<name>Ibanez RG421EX</name>
<price>21278</price>
<count>129</count>
</element0>
<element1>
<name>Fender Debut Stratocaster HSS Dakota Red</name>
<price>8400</price>
<count>289</count>
</element1>
<element2>
<name>Harley Benton ST-20 BK Standard Series</name>
<price>5190</price>
<count>389</count>
</element2>
</root>';
}
}

View File

@@ -0,0 +1,32 @@
<?php
/**
* @package: patterns
* @author: Yevhen Odynets
* @date: 2025-07-03
* @time: 22:38
*/
declare(strict_types = 1);
namespace Pattern\Structural\Adapter;
use JsonException;
final readonly class XMLToPHPArrayReportAdapter implements PHPArrayReportInterfaceAdapter
{
public function __construct(private XMLReport $report) {}
/**
* @throws JsonException
*/
public function getData(): array
{
$xmlData = $this->report->buildXML();
$xml = simplexml_load_string($xmlData);
$json = json_encode($xml, JSON_THROW_ON_ERROR);
return json_decode($json, true, 512, JSON_THROW_ON_ERROR);
}
}

View File

@@ -0,0 +1,28 @@
<?php
/**
* @package: patterns
* @author: Yevhen Odynets
* @date: 2025-07-03
* @time: 21:59
*/
declare(strict_types = 1);
namespace Pattern\Structural\Adapter;
use JsonException;
class YamlReport
{
public function emitYaml(): string
{
return yaml_emit([
[
'name' => 'Apple iPhone 16 Pro Max 256GB Natural Titanium (MYWY3)',
'price' => 62799,
'count' => 2127,
]
], YAML_UTF8_ENCODING);
}
}

View File

@@ -0,0 +1,23 @@
<?php
/**
* @package: patterns
* @author: Yevhen Odynets
* @date: 2025-07-03
* @time: 22:38
*/
declare(strict_types = 1);
namespace Pattern\Structural\Adapter;
final readonly class YamlToPHPArrayReportAdapter implements PHPArrayReportInterfaceAdapter
{
public function __construct(private YamlReport $report) {}
public function getData(): array
{
return yaml_parse($this->report->emitYaml());
}
}