P粉0869937882023-09-06 18:46:06
If you want to implement a polymorphic approach, it's better to create a repository or something just to manage that logic.
This is an example.
class SampleRepository { /** * repository instance value * * @var string[] | null */ private $sampleArray; // maybe here is SEASON or EXPIRY or null /** * constructor * * @param string[] | null $sampleArray */ public function __construct($sampleArray) { $this->sampleArray = $sampleArray; } /** * execute like class interface role * * @return array */ public function execute() { return (!$this->sampleArray) ? [] : $this->getResult(); } /** * get result * * @return array */ private function getResult() { // maybe pattern will be better to manage another class or trait. $pattern = [ "SEASON" => new Season(), "EXPIRY" => new Expiry() ]; return collect($this->sampleArray)->map(function($itemKey){ $requestClass = data_get($pattern,$itemKey); if (!$requestClass){ // here is space you don't expect class or canIt find correct class return ["something wrong"]; } return $requestClass->execute(); })->flatten(); } }
You can call it like this.
$sampleRepository = new SampleRepository($sampleValue); // expect string[] or null like ["SEASON"],["SEASON","EXPIRY"],null $result = $sampleRepository->execute(); // [string] or [string,string] or []
This method only works with your parameter specified values. If the return results of the Season class and the Expiry class are almost the same, it is best to manage them on traits. (i.e. $pattern in the example code)
Try some.
I read the comments, so follow..
For example, it prefers to only get the result of getResult(). Therefore, certain patterns and so much logic should not be written on getResult();
If you use traits, here is an example. First, you need to create the management behavior class.
behavior.php
<?php namespace App\Repositories; class Behavior { use Behavior\BehaviorTrait; // if you need to add another pattern, you can add trait here. }
Then, you need to create a Behavior directory at the same level. When you move this directory, you create such a signature file.
<?php namespace App\Repositories\Behavior; trait BehaviorTrait { public static function findAccessibleClass(string $itemKey) { return data_get([ "SEASON" => new Season(), "EXPIRY" => new Expiry() ],$itemKey); } }
findAccessibleClass() method is responsible for finding the correct class.
Then, you can call this method like this.
private function getResult() { return collect($this->sampleArray)->map(function($itemKey){ $requestClass = Behavior::findAccessibleClass($itemKey); // fix here. if (!$requestClass){ // here is space you don't expect class or canIt find correct class return ["something wrong"]; } return $requestClass->execute(); })->flatten(); }
If there is too much code in getResult(), it is best to separate the responsible code.
To create a Behavior Trait, getResult does not need to be responsible for the behavioral logic. In short, it will be easy to test or fix.
hope everything is fine.