|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\Core; |
| 4 | + |
| 5 | +/** |
| 6 | + * Defines an object that holds information about a URL. |
| 7 | + */ |
| 8 | +class Url { |
| 9 | + |
| 10 | + /** |
| 11 | + * The URL options. |
| 12 | + * |
| 13 | + * See \Drupal\Core\Url::fromUri() for details on the options. |
| 14 | + * |
| 15 | + * @var array |
| 16 | + */ |
| 17 | + protected $options = array(); |
| 18 | + |
| 19 | + /** |
| 20 | + * The URI. |
| 21 | + * |
| 22 | + * @var string |
| 23 | + */ |
| 24 | + protected $uri; |
| 25 | + |
| 26 | + /** |
| 27 | + * Constructs a new Url object. |
| 28 | + * |
| 29 | + * In most cases, use Url::fromRoute() rather than |
| 30 | + * constructing Url objects directly in order to avoid ambiguity and make your |
| 31 | + * code more self-documenting. |
| 32 | + * |
| 33 | + * @param string $uri |
| 34 | + * The uri |
| 35 | + * @param array $options |
| 36 | + * See \Drupal\Core\Url::fromUri() for details. |
| 37 | + * |
| 38 | + * @see static::fromRoute() |
| 39 | + * @see static::fromUri() |
| 40 | + * |
| 41 | + * @todo Update this documentation for non-routed URIs in |
| 42 | + * https://www.drupal.org/node/2346787 |
| 43 | + */ |
| 44 | + public function __construct($uri, $options = array()) { |
| 45 | + $this->uri = $uri; |
| 46 | + $this->options = $options; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Creates a new Url object from a URI. |
| 51 | + * |
| 52 | + * @param string $uri |
| 53 | + * The URI of the resource including the scheme. |
| 54 | + * @param array $options |
| 55 | + * (optional) An associative array of additional URL options, with the |
| 56 | + * following elements: |
| 57 | + * - 'attributes': An associative array of HTML attributes that will be |
| 58 | + * added to the anchor tag if you use the \Drupal\Core\Link class to make |
| 59 | + * the link. |
| 60 | + * |
| 61 | + * @return \Drupal\Core\Url |
| 62 | + * A new Url object. |
| 63 | + */ |
| 64 | + public static function fromUri($uri, $options = []) { |
| 65 | + $url = new static($uri, $options); |
| 66 | + |
| 67 | + return $url; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Returns the URL options. |
| 72 | + * |
| 73 | + * @return array |
| 74 | + * The array of options. See \Drupal\Core\Url::fromUri() for details on what |
| 75 | + * it contains. |
| 76 | + */ |
| 77 | + public function getOptions() { |
| 78 | + return $this->options; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Gets a specific option. |
| 83 | + * |
| 84 | + * See \Drupal\Core\Url::fromUri() for details on the options. |
| 85 | + * |
| 86 | + * @param string $name |
| 87 | + * The name of the option. |
| 88 | + * |
| 89 | + * @return mixed |
| 90 | + * The value for a specific option, or NULL if it does not exist. |
| 91 | + */ |
| 92 | + public function getOption($name) { |
| 93 | + if (!isset($this->options[$name])) { |
| 94 | + return NULL; |
| 95 | + } |
| 96 | + |
| 97 | + return $this->options[$name]; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Sets the URL options. |
| 102 | + * |
| 103 | + * @param array $options |
| 104 | + * The array of options. See \Drupal\Core\Url::fromUri() for details on what |
| 105 | + * it contains. |
| 106 | + * |
| 107 | + * @return $this |
| 108 | + */ |
| 109 | + public function setOptions($options) { |
| 110 | + $this->options = $options; |
| 111 | + return $this; |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Sets a specific option. |
| 116 | + * |
| 117 | + * See \Drupal\Core\Url::fromUri() for details on the options. |
| 118 | + * |
| 119 | + * @param string $name |
| 120 | + * The name of the option. |
| 121 | + * @param mixed $value |
| 122 | + * The option value. |
| 123 | + * |
| 124 | + * @return $this |
| 125 | + */ |
| 126 | + public function setOption($name, $value) { |
| 127 | + $this->options[$name] = $value; |
| 128 | + return $this; |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Returns the URI value for this Url object. |
| 133 | + * |
| 134 | + * @return string |
| 135 | + * A URI. |
| 136 | + */ |
| 137 | + public function getUri() { |
| 138 | + return $this->uri; |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Generates the string URL representation for this Url object. |
| 143 | + * |
| 144 | + * @return string |
| 145 | + * A string URL. |
| 146 | + */ |
| 147 | + public function __tostring() { |
| 148 | + return $this->uri; |
| 149 | + } |
| 150 | + |
| 151 | +} |
0 commit comments