Last active
November 23, 2022 08:54
-
-
Save pchelk1n/1786823c7a351c5b3f909631ba9f0d8f to your computer and use it in GitHub Desktop.
PHP Strategy Pattern
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
..... | |
"autoload": { | |
"psr-4": {"App\\": "src/"} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\YourStrategyResolver\Handlers; | |
interface Handler | |
{ | |
public function can(): bool; | |
public function do(): array; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require __DIR_.'vendor/autoload.php'; | |
use App\YourStrategyResolver\Handlers\YourOneStrategyHandler; | |
use App\YourStrategyResolver\Handlers\YourTwoStrategyHandler; | |
use App\YourStrategyResolver\YourStrategyResolver; | |
$strategyResolver = new YourStrategryResolver(); | |
$strategyResolver->addHandler(new YourOneStrategyHandler()); | |
$strategyResolver->addHandler(new YourTwoStrategyHandler()); | |
// .. add some more handlers | |
// print ['one data'] | |
var_dump($strategyResolver->resolve()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\YourStrategyResolver\Handlers; | |
class YourOneStrategyHandler implements Handler | |
{ | |
public function can(): bool | |
{ | |
// your logic | |
return true; | |
} | |
public function do(): array | |
{ | |
// your logic | |
return ['one data']; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\YourStrategyResolver; | |
use App\YourStrategyResolver\Handlers\Handler; | |
class YourStrategyResolver | |
{ | |
/** | |
* @var Handler[] | |
*/ | |
private $handlers = []; | |
public function addHandler(Handler $handler): void | |
{ | |
$this->handlers[] = $handler; | |
} | |
public function resolve(): ?array | |
{ | |
foreach ($this->handlers as $handler) { | |
if ($handler->can()) { | |
return $handler->do(); | |
} | |
} | |
return null; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\YourStrategyResolver\Handlers; | |
class YourTwoStrategyHandler implements Handler | |
{ | |
public function can(): bool | |
{ | |
// your logic | |
return false; | |
} | |
public function do(): array | |
{ | |
// your logic | |
return ['two data']; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment