3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Mvc\Router
;
18 class PriorityList
implements Iterator
, Countable
21 * Internal list of all routes.
25 protected $routes = array();
28 * Serial assigned to routes to preserve LIFO.
32 protected $serial = 0;
35 * Internal counter to avoid usage of count().
42 * Whether the list was already sorted.
46 protected $sorted = false;
52 * @param RouteInterface $route
53 * @param int $priority
56 public function insert($name, RouteInterface
$route, $priority)
58 $this->sorted
= false;
61 $this->routes
[$name] = array(
63 'priority' => (int) $priority,
64 'serial' => $this->serial++
,
74 public function remove($name)
76 if (!isset($this->routes
[$name])) {
82 unset($this->routes
[$name]);
90 public function clear()
92 $this->routes
= array();
95 $this->sorted
= false;
101 * @param string $name
102 * @return RouteInterface
104 public function get($name)
106 if (!isset($this->routes
[$name])) {
110 return $this->routes
[$name]['route'];
118 protected function sort()
120 uasort($this->routes
, array($this, 'compare'));
121 $this->sorted
= true;
125 * Compare the priority of two routes.
127 * @param array $route1,
128 * @param array $route2
131 protected function compare(array $route1, array $route2)
133 if ($route1['priority'] === $route2['priority']) {
134 return ($route1['serial'] > $route2['serial'] ?
-1 : 1);
137 return ($route1['priority'] > $route2['priority'] ?
-1 : 1);
141 * rewind(): defined by Iterator interface.
143 * @see Iterator::rewind()
146 public function rewind()
148 if (!$this->sorted
) {
152 reset($this->routes
);
156 * current(): defined by Iterator interface.
158 * @see Iterator::current()
159 * @return RouteInterface
161 public function current()
163 $node = current($this->routes
);
164 return ($node !== false ?
$node['route'] : false);
168 * key(): defined by Iterator interface.
170 * @see Iterator::key()
173 public function key()
175 return key($this->routes
);
179 * next(): defined by Iterator interface.
181 * @see Iterator::next()
182 * @return RouteInterface
184 public function next()
186 $node = next($this->routes
);
187 return ($node !== false ?
$node['route'] : false);
191 * valid(): defined by Iterator interface.
193 * @see Iterator::valid()
196 public function valid()
198 return ($this->current() !== false);
202 * count(): defined by Countable interface.
204 * @see Countable::count()
207 public function count()