| 1: | <?php |
| 2: | /** |
| 3: | * @package OpenCart |
| 4: | * |
| 5: | * @author Daniel Kerr |
| 6: | * @copyright Copyright (c) 2005 - 2022, OpenCart, Ltd. (https://www.opencart.com/) |
| 7: | * @license https://opensource.org/licenses/GPL-3.0 |
| 8: | * @author Daniel Kerr |
| 9: | * |
| 10: | * @see https://www.opencart.com |
| 11: | */ |
| 12: | namespace Opencart\System\Library; |
| 13: | /** |
| 14: | * Class URL |
| 15: | */ |
| 16: | class Url { |
| 17: | /** |
| 18: | * @var string |
| 19: | */ |
| 20: | private string $url; |
| 21: | /** |
| 22: | * @var array<int, object> |
| 23: | */ |
| 24: | private array $rewrite = []; |
| 25: | |
| 26: | /** |
| 27: | * Constructor |
| 28: | * |
| 29: | * @param string $url |
| 30: | */ |
| 31: | public function __construct(string $url) { |
| 32: | $this->url = $url; |
| 33: | } |
| 34: | |
| 35: | /** |
| 36: | * addRewrite |
| 37: | * |
| 38: | * Add a rewrite method to the URL system |
| 39: | * |
| 40: | * @param \Opencart\System\Engine\Controller $rewrite |
| 41: | * |
| 42: | * @return void |
| 43: | */ |
| 44: | public function addRewrite(object $rewrite): void { |
| 45: | if (is_callable([$rewrite, 'rewrite'])) { |
| 46: | $this->rewrite[] = $rewrite; |
| 47: | } |
| 48: | } |
| 49: | |
| 50: | /** |
| 51: | * Link |
| 52: | * |
| 53: | * Generates a URL |
| 54: | * |
| 55: | * @param string $route |
| 56: | * @param mixed $args |
| 57: | * @param bool $js |
| 58: | * |
| 59: | * @return string |
| 60: | */ |
| 61: | public function link(string $route, $args = '', bool $js = false): string { |
| 62: | $url = $this->url . 'index.php?route=' . $route; |
| 63: | |
| 64: | if ($args) { |
| 65: | if (is_array($args)) { |
| 66: | $url .= '&' . http_build_query($args); |
| 67: | } else { |
| 68: | $url .= '&' . trim($args, '&'); |
| 69: | } |
| 70: | } |
| 71: | |
| 72: | foreach ($this->rewrite as $rewrite) { |
| 73: | $url = $rewrite->rewrite($url); |
| 74: | } |
| 75: | |
| 76: | if (!$js) { |
| 77: | return str_replace('&', '&', $url); |
| 78: | } else { |
| 79: | return $url; |
| 80: | } |
| 81: | } |
| 82: | } |
| 83: |