Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ability to call methods on entity via presenter #25

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions spec/Laracasts/Presenter/PresenterSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace spec\Laracasts\Presenter;

use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

use Laracasts\Presenter\Presenter;

class PresenterSpec extends ObjectBehavior
{
function let()
{
$this->beAnInstanceOf('spec\Laracasts\Presenter\ExamplePresenter');
$this->beConstructedWith(new ExampleEntity);
}

function it_retrieves_properties_of_its_entity()
{
$foo = $this->foo->shouldEqual('bar');;
}

function it_calls_methods_of_its_entity_if_the_method_is_not_defined()
{
$this->baz()->shouldReturn(['foo', 'bar']);
}

function it_calls_methods_of_its_entity_if_the_method_is_not_defined_and_passes_the_arguments()
{
$this->multiply(1, 2, 3)->shouldReturn(6);
}
}

class ExampleEntity {
public $foo = 'bar';

public function baz()
{
return ['foo', 'bar'];
}

public function multiply($x, $y, $z)
{
return $x * $y * $z;
}
}

class ExamplePresenter extends Presenter {

}
19 changes: 18 additions & 1 deletion src/Laracasts/Presenter/Presenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,21 @@ public function __get($property)
return $this->entity->{$property};
}

}
/**
* Allow for method-style retrieval
*
* @param $method
* @param $arguments
* @return mixed
*/
public function __call($method, $arguments)
{
if (method_exists($this->entity, $method))
{
return call_user_func_array([$this->entity, $method], $arguments);
}

throw new \Exception("Method not found on presenter or its entity.");
}

}