Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.
   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  /**
  17   * Contains the url class, providing a representation of a url and operations on its component parts.
  18   *
  19   * @package tool_moodlenet
  20   * @copyright 2020 Jake Dallimore <jrhdallimore@gmail.com>
  21   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22   */
  23  namespace tool_moodlenet\local;
  24  
  25  /**
  26   * The url class, providing a representation of a url and operations on its component parts.
  27   *
  28   * @copyright 2020 Jake Dallimore <jrhdallimore@gmail.com>
  29   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30   */
  31  class url {
  32  
  33      /** @var string $url the full URL string.*/
  34      protected $url;
  35  
  36      /** @var string|null $path the path component of this URL.*/
  37      protected $path;
  38  
  39      /** @var host|null $host the host component of this URL.*/
  40      protected $host;
  41  
  42      /**
  43       * The url constructor.
  44       *
  45       * @param string $url the URL string.
  46       * @throws \coding_exception if the URL does not pass syntax validation.
  47       */
  48      public function __construct(string $url) {
  49          // This object supports URLs as per the spec, so non-ascii chars must be encoded as per IDNA rules.
  50          if (!filter_var($url, FILTER_VALIDATE_URL)) {
  51              throw new \coding_exception('Malformed URL');
  52          }
  53          $this->url = $url;
  54          $this->path = parse_url($url, PHP_URL_PATH);
  55          $this->host = parse_url($url, PHP_URL_HOST);
  56      }
  57  
  58      /**
  59       * Get the path component of the URL.
  60       *
  61       * @return string|null the path component of the URL.
  62       */
  63      public function get_path(): ?string {
  64          return $this->path;
  65      }
  66  
  67      /**
  68       * Return the domain component of the URL.
  69       *
  70       * @return string|null the domain component of the URL.
  71       */
  72      public function get_host(): ?string {
  73          return $this->host;
  74      }
  75  
  76      /**
  77       * Return the full URL string.
  78       *
  79       * @return string the full URL string.
  80       */
  81      public function get_value() {
  82          return  $this->url;
  83      }
  84  }