好的,坚持我在这里.我有一个我需要遵循的过程,并且有很多if语句我已经分解成较小的类而不是一个丑陋的开关或if/else语句.基本上,我调用类的handle方法,它确定我们在进程中的位置,实例化适当的类,然后构建所需的集合并返回它.
我有一个模型,我试图获得该nextAction特定记录.在nextAction将有一个名称,日期,timeRemaining和路由键返回集合.这是我的FreeLook.php模型中的方法:
public function nextAction()
{
// handle basically just calls a handle method on the NextActionHandler class.
return handle(new NextActionHandler($this));
}
Run Code Online (Sandbox Code Playgroud)
该NextActionHandler课程列出了我们在这个过程中的位置.它通过使用我创建的特征来实现Actionable.这是NextActionHandler班级:
class NextActionHandler
{
use Actionable;
protected $freeLook;
public function __construct($freeLook)
{
$this->freeLook = $freeLook;
}
public function handle()
{
switch (true) {
case $this->wantsSign() && !$this->signJobIsComplete():
return handle(new SignNextAction($this->freeLook));
case $this->wantsPaint() && !$this->paintJobIsComplete():
return handle(new PaintNextAction($this->freeLook));
case $this->needsToSubmitCoOp():
return handle(new CoOpNextAction($this->freeLook));
default:
return handle(new DefaultNextAction($this->freeLook));
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是一些Actionable特质.我不会把所有这些都放在这里,因为它真的很长.我包含了很多逻辑,以便弄清楚我们在这个过程中的位置.需要注意的一点是,它确实包含构建我返回的集合输出的逻辑.(如果你想要完整的Actionable代码,请到这里)
protected function needToChooseVendor()
{
$this->name = 'Choose Vendor';
$this->duration = $this->freeLook->status->expected_duration;
$this->calculateTimeRemaining();
return $this->buildOutput();
}
protected function calculateTimeRemaining()
{
if ($this->duration) {
$this->timeRemaining = Carbon::now()->diffInDays($this->date);
}
}
protected function buildOutput()
{
return collect([
'name' => $this->name,
'date' => $this->date,
'timeRemaining' => $this->timeRemaining,
'route' => $this->route,
]);
}
Run Code Online (Sandbox Code Playgroud)
最后,我的DefaultNextAction类,它也使用特征来构造输出并返回它.
class DefaultNextAction
{
use Actionable;
public function __construct($freeLook)
{
$this->freeLook = $freeLook;
$this->date = $freeLook->next_action_at;
}
public function handle()
{
return $this->returnDefaultNextAction();
}
protected function returnDefaultNextAction()
{
$this->name = $this->freeLook->status->name;
$this->duration = $this->freeLook->status->expected_duration;
$this->calculateTimeRemaining();
$this->output = $this->buildOutput();
return $this->output;
}
}
Run Code Online (Sandbox Code Playgroud)
所以,当我在returnDefaultNextAction方法中进行转储和死亡时,我得到了预期的结果.但是,当我在我的模型中倾倒和死亡时,我总是得到null.这是什么原因?由于我使用特征并且特征无法实例化,我认为我正在处理对象的相同实例并且所有应该都可以工作.
任何想法都会非常有用!
编辑1
这是handle我用来包裹所有对象的方法.
if (!function_exists('handle')) {
function handle($object)
{
if (!is_object($object)) {
throw new Exception('An object must be passed to the handle method. This is not what happened.');
}
call_user_func([$object, 'handle']);
}
}
Run Code Online (Sandbox Code Playgroud)
编辑2
即使我在我的模型中调用我的句柄方法,我得到相同的null结果:
public function nextAction()
{
$nextAction = new NextActionHandler($this);
return $nextAction->handle();
}
Run Code Online (Sandbox Code Playgroud)
您的handle函数没有返回任何内容。需要是
if (!function_exists('handle')) {
function handle($object)
{
if (!is_object($object)) {
throw new Exception('An object must be passed to the handle method. This is not what happened.');
}
return call_user_func([$object, 'handle']);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
82 次 |
| 最近记录: |