Home > Blockchain >  How I can use Polymorphism to return multiple results and use multi behaviours in the same time
How I can use Polymorphism to return multiple results and use multi behaviours in the same time

Time:12-29

I'm using Laravel 9 and I have a request can contains :

  • Parameter called SEASON the value can be an array or null so SEASON parameter can be an array and can be also null
  • Parameter called EXPIRY can be an array and can be also null

I have two classes one for the SEASON feature and the other class for EXPIRY both they extends from Repository. and both have a method called execute that return an array

abstract class Repository 
{
     abstract public function execute(): array;
}


class Expiry extends Repository
{
   public function execute()
   {
       return ['The Request contain Expiry Parameter, and seasonal behaviours is done'];
   }
}


class Season extends Repository
{
   public function execute()
   {
       return ['The Request contain Season Parameter, and expiry behaviours is done'];
   }
}

I would like to call execute method of Season class if my request contains SEASON, or call the execute method of expiry if my request contains Expiry. OR Call both of them and merge the execute return of execute in one array so I can have as result.

['The Request contain Expiry Parameter, and seasonal behaviours is done', 'The Request contain Expiry Parameter, and expiry behaviours is done']

That's what I tried inside my controller :

public function bootstrap($data)
{
    $parseTopics = Helper::parseTopicsRequest();

    $basicProgram = new BasicProgramRepository();
    $seasonalProgram = new SeasonalProgramRepository($parseTopics['SEASONAL']);


    $object = count($parseTopics['SEASONAL']) ? $seasonalProgram : $basicProgram;
    // Polymorphism
    return $object->execute();
}

Question 1 : I'm not sure if I should use this way or something like to fix my need:

$employe = new Program(new BasicProgramRepository());

Expected Result : The expected result depends on if I have season parameter and expiry. What I want to achieve is to use different behaviours ( execute method )

CodePudding user response:

if you want to achieve Polymorphism method, it will be better creating repository or something only for managing that logic.

here is sample.

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();
    }
}

and you can call like this.

$sampleRepository  = new SampleRepository($sampleValue); // expect string[] or null like ["SEASON"],["SEASON","EXPIRY"],null
    $result = $sampleRepository->execute(); // [string] or [string,string] or []

this approach is only what your parameter is secified value. if your return result is almost same both of Season class and Expiry class, it will be better to manage on trait. (that is $pattern on sample code)

try some.

  • Related