Parcourir la source

ZF-8922 - Add support for fragments (anchors) to Zend_Navigation

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@24440 44c647ce-9c0f-0410-b52a-842ac1e357ba
padraic il y a 14 ans
Parent
commit
faef263893

+ 44 - 0
library/Zend/Navigation/Page.php

@@ -42,6 +42,20 @@ abstract class Zend_Navigation_Page extends Zend_Navigation_Container
     protected $_label;
 
     /**
+     * Fragment identifier (anchor identifier)
+     * 
+     * The fragment identifier (anchor identifier) pointing to an anchor within 
+     * a resource that is subordinate to another, primary resource.
+     * The fragment identifier introduced by a hash mark "#".
+     * Example: http://www.example.org/foo.html#bar ("bar" is the fragment identifier)
+     * 
+     * @link http://www.w3.org/TR/html401/intro/intro.html#fragment-uri
+     * 
+     * @var string|null
+     */
+    protected $_fragmentIdentifier;
+
+    /**
      * Page id
      *
      * @var string|null
@@ -330,6 +344,35 @@ abstract class Zend_Navigation_Page extends Zend_Navigation_Container
     }
 
     /**
+     * Sets a fragment identifier
+     *
+     * @param  string $fragmentIdentifier   new fragment identifier
+     * @return Zend_Navigation_Page         fluent interface, returns self
+     * @throws Zend_Navigation_Exception    if empty/no string is given
+     */
+    public function setFragmentIdentifier($fragmentIdentifier)
+    {
+        if (null !== $fragmentIdentifier && !is_string($fragmentIdentifier)) {
+            require_once 'Zend/Navigation/Exception.php';
+            throw new Zend_Navigation_Exception(
+                    'Invalid argument: $fragmentIdentifier must be a string or null');
+        }
+ 
+        $this->_fragmentIdentifier = $fragmentIdentifier;
+        return $this;
+    }
+    
+     /**
+     * Returns fragment identifier
+     *
+     * @return string|null  fragment identifier
+     */
+    public function getFragmentIdentifier()
+    {
+        return $this->_fragmentIdentifier;
+    }
+
+    /**
      * Sets page id
      *
      * @param  string|null $id            [optional] id to set. Default is null,
@@ -1091,6 +1134,7 @@ abstract class Zend_Navigation_Page extends Zend_Navigation_Container
             $this->getCustomProperties(),
             array(
                 'label'     => $this->getlabel(),
+                'fragmentIdentifier' => $this->getFragmentIdentifier(),
                 'id'        => $this->getId(),
                 'class'     => $this->getClass(),
                 'title'     => $this->getTitle(),

+ 8 - 0
library/Zend/Navigation/Page/Mvc.php

@@ -216,6 +216,14 @@ class Zend_Navigation_Page_Mvc extends Zend_Navigation_Page
                                       $this->getRoute(),
                                       $this->getResetParams());
 
+        // Add the fragment identifier if it is set
+        $fragmentIdentifier = $this->getFragmentIdentifier();       
+        if (null !== $fragmentIdentifier) {
+            $url .= '#' . $fragmentIdentifier;
+        }         
+        
+         return $this->_hrefCache = $url;
+
         return $this->_hrefCache = $url;
     }
 

+ 12 - 1
library/Zend/Navigation/Page/Uri.php

@@ -79,7 +79,18 @@ class Zend_Navigation_Page_Uri extends Zend_Navigation_Page
      */
     public function getHref()
     {
-        return $this->getUri();
+        $uri = $this->getUri();
+        
+        $fragmentIdentifier = $this->getFragmentIdentifier();       
+        if (null !== $fragmentIdentifier) {
+            if ('#' == substr($uri, -1)) {
+                return $uri . $fragmentIdentifier;
+            } else {                
+                return $uri . '#' . $fragmentIdentifier;
+            }
+        }
+        
+        return $uri;
     }
 
     // Public methods:

+ 33 - 0
tests/Zend/Navigation/Page/MvcTest.php

@@ -103,6 +103,38 @@ class Zend_Navigation_Page_MvcTest extends PHPUnit_Framework_TestCase
         $this->assertEquals('/lolcat/myaction/1337', $page->getHref());
     }
 
