/library/Zend/Mvc/Router/Http/Literal.php

https://github.com/noose/zf2 · PHP · 126 lines · 51 code · 12 blank · 63 comment · 10 complexity · 9cf6be25b169b38de07f14c5b42802e5 MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Router
  17. * @subpackage Route
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * @namespace
  23. */
  24. namespace Zend\Mvc\Router\Http;
  25. use Traversable,
  26. Zend\Config\Config,
  27. Zend\Http\Request,
  28. Zend\Mvc\Router\Exception,
  29. Zend\Mvc\Router\Route,
  30. Zend\Mvc\Router\RouteMatch;
  31. /**
  32. * Literal route.
  33. *
  34. * @package Zend_Router
  35. * @subpackage Route
  36. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  37. * @license http://framework.zend.com/license/new-bsd New BSD License
  38. * @see http://manuals.rubyonrails.com/read/chapter/65
  39. */
  40. class Literal implements Route
  41. {
  42. /**
  43. * Route to match.
  44. *
  45. * @var string
  46. */
  47. protected $route;
  48. /**
  49. * Default values.
  50. *
  51. * @var array
  52. */
  53. protected $defaults;
  54. /**
  55. * __construct(): defined by Route interface.
  56. *
  57. * @see Route::__construct()
  58. * @param mixed $options
  59. * @return void
  60. */
  61. public function __construct($options = null)
  62. {
  63. if ($options instanceof Config) {
  64. $options = $options->toArray();
  65. } elseif ($options instanceof Traversable) {
  66. $options = iterator_to_array($options);
  67. }
  68. if (!is_array($options)) {
  69. throw new Exception\InvalidArgumentException('Options must either be an array or a Traversable object');
  70. }
  71. if (!isset($options['route']) || !is_string($options['route'])) {
  72. throw new Exception\InvalidArgumentException('Route not defined nor not a string');
  73. }
  74. if (!isset($options['defaults']) || !is_array($options['defaults'])) {
  75. throw new Exception\InvalidArgumentException('Defaults not defined nor not an array');
  76. }
  77. $this->route = $options['route'];
  78. $this->defaults = $options['defaults'];
  79. }
  80. /**
  81. * match(): defined by Route interface.
  82. *
  83. * @see Route::match()
  84. * @param Request $request
  85. * @return RouteMatch
  86. */
  87. public function match(Request $request, $pathOffset = null)
  88. {
  89. $uri = $request->uri();
  90. $path = $uri->getPath();
  91. if ($pathOffset !== null) {
  92. if (strpos($path, $this->route) === $pathOffset) {
  93. return new RouteMatch($this->defaults, $this);
  94. }
  95. } else {
  96. if ($path === $this->route) {
  97. return new RouteMatch($this->defaults, $this);
  98. }
  99. }
  100. return null;
  101. }
  102. /**
  103. * assemble(): Defined by Route interface.
  104. *
  105. * @see Route::assemble()
  106. * @param array $params
  107. * @param array $options
  108. * @return mixed
  109. */
  110. public function assemble(array $params = null, array $options = null)
  111. {
  112. return $this->route;
  113. }
  114. }