Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Mvc / Router / PriorityList.php
blob15cd823a708a79ce4cc4878d56422ce58ce3be95
1 <?php
2 /**
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
8 */
10 namespace Zend\Mvc\Router;
12 use Countable;
13 use Iterator;
15 /**
16 * Priority list
18 class PriorityList implements Iterator, Countable
20 /**
21 * Internal list of all routes.
23 * @var array
25 protected $routes = array();
27 /**
28 * Serial assigned to routes to preserve LIFO.
30 * @var int
32 protected $serial = 0;
34 /**
35 * Internal counter to avoid usage of count().
37 * @var int
39 protected $count = 0;
41 /**
42 * Whether the list was already sorted.
44 * @var bool
46 protected $sorted = false;
48 /**
49 * Insert a new route.
51 * @param string $name
52 * @param RouteInterface $route
53 * @param int $priority
54 * @return void
56 public function insert($name, RouteInterface $route, $priority)
58 $this->sorted = false;
59 $this->count++;
61 $this->routes[$name] = array(
62 'route' => $route,
63 'priority' => (int) $priority,
64 'serial' => $this->serial++,
68 /**
69 * Remove a route.
71 * @param string $name
72 * @return void
74 public function remove($name)
76 if (!isset($this->routes[$name])) {
77 return;
80 $this->count--;
82 unset($this->routes[$name]);
85 /**
86 * Remove all routes.
88 * @return void
90 public function clear()
92 $this->routes = array();
93 $this->serial = 0;
94 $this->count = 0;
95 $this->sorted = false;
98 /**
99 * Get a route.
101 * @param string $name
102 * @return RouteInterface
104 public function get($name)
106 if (!isset($this->routes[$name])) {
107 return null;
110 return $this->routes[$name]['route'];
114 * Sort all routes.
116 * @return void
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
129 * @return int
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()
144 * @return void
146 public function rewind()
148 if (!$this->sorted) {
149 $this->sort();
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()
171 * @return string
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()
194 * @return bool
196 public function valid()
198 return ($this->current() !== false);
202 * count(): defined by Countable interface.
204 * @see Countable::count()
205 * @return int
207 public function count()
209 return $this->count;