diff --git a/.travis.yml b/.travis.yml index b51d696..0a6ef3e 100755 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,6 @@ language: php -php: - - 5.3 +php: - 5.4 before_script: diff --git a/README.md b/README.md index f848de6..e9e0fb9 100755 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ This library provides a simple class to help make a `Presenter` for your objects Add `robclancy/presenter` to the "require" section of your `composer.json` file. ```json - "robclancy/presenter": "1.1.*" + "robclancy/presenter": "1.3.*" ``` Run `composer update` to get the latest version of the package. @@ -55,7 +55,7 @@ Now presenters will automatically be created if using the =5.3.0" + "php": ">=5.4.0" }, "require-dev": { "mockery/mockery": "0.7.2", - "illuminate/view": "4.0.*", + "illuminate/view": "4.2.*", "phpunit/phpunit": "3.7.*" }, "autoload": { - "psr-0": { - "Robbo\\Presenter": "src/" + "psr-4": { + "Robbo\\Presenter\\": "src/" } }, "extra": { "branch-alias": { - "dev-master": "1.2-dev" + "dev-master": "1.3-dev" } }, "minimum-stability": "stable" diff --git a/src/Robbo/Presenter/Decorator.php b/src/Decorator.php similarity index 100% rename from src/Robbo/Presenter/Decorator.php rename to src/Decorator.php diff --git a/src/Robbo/Presenter/PresentableInterface.php b/src/PresentableInterface.php similarity index 100% rename from src/Robbo/Presenter/PresentableInterface.php rename to src/PresentableInterface.php diff --git a/src/Robbo/Presenter/Presenter.php b/src/Presenter.php similarity index 81% rename from src/Robbo/Presenter/Presenter.php rename to src/Presenter.php index 9f13724..8afcd7c 100755 --- a/src/Robbo/Presenter/Presenter.php +++ b/src/Presenter.php @@ -45,7 +45,7 @@ protected function __getDecorator() /** * This is so you can extend the decorator and inject it into the presenter at the class level so the - * new decorator will be used for nested presenters. Method name should be "setDecorator" however + * new decorator will be used for nested presenters. Method name should be "setDecorator" however * like above I want to make conflicts less likely. * * @param \Robbo\Presenter\Decorator @@ -58,7 +58,7 @@ public static function setExtendedDecorator(Decorator $decorator) /** * Get the object we are wrapping. - * + * * @return mixed */ public function getObject() @@ -75,12 +75,15 @@ public function getObject() public function offsetExists($offset) { // We only check isset on the array, if it is an object we return true as the object could be overloaded - if (is_array($this->object)) + if ( ! is_array($this->object)) return true; + + if ($method = $this->getPresenterMethodFromVariable($offset)) { - return isset($this->object[$offset]); + $result = $this->$method(); + return isset($result); } - return true; + return isset($this->object[$offset]); } /** @@ -130,15 +133,14 @@ public function offsetUnset($offset) } /** - * Pass any unknown varible calls to present{$variable} or fall through to the injected object. + * Pass any unknown variable calls to present{$variable} or fall through to the injected object. * * @param string $var * @return mixed */ public function __get($var) { - $method = 'present'.str_replace(' ', '', ucwords(str_replace(array('-', '_'), ' ', $var))); - if (method_exists($this, $method)) + if ($method = $this->getPresenterMethodFromVariable($var)) { return $this->$method(); } @@ -173,6 +175,12 @@ public function __call($method, $arguments) */ public function __isset($name) { + if ($method = $this->getPresenterMethodFromVariable($name)) + { + $result = $this->$method(); + return isset($result); + } + if (is_array($this->object)) { return isset($this->object[$name]); @@ -182,7 +190,7 @@ public function __isset($name) } /** - * Allow to unset a variable through the persenter + * Allow to unset a variable through the presenter * * @param string $name */ @@ -197,4 +205,18 @@ public function __unset($name) unset($this->object->$name); } + /** + * Fetch the 'present' method name for the given variable. + * + * @param string $variable + * @return string|null + */ + protected function getPresenterMethodFromVariable($variable) + { + $method = 'present'.str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $variable))); + if (method_exists($this, $method)) + { + return $method; + } + } } diff --git a/src/Robbo/Presenter/PresenterServiceProvider.php b/src/PresenterServiceProvider.php similarity index 81% rename from src/Robbo/Presenter/PresenterServiceProvider.php rename to src/PresenterServiceProvider.php index c41fc21..06fefe3 100755 --- a/src/Robbo/Presenter/PresenterServiceProvider.php +++ b/src/PresenterServiceProvider.php @@ -30,7 +30,7 @@ public function register() { $this->registerDecorator(); - $this->registerEnvironment(); + $this->registerFactory(); } /** @@ -56,31 +56,31 @@ public function registerDecorator() /** * Copied from the view service provider... * - * Register the view environment. + * Register the view factory. * * @return void */ - public function registerEnvironment() + public function registerFactory() { $this->app['view'] = $this->app->share(function($app) { // Next we need to grab the engine resolver instance that will be used by the - // environment. The resolver will be used by an environment to get each of + // factory. The resolver will be used by a factory to get each of // the various engine implementations such as plain PHP or Blade engine. $resolver = $app['view.engine.resolver']; $finder = $app['view.finder']; - $env = new View\Environment($resolver, $finder, $app['events'], $app['presenter.decorator']); + $factory = new View\Factory($resolver, $finder, $app['events'], $app['presenter.decorator']); - // We will also set the container instance on this view environment since the + // We will also set the container instance on this view factory since the // view composers may be classes registered in the container, which allows // for great testable, flexible composers for the application developer. - $env->setContainer($app); + $factory->setContainer($app); - $env->share('app', $app); + $factory->share('app', $app); - return $env; + return $factory; }); } @@ -91,7 +91,7 @@ public function registerEnvironment() */ public function provides() { - return array(); + return []; } } \ No newline at end of file diff --git a/src/Robbo/Presenter/View/Environment.php b/src/View/Factory.php similarity index 87% rename from src/Robbo/Presenter/View/Environment.php rename to src/View/Factory.php index eecaf7f..e682c50 100755 --- a/src/Robbo/Presenter/View/Environment.php +++ b/src/View/Factory.php @@ -6,9 +6,9 @@ use Illuminate\View\ViewFinderInterface; use Robbo\Presenter\PresentableInterface; use Illuminate\View\Engines\EngineResolver; -use Illuminate\View\Environment as BaseEnvironment; +use Illuminate\View\Factory as BaseFactory; -class Environment extends BaseEnvironment { +class Factory extends BaseFactory { /** * Used for "decorating" objects to have presenters. @@ -18,7 +18,7 @@ class Environment extends BaseEnvironment { protected $presenterDecorator; /** - * Create a new view environment instance. + * Create a new view factory instance. * * @param \Illuminate\View\Engines\EngineResolver $engines * @param \Illuminate\View\ViewFinderInterface $finder @@ -41,7 +41,7 @@ public function __construct(EngineResolver $engines, ViewFinderInterface $finder * @param array $mergeData * @return Illuminate\View\View */ - public function make($view, $data = array(), $mergeData = array()) + public function make($view, $data = [], $mergeData = []) { $path = $this->finder->find($view); @@ -51,7 +51,7 @@ public function make($view, $data = array(), $mergeData = array()) } /** - * Add a piece of shared data to the environment. + * Add a piece of shared data to the factory. * * @param string $key * @param mixed $value diff --git a/src/Robbo/Presenter/View/View.php b/src/View/View.php similarity index 74% rename from src/Robbo/Presenter/View/View.php rename to src/View/View.php index eec7800..4707ac1 100755 --- a/src/Robbo/Presenter/View/View.php +++ b/src/View/View.php @@ -16,9 +16,9 @@ public function with($key, $value = null) { if (is_array($key)) { - return parent::with($this->environment->decorate($key)); + return parent::with($this->factory->decorate($key)); } - return parent::with($key, $this->environment->decorate($value)); + return parent::with($key, $this->factory->decorate($value)); } } \ No newline at end of file diff --git a/tests/PresenterTest.php b/tests/PresenterTest.php index 7ce108f..aae7a91 100755 --- a/tests/PresenterTest.php +++ b/tests/PresenterTest.php @@ -71,11 +71,21 @@ public function testArrayIsset() $this->assertTrue(isset($presenter['testVar'])); $this->assertFalse(isset($presenter['unsetVar'])); + $this->assertTrue(isset($presenter['awesome'])); $presenter = new PresenterStub(new InjectStub); $this->assertTrue(isset($presenter['unsetVar'])); } + public function testObjectIsset() + { + $presenter = new PresenterStub(new InjectStub); + + $this->assertTrue(isset($presenter->testVar)); + $this->assertTrue(isset($presenter->awesome)); + $this->assertFalse(isset($presenter->unsetVar)); + } + public function testArraySet() { $presenter = new PresenterStub(array('testVar' => 'testvar')); diff --git a/tests/ViewEnvironmentTest.php b/tests/ViewFactoryTest.php similarity index 76% rename from tests/ViewEnvironmentTest.php rename to tests/ViewFactoryTest.php index df72ce0..22cf9d2 100755 --- a/tests/ViewEnvironmentTest.php +++ b/tests/ViewFactoryTest.php @@ -5,10 +5,10 @@ use Robbo\Presenter\Decorator; use Robbo\Presenter\View\View; use Illuminate\Support\Collection; -use Robbo\Presenter\View\Environment; +use Robbo\Presenter\View\Factory; use Robbo\Presenter\PresentableInterface; -class ViewEnvironmentTest extends PHPUnit_Framework_TestCase { +class ViewFactoryTest extends PHPUnit_Framework_TestCase { public function tearDown() { @@ -25,10 +25,10 @@ public function testMakeView() )) ); - $env = $this->getEnvironment(); - $env->finder->shouldReceive('find')->once()->andReturn('test'); + $factory = $this->getFactory(); + $factory->finder->shouldReceive('find')->once()->andReturn('test'); - $view = $env->make('test', $data); + $view = $factory->make('test', $data); $this->assertInstanceOf('Robbo\Presenter\View\View', $view); $this->assertSame($view['meh'], $data['meh']); @@ -38,9 +38,9 @@ public function testMakeView() $this->assertInstanceOf('Robbo\Presenter\Presenter', $view['collection']['presentable']); } - protected function getEnvironment() + protected function getFactory() { - return new EnvironmentStub( + return new FactoryStub( m::mock('Illuminate\View\Engines\EngineResolver'), m::mock('Illuminate\View\ViewFinderInterface'), m::mock('Illuminate\Events\Dispatcher'), @@ -49,11 +49,11 @@ protected function getEnvironment() } } -class EnvironmentStub extends Environment { +class FactoryStub extends Factory { public $finder; - protected function getEngineFromPath($path) + public function getEngineFromPath($path) { return m::mock('Illuminate\View\Engines\EngineInterface'); } @@ -75,7 +75,7 @@ public function getPresentableObject() public function getPresenter() { - return new EnvPresenterStub($this); + return new FactoryPresenterStub($this); } } @@ -83,8 +83,8 @@ class SecondPresentableStub implements PresentableInterface { public function getPresenter() { - return new EnvPresenterStub($this); + return new FactoryPresenterStub($this); } } -class EnvPresenterStub extends Presenter {} \ No newline at end of file +class FactoryPresenterStub extends Presenter {} \ No newline at end of file diff --git a/tests/ViewTest.php b/tests/ViewTest.php index 0bae710..8c0adc0 100755 --- a/tests/ViewTest.php +++ b/tests/ViewTest.php @@ -15,14 +15,14 @@ public function tearDown() public function testWithMakesPresentable() { - $env = new EnvironmentStub( + $factory = new FactoryStub( m::mock('Illuminate\View\Engines\EngineResolver'), m::mock('Illuminate\View\ViewFinderInterface'), m::mock('Illuminate\Events\Dispatcher'), new Decorator ); - $view = new View($env, m::mock('Illuminate\View\Engines\EngineInterface'), 'test', 'test/path'); + $view = new View($factory, m::mock('Illuminate\View\Engines\EngineInterface'), 'test', 'test/path'); $view->with('presenter', new ViewPresentableStub); @@ -31,14 +31,14 @@ public function testWithMakesPresentable() public function testWithMakesArrayPresentable() { - $env = new EnvironmentStub( + $factory = new FactoryStub( m::mock('Illuminate\View\Engines\EngineResolver'), m::mock('Illuminate\View\ViewFinderInterface'), m::mock('Illuminate\Events\Dispatcher'), new Decorator ); - $view = new View($env, m::mock('Illuminate\View\Engines\EngineInterface'), 'test', 'test/path'); + $view = new View($factory, m::mock('Illuminate\View\Engines\EngineInterface'), 'test', 'test/path'); $data = array( 'presenter' => new ViewPresentableStub