mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-08-01 12:40:11 +02:00
Merge branch 'main' into refactor
This commit is contained in:
@@ -6,8 +6,12 @@ class WinJsonWriter implements JsonWriter
|
|||||||
{
|
{
|
||||||
public function write(array $data, bool $formatted): string
|
public function write(array $data, bool $formatted): string
|
||||||
{
|
{
|
||||||
|
$options = 0;
|
||||||
|
|
||||||
|
if ($formatted) {
|
||||||
|
$options = JSON_PRETTY_PRINT;
|
||||||
|
}
|
||||||
|
|
||||||
return json_encode($data, JSON_PRETTY_PRINT);
|
return json_encode($data, $options);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -8,13 +8,13 @@ use DesignPatterns\Creational\Builder\Parts\Vehicle;
|
|||||||
|
|
||||||
interface Builder
|
interface Builder
|
||||||
{
|
{
|
||||||
public function createVehicle();
|
public function createVehicle(): void;
|
||||||
|
|
||||||
public function addWheel();
|
public function addWheel(): void;
|
||||||
|
|
||||||
public function addEngine();
|
public function addEngine(): void;
|
||||||
|
|
||||||
public function addDoors();
|
public function addDoors(): void;
|
||||||
|
|
||||||
public function getVehicle(): Vehicle;
|
public function getVehicle(): Vehicle;
|
||||||
}
|
}
|
||||||
|
@@ -14,19 +14,19 @@ class CarBuilder implements Builder
|
|||||||
{
|
{
|
||||||
private Car $car;
|
private Car $car;
|
||||||
|
|
||||||
public function addDoors()
|
public function addDoors(): void
|
||||||
{
|
{
|
||||||
$this->car->setPart('rightDoor', new Door());
|
$this->car->setPart('rightDoor', new Door());
|
||||||
$this->car->setPart('leftDoor', new Door());
|
$this->car->setPart('leftDoor', new Door());
|
||||||
$this->car->setPart('trunkLid', new Door());
|
$this->car->setPart('trunkLid', new Door());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function addEngine()
|
public function addEngine(): void
|
||||||
{
|
{
|
||||||
$this->car->setPart('engine', new Engine());
|
$this->car->setPart('engine', new Engine());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function addWheel()
|
public function addWheel(): void
|
||||||
{
|
{
|
||||||
$this->car->setPart('wheelLF', new Wheel());
|
$this->car->setPart('wheelLF', new Wheel());
|
||||||
$this->car->setPart('wheelRF', new Wheel());
|
$this->car->setPart('wheelRF', new Wheel());
|
||||||
@@ -34,7 +34,7 @@ class CarBuilder implements Builder
|
|||||||
$this->car->setPart('wheelRR', new Wheel());
|
$this->car->setPart('wheelRR', new Wheel());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function createVehicle()
|
public function createVehicle(): void
|
||||||
{
|
{
|
||||||
$this->car = new Car();
|
$this->car = new Car();
|
||||||
}
|
}
|
||||||
|
@@ -14,18 +14,18 @@ class TruckBuilder implements Builder
|
|||||||
{
|
{
|
||||||
private Truck $truck;
|
private Truck $truck;
|
||||||
|
|
||||||
public function addDoors()
|
public function addDoors(): void
|
||||||
{
|
{
|
||||||
$this->truck->setPart('rightDoor', new Door());
|
$this->truck->setPart('rightDoor', new Door());
|
||||||
$this->truck->setPart('leftDoor', new Door());
|
$this->truck->setPart('leftDoor', new Door());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function addEngine()
|
public function addEngine(): void
|
||||||
{
|
{
|
||||||
$this->truck->setPart('truckEngine', new Engine());
|
$this->truck->setPart('truckEngine', new Engine());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function addWheel()
|
public function addWheel(): void
|
||||||
{
|
{
|
||||||
$this->truck->setPart('wheel1', new Wheel());
|
$this->truck->setPart('wheel1', new Wheel());
|
||||||
$this->truck->setPart('wheel2', new Wheel());
|
$this->truck->setPart('wheel2', new Wheel());
|
||||||
@@ -35,7 +35,7 @@ class TruckBuilder implements Builder
|
|||||||
$this->truck->setPart('wheel6', new Wheel());
|
$this->truck->setPart('wheel6', new Wheel());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function createVehicle()
|
public function createVehicle(): void
|
||||||
{
|
{
|
||||||
$this->truck = new Truck();
|
$this->truck = new Truck();
|
||||||
}
|
}
|
||||||
|
@@ -4,14 +4,8 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace DesignPatterns\Creational\Pool;
|
namespace DesignPatterns\Creational\Pool;
|
||||||
|
|
||||||
use DateTime;
|
|
||||||
|
|
||||||
class StringReverseWorker
|
class StringReverseWorker
|
||||||
{
|
{
|
||||||
public function __construct()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public function run(string $text): string
|
public function run(string $text): string
|
||||||
{
|
{
|
||||||
return strrev($text);
|
return strrev($text);
|
||||||
|
@@ -20,7 +20,7 @@ class WorkerPool implements Countable
|
|||||||
|
|
||||||
public function get(): StringReverseWorker
|
public function get(): StringReverseWorker
|
||||||
{
|
{
|
||||||
if (count($this->freeWorkers) == 0) {
|
if (count($this->freeWorkers) === 0) {
|
||||||
$worker = new StringReverseWorker();
|
$worker = new StringReverseWorker();
|
||||||
} else {
|
} else {
|
||||||
$worker = array_pop($this->freeWorkers);
|
$worker = array_pop($this->freeWorkers);
|
||||||
@@ -31,10 +31,9 @@ class WorkerPool implements Countable
|
|||||||
return $worker;
|
return $worker;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function dispose(StringReverseWorker $worker)
|
public function dispose(StringReverseWorker $worker): void
|
||||||
{
|
{
|
||||||
$key = spl_object_hash($worker);
|
$key = spl_object_hash($worker);
|
||||||
|
|
||||||
if (isset($this->occupiedWorkers[$key])) {
|
if (isset($this->occupiedWorkers[$key])) {
|
||||||
unset($this->occupiedWorkers[$key]);
|
unset($this->occupiedWorkers[$key]);
|
||||||
$this->freeWorkers[$key] = $worker;
|
$this->freeWorkers[$key] = $worker;
|
||||||
|
@@ -14,13 +14,10 @@ final class StaticFactory
|
|||||||
{
|
{
|
||||||
public static function factory(string $type): Formatter
|
public static function factory(string $type): Formatter
|
||||||
{
|
{
|
||||||
if ($type == 'number') {
|
return match ($type) {
|
||||||
return new FormatNumber();
|
'number' => new FormatNumber(),
|
||||||
}
|
'string' => new FormatString(),
|
||||||
if ($type == 'string') {
|
default => throw new InvalidArgumentException('Unknown format given'),
|
||||||
return new FormatString();
|
};
|
||||||
}
|
|
||||||
|
|
||||||
throw new InvalidArgumentException('Unknown format given');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user