Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Merge branch 'feature/navigation-refactor' of https://github.com/fros…
Browse files Browse the repository at this point in the history
…chdesign/zf2 into feature/navigation-refactor-cs
  • Loading branch information
Show file tree
Hide file tree
Showing 12 changed files with 336 additions and 188 deletions.
224 changes: 122 additions & 102 deletions src/AbstractPage.php

Large diffs are not rendered by default.

97 changes: 52 additions & 45 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* @namespace
*/
namespace Zend\Navigation;

use Zend\Config;

/**
Expand All @@ -46,21 +47,21 @@ abstract class Container implements \RecursiveIterator, \Countable
*
* @var array
*/
protected $_pages = array();
protected $pages = array();

/**
* An index that contains the order in which to iterate pages
*
* @var array
*/
protected $_index = array();
protected $index = array();

/**
* Whether index is dirty and needs to be re-arranged
*
* @var bool
*/
protected $_dirtyIndex = false;
protected $dirtyIndex = false;

// Internal methods:

Expand All @@ -71,11 +72,11 @@ abstract class Container implements \RecursiveIterator, \Countable
*/
protected function _sort()
{
if ($this->_dirtyIndex) {
if ($this->dirtyIndex) {
$newIndex = array();
$index = 0;

foreach ($this->_pages as $hash => $page) {
foreach ($this->pages as $hash => $page) {
$order = $page->getOrder();
if ($order === null) {
$newIndex[$hash] = $index;
Expand All @@ -86,8 +87,8 @@ protected function _sort()
}

asort($newIndex);
$this->_index = $newIndex;
$this->_dirtyIndex = false;
$this->index = $newIndex;
$this->dirtyIndex = false;
}
}

Expand All @@ -100,7 +101,7 @@ protected function _sort()
*/
public function notifyOrderUpdated()
{
$this->_dirtyIndex = true;
$this->dirtyIndex = true;
}

/**
Expand All @@ -118,28 +119,30 @@ public function addPage($page)
{
if ($page === $this) {
throw new Exception\InvalidArgumentException(
'A page cannot have itself as a parent');
'A page cannot have itself as a parent'
);
}

if (is_array($page) || $page instanceof Config\Config) {
$page = AbstractPage::factory($page);
} elseif (!$page instanceof AbstractPage) {
throw new Exception\InvalidArgumentException(
'Invalid argument: $page must be an instance of ' .
'Zend_Navigation_Page or Zend_Config, or an array');
'Invalid argument: $page must be an instance of '
. 'Zend_Navigation_Page or Zend_Config, or an array'
);
}

$hash = $page->hashCode();

if (array_key_exists($hash, $this->_index)) {
if (array_key_exists($hash, $this->index)) {
// page is already in container
return $this;
}

// adds page to container and sets dirty flag
$this->_pages[$hash] = $page;
$this->_index[$hash] = $page->getOrder();
$this->_dirtyIndex = true;
$this->pages[$hash] = $page;
$this->index[$hash] = $page->getOrder();
$this->dirtyIndex = true;

// inject self as page parent
$page->setParent($this);
Expand Down Expand Up @@ -201,7 +204,7 @@ public function setPages(array $pages)
*/
public function getPages()
{
return $this->_pages;
return $this->pages;
}

/**
Expand All @@ -218,17 +221,17 @@ public function removePage($page)
$hash = $page->hashCode();
} elseif (is_int($page)) {
$this->_sort();
if (!$hash = array_search($page, $this->_index)) {
if (!$hash = array_search($page, $this->index)) {
return false;
}
} else {
return false;
}

if (isset($this->_pages[$hash])) {
unset($this->_pages[$hash]);
unset($this->_index[$hash]);
$this->_dirtyIndex = true;
if (isset($this->pages[$hash])) {
unset($this->pages[$hash]);
unset($this->index[$hash]);
$this->dirtyIndex = true;
return true;
}

Expand All @@ -242,8 +245,8 @@ public function removePage($page)
*/
public function removePages()
{
$this->_pages = array();
$this->_index = array();
$this->pages = array();
$this->index = array();
return $this;
}

Expand All @@ -257,10 +260,10 @@ public function removePages()
*/
public function hasPage(AbstractPage $page, $recursive = false)
{
if (array_key_exists($page->hashCode(), $this->_index)) {
if (array_key_exists($page->hashCode(), $this->index)) {
return true;
} elseif ($recursive) {
foreach ($this->_pages as $childPage) {
foreach ($this->pages as $childPage) {
if ($childPage->hasPage($page, true)) {
return true;
}
Expand All @@ -277,7 +280,7 @@ public function hasPage(AbstractPage $page, $recursive = false)
*/
public function hasPages()
{
return count($this->_index) > 0;
return count($this->index) > 0;
}

/**
Expand Down Expand Up @@ -369,10 +372,13 @@ public function __call($method, $arguments)
return $this->{$match[1]}($match[2], $arguments[0]);
}

throw new Exception\BadMethodCallException(sprintf(
throw new Exception\BadMethodCallException(
sprintf(
'Bad method call: Unknown method %s::%s',
get_class($this),
$method));
$method
)
);
}

/**
Expand All @@ -384,11 +390,11 @@ public function toArray()
{
$pages = array();

$this->_dirtyIndex = true;
$this->dirtyIndex = true;
$this->_sort();
$indexes = array_keys($this->_index);
$indexes = array_keys($this->index);
foreach ($indexes as $hash) {
$pages[] = $this->_pages[$hash]->toArray();
$pages[] = $this->pages[$hash]->toArray();
}
return $pages;
}
Expand All @@ -406,15 +412,16 @@ public function toArray()
public function current()
{
$this->_sort();
current($this->_index);
$hash = key($this->_index);
current($this->index);
$hash = key($this->index);

if (isset($this->_pages[$hash])) {
return $this->_pages[$hash];
if (isset($this->pages[$hash])) {
return $this->pages[$hash];
} else {
throw new Exception\OutOfBoundsException(
'Corruption detected in container; ' .
'invalid key found in internal iterator');
'Corruption detected in container; '
. 'invalid key found in internal iterator'
);
}
}

Expand All @@ -428,7 +435,7 @@ public function current()
public function key()
{
$this->_sort();
return key($this->_index);
return key($this->index);
}

/**
Expand All @@ -441,7 +448,7 @@ public function key()
public function next()
{
$this->_sort();
next($this->_index);
next($this->index);
}

/**
Expand All @@ -454,7 +461,7 @@ public function next()
public function rewind()
{
$this->_sort();
reset($this->_index);
reset($this->index);
}

/**
Expand All @@ -467,7 +474,7 @@ public function rewind()
public function valid()
{
$this->_sort();
return current($this->_index) !== false;
return current($this->index) !== false;
}

/**
Expand All @@ -491,10 +498,10 @@ public function hasChildren()
*/
public function getChildren()
{
$hash = key($this->_index);
$hash = key($this->index);

if (isset($this->_pages[$hash])) {
return $this->_pages[$hash];
if (isset($this->pages[$hash])) {
return $this->pages[$hash];
}

return null;
Expand All @@ -511,6 +518,6 @@ public function getChildren()
*/
public function count()
{
return count($this->_index);
return count($this->index);
}
}
6 changes: 3 additions & 3 deletions src/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Navigation
* @category Zend
* @package Zend_Navigation
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

/**
Expand Down
8 changes: 6 additions & 2 deletions src/Exception/BadMethodCallException.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
<?php

/**
* @namespace
*/
namespace Zend\Navigation\Exception;

use Zend\Navigation\Exception;

class BadMethodCallException extends \InvalidArgumentException implements Exception
class BadMethodCallException extends \InvalidArgumentException
implements Exception
{

}
5 changes: 4 additions & 1 deletion src/Exception/DomainException.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
<?php

/**
* @namespace
*/
namespace Zend\Navigation\Exception;

use Zend\Navigation\Exception;

class DomainException extends \DomainException implements Exception
{

}
8 changes: 6 additions & 2 deletions src/Exception/InvalidArgumentException.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
<?php

/**
* @namespace
*/
namespace Zend\Navigation\Exception;

use Zend\Navigation\Exception;

class InvalidArgumentException extends \InvalidArgumentException implements Exception
class InvalidArgumentException extends \InvalidArgumentException
implements Exception
{

}
8 changes: 6 additions & 2 deletions src/Exception/OutOfBoundsException.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
<?php

/**
* @namespace
*/
namespace Zend\Navigation\Exception;

use Zend\Navigation\Exception;

class OutOfBoundsException extends \InvalidArgumentException implements Exception
class OutOfBoundsException extends \InvalidArgumentException
implements Exception
{

}
5 changes: 3 additions & 2 deletions src/Navigation.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ public function __construct($pages = null)
$this->addPages($pages);
} elseif (null !== $pages) {
throw new Exception\InvalidArgumentException(
'Invalid argument: $pages must be an array, an ' .
'instance of Zend_Config, or null');
'Invalid argument: $pages must be an array, an '
. 'instance of Zend_Config, or null'
);
}
}
}
Loading

0 comments on commit ecbe771

Please sign in to comment.