I'm using Laravel 9 and I have a request that contains:
array
or null So the SEASON
parameter can be array
or null
array
or null
I have two classes, one for the SEASON
functionality and another for EXPIRY
, both from the Repository
Extension. Both have a method called execute
which returns an array
abstract class Repository { abstract public function execute(): array; } class Expiry extends Repository { public function execute() { return ['The Request contains Expiry Parameter, and seasonal behaviors is done']; } } class Season extends Repository { public function execute() { return ['The Request contain Season Parameter, and expiry behaviors is done']; } }
If my request contains SEASON, I want to call the execute method of the Season class, or if my request contains Expiry, I want to call the execute method of expiry. Or call them and merge the execution returns into an array so I can get the results.
['The Request contain Expiry Parameter, and seasonal behaviors is done', 'The Request contain Expiry Parameter, and expiry behaviors is done'];
This is what I tried in the 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 or something similar to solve my needs:
$employe = new Program(new BasicProgramRepository());
Expected results: The expected result depends on whether I have season parameters and expiry time. What I want to achieve is to use different behavior (execute method)
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.
You can call it like this.
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