+    /**
+     * @group ZF-8922
+     */
+    public function testGetHrefWithFragmentIdentifier()
+    {
+        $page = new Zend_Navigation_Page_Mvc(array(
+            'label'              => 'foo',
+            'fragmentIdentifier' => 'qux',
+            'controller'         => 'mycontroller',
+            'action'             => 'myaction',
+            'route'              => 'myroute',
+            'params'             => array(
+                'page' => 1337
+            )
+        ));
+ 
+        $this->_front->getRouter()->addRoute(
+            'myroute',
+            new Zend_Controller_Router_Route(
+                'lolcat/:action/:page',
+                array(
+                    'module'     => 'default',
+                    'controller' => 'foobar',
+                    'action'     => 'bazbat',
+                    'page'       => 1
+                )
+            )
+        );
+ 
+        $this->assertEquals('/lolcat/myaction/1337#qux', $page->getHref());
+    } 
+
     public function testIsActiveReturnsTrueOnIdenticalModuleControllerAction()
     {
         $page = new Zend_Navigation_Page_Mvc(array(
@@ -353,6 +385,7 @@ class Zend_Navigation_Page_MvcTest extends PHPUnit_Framework_TestCase
             'action' => 'index',
             'controller' => 'index',
             'module' => 'test',
+            'fragmentIdentifier' => 'bar',
             'id' => 'my-id',
             'class' => 'my-class',
             'title' => 'my-title',

+ 18 - 0
tests/Zend/Navigation/Page/UriTest.php

@@ -100,4 +100,22 @@ class Zend_Navigation_Page_UriTest extends PHPUnit_Framework_TestCase
 
         $this->assertEquals($uri, $page->getHref());
     }
+
+    /**
+     * @group ZF-8922
+     */
+    public function testGetHrefWithFragmentIdentifier()
+    {
+        $uri = 'http://www.example.com/foo.html';
+        
+        $page = new Zend_Navigation_Page_Uri();
+        $page->setUri($uri);
+        $page->setFragmentIdentifier('bar');
+        
+        $this->assertEquals($uri . '#bar', $page->getHref());
+        
+        $page->setUri('#');
+        
+        $this->assertEquals('#bar', $page->getHref());
+    }
 }

+ 34 - 3
tests/Zend/Navigation/PageTest.php

@@ -162,6 +162,35 @@ class Zend_Navigation_PageTest extends PHPUnit_Framework_TestCase
         }
     }
 
+    /**
+     * @group ZF-8922
+     */
+    public function testSetAndGetFragmentIdentifier()
+    {
+        $page = Zend_Navigation_Page::factory(array(
+            'uri'                => '#',
+            'fragmentIdentifier' => 'foo',
+        ));
+        
+        $this->assertEquals('foo', $page->getFragmentIdentifier());
+        
+        $page->setFragmentIdentifier('bar');
+        $this->assertEquals('bar', $page->getFragmentIdentifier());
+        
+        $invalids = array(42, (object) null);
+        foreach ($invalids as $invalid) {
+            try {
+                $page->setFragmentIdentifier($invalid);
+                $this->fail('An invalid value was set, but a ' .
+                            'Zend_Navigation_Exception was not thrown');
+            } catch (Zend_Navigation_Exception $e) {
+                $this->assertContains(
+                    'Invalid argument: $fragmentIdentifier', $e->getMessage()
+                );
+            }
+        }
+    } 
+
     public function testSetAndGetId()
     {
         $page = Zend_Navigation_Page::factory(array(
@@ -1094,7 +1123,8 @@ class Zend_Navigation_PageTest extends PHPUnit_Framework_TestCase
     {
         $options = array(
             'label'    => 'foo',
-            'uri'      => '#',
+            'uri'      => 'http://www.example.com/foo.html',
+            'fragmentIdentifier' => 'bar',
             'id'       => 'my-id',
             'class'    => 'my-class',
             'title'    => 'my-title',
@@ -1114,11 +1144,11 @@ class Zend_Navigation_PageTest extends PHPUnit_Framework_TestCase
             'pages'    => array(
                 array(
                     'label' => 'foo.bar',
-                    'uri'   => '#'
+                    'uri'   => 'http://www.example.com/foo.html'
                 ),
                 array(
                     'label' => 'foo.baz',
-                    'uri'   => '#'
+                    'uri'   => 'http://www.example.com/foo.html'
                 )
             )
         );
@@ -1140,6 +1170,7 @@ class Zend_Navigation_PageTest extends PHPUnit_Framework_TestCase
 
         // tweak options to what we expect sub page 1 to be
         $options['label'] = 'foo.bar';
+        $options['fragmentIdentifier'] = null;
         $options['order'] = null;
         $options['id'] = null;
         $options['class'] = null;