diff --git a/src/AbstractContainer.php b/src/AbstractContainer.php index aabfcc5..ccfacdf 100644 --- a/src/AbstractContainer.php +++ b/src/AbstractContainer.php @@ -93,7 +93,7 @@ public function notifyOrderUpdated() * calling {@link Page\AbstractPage::setParent()}. * * @param Page\AbstractPage|array|Traversable $page page to add - * @return AbstractContainer fluent interface, returns self + * @return self fluent interface, returns self * @throws Exception\InvalidArgumentException if page is invalid */ public function addPage($page) @@ -136,7 +136,7 @@ public function addPage($page) * Adds several pages at once * * @param array|Traversable|AbstractContainer $pages pages to add - * @return AbstractContainer fluent interface, returns self + * @return self fluent interface, returns self * @throws Exception\InvalidArgumentException if $pages is not array, * Traversable or AbstractContainer */ @@ -172,7 +172,7 @@ public function addPages($pages) * Sets pages this container should have, removing existing pages * * @param array $pages pages to set - * @return AbstractContainer fluent interface, returns self + * @return self fluent interface, returns self */ public function setPages(array $pages) { @@ -223,7 +223,7 @@ public function removePage($page) /** * Removes all pages in container * - * @return AbstractContainer fluent interface, returns self + * @return self fluent interface, returns self */ public function removePages() { diff --git a/src/Page/AbstractPage.php b/src/Page/AbstractPage.php index 4059beb..0cdf538 100644 --- a/src/Page/AbstractPage.php +++ b/src/Page/AbstractPage.php @@ -115,6 +115,13 @@ abstract class AbstractPage extends AbstractContainer */ protected $permission; + /** + * Text domain for Translator + * + * @var string + */ + protected $textDomain; + /** * Whether this page should be considered active * @@ -143,6 +150,13 @@ abstract class AbstractPage extends AbstractContainer */ protected $properties = array(); + /** + * Static factories list for factory pages + * + * @var array + */ + protected static $factories = array(); + // Initialization: /** @@ -220,6 +234,14 @@ public static function factory($options) } } + if (static::$factories) { + foreach (static::$factories as $factoryCallBack) { + if (($page = call_user_func($factoryCallBack, $options))) { + return $page; + } + } + } + $hasUri = isset($options['uri']); $hasMvc = isset($options['action']) || isset($options['controller']) || isset($options['route']); @@ -235,6 +257,16 @@ public static function factory($options) } } + /** + * Add static factory for self::factory function + * + * @param type $callback Any callable variable + */ + public static function addFactory($callback) + { + static::$factories[] = $callback; + } + /** * Page constructor * @@ -732,6 +764,33 @@ public function getPermission() return $this->permission; } + /** + * Sets text domain for translation + * + * @param string|null $textDomain [optional] text domain to associate + * with this page. Default is null, which + * sets no text domain. + * + * @return AbstractPage fluent interface, returns self + */ + public function setTextDomain($textDomain = null) + { + if (null !== $textDomain) { + $this->textDomain = $textDomain; + } + return $this; + } + + /** + * Returns text domain for translation + * + * @return mixed|null text domain or null + */ + public function getTextDomain() + { + return $this->textDomain; + } + /** * Sets whether page should be considered active or not * diff --git a/src/Page/Mvc.php b/src/Page/Mvc.php index cdda1c4..b1f80da 100644 --- a/src/Page/Mvc.php +++ b/src/Page/Mvc.php @@ -97,7 +97,14 @@ class Mvc extends AbstractPage * * @var RouteStackInterface */ - protected static $defaultRouter= null; + protected static $defaultRouter = null; + + /** + * Default route name + * + * @var string + */ + protected static $defaultRoute = null; // Accessors: @@ -228,8 +235,8 @@ public function getHref() } switch (true) { - case ($this->getRoute() !== null): - $name = $this->getRoute(); + case ($this->getRoute() !== null || static::getDefaultRoute() !== null): + $name = ($this->getRoute() !== null) ? $this->getRoute() : static::getDefaultRoute(); break; case ($this->getRouteMatch() !== null): $name = $this->getRouteMatch()->getMatchedRouteName(); @@ -359,13 +366,7 @@ public function getQuery() */ public function setParams(array $params = null) { - if (null === $params) { - $this->params = array(); - } else { - // TODO: do this more intelligently? - $this->params = $params; - } - + $this->params = empty($params) ? array() : $params; $this->hrefCache = null; return $this; } @@ -508,6 +509,27 @@ public static function getDefaultRouter() return static::$defaultRouter; } + /** + * Set default route name + * + * @param string $route + * @return void + */ + public static function setDefaultRoute($route) + { + static::$defaultRoute = $route; + } + + /** + * Get default route name + * + * @return string + */ + public static function getDefaultRoute() + { + return static::$defaultRoute; + } + // Public methods: /** diff --git a/test/ContainerTest.php b/test/ContainerTest.php index 9b79668..73f8eae 100644 --- a/test/ContainerTest.php +++ b/test/ContainerTest.php @@ -5,7 +5,6 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Navigation */ namespace ZendTest\Navigation; @@ -17,9 +16,6 @@ /** * Tests the class Zend_Navigation_Container * - * @category Zend - * @package Zend_Navigation - * @subpackage UnitTests * @group Zend_Navigation */ class ContainerTest extends \PHPUnit_Framework_TestCase @@ -799,7 +795,7 @@ public function testFindOneByShouldReturnNullIfNotFound() { $nav = $this->_getFindByNavigation(); - $found = $nav->findOneBy('id', 'non-existant'); + $found = $nav->findOneBy('id', 'non-existent'); $this->assertNull($found); } @@ -823,7 +819,7 @@ public function testFindAllByShouldReturnAllMatchingPages() public function testFindAllByShouldReturnEmptyArrayifNotFound() { $nav = $this->_getFindByNavigation(); - $found = $nav->findAllBy('id', 'non-existant'); + $found = $nav->findAllBy('id', 'non-existent'); $expected = array('type' => 'array', 'count' => 0); $actual = array('type' => gettype($found), 'count' => count($found)); diff --git a/test/NavigationTest.php b/test/NavigationTest.php index b36360f..3dee571 100644 --- a/test/NavigationTest.php +++ b/test/NavigationTest.php @@ -5,7 +5,6 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Navigation */ namespace ZendTest\Navigation; @@ -17,9 +16,6 @@ */ /** - * @category Zend - * @package Zend_Navigation - * @subpackage UnitTests * @group Zend_Navigation */ class NavigationTest extends \PHPUnit_Framework_TestCase diff --git a/test/Page/MvcTest.php b/test/Page/MvcTest.php index 32b7d40..f589906 100644 --- a/test/Page/MvcTest.php +++ b/test/Page/MvcTest.php @@ -5,7 +5,6 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Navigation */ namespace ZendTest\Navigation\Page; @@ -25,9 +24,6 @@ /** * Tests the class Zend_Navigation_Page_Mvc * - * @category Zend - * @package Zend_Navigation - * @subpackage UnitTests * @group Zend_Navigation */ class MvcTest extends TestCase @@ -53,6 +49,21 @@ protected function tearDown() { } + public function testHrefGeneratedByRouterWithDefaultRoute() + { + $page = new Page\Mvc(array( + 'label' => 'foo', + 'action' => 'index', + 'controller' => 'index' + )); + Page\Mvc::setDefaultRoute('default'); + $page->setRouter($this->router); + $page->setAction('view'); + $page->setController('news'); + + $this->assertEquals('/news/view', $page->getHref()); + } + public function testHrefGeneratedByRouterRequiresNoRoute() { $page = new Page\Mvc(array( diff --git a/test/Page/PageFactoryTest.php b/test/Page/PageFactoryTest.php index 63de42d..66b584b 100644 --- a/test/Page/PageFactoryTest.php +++ b/test/Page/PageFactoryTest.php @@ -5,7 +5,6 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Navigation */ namespace ZendTest\Navigation; @@ -16,15 +15,31 @@ /** * Tests Zend_Navigation_Page::factory() * -/** - * @category Zend - * @package Zend_Navigation - * @subpackage UnitTests * @group Zend_Navigation */ class PageFactoryTest extends \PHPUnit_Framework_TestCase { + public function testDetectFactoryPage() + { + AbstractPage::addFactory(function ($page) { + if (isset($page['factory_uri'])) { + return new \Zend\Navigation\Page\Uri($page); + } elseif (isset($page['factory_mvc'])) { + return new \Zend\Navigation\Page\Mvc($page); + } + }); + + $this->assertInstanceOf('Zend\\Navigation\\Page\\Uri', AbstractPage::factory(array( + 'label' => 'URI Page', + 'factory_uri' => '#' + ))); + + $this->assertInstanceOf('Zend\\Navigation\\Page\\Mvc', AbstractPage::factory(array( + 'label' => 'URI Page', + 'factory_mvc' => '#' + ))); + } public function testDetectMvcPage() { @@ -118,8 +133,8 @@ public function testShouldFailForInvalidType() public function testShouldFailForNonExistantType() { $pageConfig = array( - 'type' => 'My_NonExistant_Page', - 'label' => 'My non-existant Page' + 'type' => 'My_NonExistent_Page', + 'label' => 'My non-existent Page' ); try { @@ -128,7 +143,7 @@ public function testShouldFailForNonExistantType() return; } - $msg = 'An exception has not been thrown for non-existant class'; + $msg = 'An exception has not been thrown for non-existent class'; $this->fail($msg); } diff --git a/test/Page/PageTest.php b/test/Page/PageTest.php index bafb21a..402de5a 100644 --- a/test/Page/PageTest.php +++ b/test/Page/PageTest.php @@ -5,7 +5,6 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Navigation */ namespace ZendTest\Navigation\Page; @@ -20,9 +19,6 @@ * Tests the class Zend_Navigation_Page * * @author Robin Skoglund - * @category Zend - * @package Zend_Navigation - * @subpackage UnitTests * @group Zend_Navigation */ class PageTest extends \PHPUnit_Framework_TestCase @@ -71,37 +67,6 @@ public function testGetShouldMapToNativeProperties() $this->assertEquals('bar', $page->get('Action')); } - /** - * This functionality was removed in ZF2 to comply with new URL helper; do we need it? - * - * @group disable - */ - public function testSetShouldNormalizePropertyName() - { - $page = AbstractPage::factory(array( - 'type' => 'mvc' - )); - - $page->setResetParams(false); - $page->set('reset_params', true); - $this->assertTrue($page->getResetParams()); - } - - /** - * This functionality was removed in ZF2 to comply with new URL helper; do we need it? - * - * @group disable - */ - public function testGetShouldNormalizePropertyName() - { - $page = AbstractPage::factory(array( - 'type' => 'mvc' - )); - - $page->setResetParams(false); - $this->assertFalse($page->get('reset_params')); - } - public function testShouldSetAndGetShouldMapToProperties() { $page = AbstractPage::factory(array( @@ -726,6 +691,17 @@ public function testSetVisibleShouldJuggleValue() $this->assertFalse($page->isVisible()); } + public function testSetTranslatorTextDomainString() + { + $page = AbstractPage::factory(array( + 'type' => 'uri', + 'label' => 'hello' + )); + + $page->setTextdomain('foo'); + $this->assertEquals('foo', $page->getTextdomain()); + } + public function testMagicOverLoadsShouldSetAndGetNativeProperties() { $page = AbstractPage::factory(array( diff --git a/test/Page/UriTest.php b/test/Page/UriTest.php index f0b17b3..1f49411 100644 --- a/test/Page/UriTest.php +++ b/test/Page/UriTest.php @@ -5,7 +5,6 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Navigation */ namespace ZendTest\Navigation\Page; @@ -17,9 +16,6 @@ /** * Tests the class Zend_Navigation_Page_Uri * - * @category Zend - * @package Zend_Navigation - * @subpackage UnitTests * @group Zend_Navigation */ class UriTest extends \PHPUnit_Framework_TestCase diff --git a/test/ServiceFactoryTest.php b/test/ServiceFactoryTest.php index 5fa4e2e..0527871 100644 --- a/test/ServiceFactoryTest.php +++ b/test/ServiceFactoryTest.php @@ -5,7 +5,6 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Navigation */ namespace ZendTest\Navigation; @@ -22,9 +21,6 @@ /** * Tests the class Zend\Navigation\MvcNavigationFactory * - * @category Zend - * @package Zend_Navigation - * @subpackage UnitTests * @group Zend_Navigation */ class ServiceFactoryTest extends \PHPUnit_Framework_TestCase @@ -149,7 +145,7 @@ public function testConstructedNavigationFactoryInjectRouterAndMatcher() $this->isInstanceOf("Zend\Mvc\Router\RouteMatch"), $this->isInstanceOf("Zend\Mvc\Router\RouteStackInterface")); - $this->serviceManager->setFactory('Navigation', function ($serviceLocator) use ($factory){ + $this->serviceManager->setFactory('Navigation', function ($serviceLocator) use ($factory) { return $factory->createService($serviceLocator); }); diff --git a/test/TestAsset/AbstractContainer.php b/test/TestAsset/AbstractContainer.php index 0c1f53c..5003fc5 100644 --- a/test/TestAsset/AbstractContainer.php +++ b/test/TestAsset/AbstractContainer.php @@ -5,16 +5,10 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Navigation */ namespace ZendTest\Navigation\TestAsset; -/** - * @category Zend - * @package Zend_Navigation - * @subpackage UnitTests - */ class AbstractContainer extends \Zend\Navigation\AbstractContainer { public function addPage($page) diff --git a/test/TestAsset/FileNavigationFactory.php b/test/TestAsset/FileNavigationFactory.php index d94a96d..e663a28 100644 --- a/test/TestAsset/FileNavigationFactory.php +++ b/test/TestAsset/FileNavigationFactory.php @@ -5,7 +5,6 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Navigation */ namespace ZendTest\Navigation\TestAsset; diff --git a/test/TestAsset/InvalidPage.php b/test/TestAsset/InvalidPage.php index 4d5b854..38b1ca3 100644 --- a/test/TestAsset/InvalidPage.php +++ b/test/TestAsset/InvalidPage.php @@ -5,16 +5,10 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Navigation */ namespace ZendTest\Navigation\TestAsset; -/** - * @category Zend - * @package Zend_Navigation - * @subpackage UnitTests - */ class InvalidPage { /** diff --git a/test/TestAsset/Page.php b/test/TestAsset/Page.php index f4acf99..3a27443 100644 --- a/test/TestAsset/Page.php +++ b/test/TestAsset/Page.php @@ -5,18 +5,12 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Navigation */ namespace ZendTest\Navigation\TestAsset; use Zend\Navigation\Page\AbstractPage; -/** - * @category Zend - * @package Zend_Navigation - * @subpackage UnitTests - */ class Page extends AbstractPage { /** diff --git a/test/TestAsset/Router.php b/test/TestAsset/Router.php index db5ec1a..9b11d80 100644 --- a/test/TestAsset/Router.php +++ b/test/TestAsset/Router.php @@ -5,16 +5,10 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Navigation */ namespace ZendTest\Navigation\TestAsset; -/** - * @category Zend - * @package Zend_Navigation - * @subpackage UnitTests - */ class Router extends \Zend\Mvc\Router\Http\TreeRouteStack { const RETURN_URL = 'spotify:track:2nd6CTjR9zjHGT0QtpfLHe';