diff options
| author | Philipp Schüttlöffel <schuettloeffel@zqs.uni-hannover.de> | 2024-09-24 10:53:31 +0200 |
|---|---|---|
| committer | Philipp Schüttlöffel <schuettloeffel@zqs.uni-hannover.de> | 2024-09-24 10:53:31 +0200 |
| commit | 4459dd7917f4d1c34f40bb68f0e991e9c3d53e4c (patch) | |
| tree | 5c07151ae61276d334e88f6309c30d439a85c12e /lib/classes/restapi/UriTemplate.php | |
| parent | da0022e5c1abbf9825ae76debaabdff7e8623bb4 (diff) | |
| parent | 97a188592c679890a25c37ab78463add76a52ff7 (diff) | |
Merge branch 'main' into issue-3911issue-3911
Diffstat (limited to 'lib/classes/restapi/UriTemplate.php')
| -rw-r--r-- | lib/classes/restapi/UriTemplate.php | 115 |
1 files changed, 0 insertions, 115 deletions
diff --git a/lib/classes/restapi/UriTemplate.php b/lib/classes/restapi/UriTemplate.php deleted file mode 100644 index 67161de..0000000 --- a/lib/classes/restapi/UriTemplate.php +++ /dev/null @@ -1,115 +0,0 @@ -<?php -namespace RESTAPI; - -/** - * @author Jan-Hendrik Willms <tleilax+studip@gmail.com> - * @author <mlunzena@uos.de> - * @license GPL 2 or later - * @since Stud.IP 3.0 - * @deprecated Since Stud.IP 5.0. Will be removed in Stud.IP 6.0. - */ -class UriTemplate -{ - public $uri_template; - public $conditions; - - public function __construct($uri_template, $conditions = []) - { - $this->uri_template = $uri_template; - $this->conditions = $conditions; - } - - /** - * Tests whether an uri matches a template. - * - * The template may contain placeholders by prefixing an appropriate, - * unique placeholder name with a colon (:). - * - * <code>$template = '/hello/:name';</code> - * - * If the uri matches the template, all evaluated placeholders will - * be stored in the parameters array. - * - * @param String $uri The uri to test - * @param array $parameters Stores evaluated parameters on match (optional) - * - * @return bool Returns true if the uri matches the template - */ - public function match($uri, &$parameters = null) - { - // Initialize parameters array - $parameters = []; - - // Split and normalize uri and template - $given = array_filter(explode('/', $uri), 'mb_strlen'); - $rules = array_filter(explode('/', $this->uri_template)); - - // Leave if uri and template do not contain the same number of - // elements - if (count($given) !== count($rules)) { - return false; - } - - // Combine uri and template element-wise (simplifies iteration) - $combined = array_combine($rules, $given); - - // Iterate over uri and template and compare element by element - foreach ($combined as $rule => $actual) { - if ($rule[0] === ':') { - // Rule is a placeholder - $parameter_name = mb_substr($rule, 1); - - if (isset($this->conditions[$parameter_name]) - && !preg_match($this->conditions[$parameter_name], $actual)) { - return false; - } - - $parameters[$parameter_name] = $actual; - - } elseif ($actual !== $rule) { - // Elements do not match - $parameters = []; - return false; - } - } - - return true; - } - - - public function inject($params) - { - // Initialize parameters array - $parameters = []; - - // Split and normalize template - $rules = array_filter(explode('/', $this->uri_template)); - - foreach ($rules as &$rule) { - - // Rule is a placeholder - if ($rule[0] === ':') { - $parameter_name = mb_substr($rule, 1); - - if (!isset($params[$parameter_name])) { - $reason = sprintf('UriTemplate parameter :%s missing.', - htmlReady($parameter_name)); - throw new \RuntimeException($reason); - } - - $actual = $params[$parameter_name]; - - if (isset($this->conditions[$parameter_name]) - && !preg_match($this->conditions[$parameter_name], $actual)) { - $reason = sprintf('UriTemplate parameter :%s did not satisfy its condition.', - htmlReady($parameter_name)); - throw new \RuntimeException($reason); - } - - $rule = htmlReady($actual); - } - } - - return join('/', $rules); - } -} |
