UrlRequest: add current_url_starts_with, current_url_ends_with and current_url_match helper methods

This commit is contained in:
Benjamin Renard 2023-07-24 19:22:58 +02:00
parent ff9f623f99
commit 248ea089fa
Signed by: bn8
GPG key ID: 3E2E1CE1907115BC

View file

@ -9,7 +9,7 @@ namespace EesyPHP;
* @property-read string|null $current_url; * @property-read string|null $current_url;
* @property-read string|null $current_full_url; * @property-read string|null $current_full_url;
* @property-read array|null $handler; * @property-read array|null $handler;
* @property-read bool $authenticated; * @property-read bool|null $authenticated;
* @property-read bool $api_mode; * @property-read bool $api_mode;
*/ */
class UrlRequest { class UrlRequest {
@ -171,6 +171,37 @@ class UrlRequest {
return null; return null;
} }
/**
* Check if current URL starts with the provided pattern
* @param string $pattern The lookuped pattern
* @return boolean
*/
public function current_url_starts_with($pattern) {
if (!function_exists('str_starts_with'))
return strpos($this->current_url, $pattern) === 0;
return str_starts_with($this->current_url, $pattern);
}
/**
* Check if current URL ends with the provided pattern
* @param string $pattern The lookuped pattern
* @return boolean
*/
public function current_url_ends_with($pattern) {
if (!function_exists('str_ends_with'))
return strpos($this->current_url, $pattern) === (strlen($this->current_url) - strlen($pattern));
return str_ends_with($this->current_url, $pattern);
}
/**
* Check if current URL matched with the provided pattern
* @param string $pattern A valid PREG pattern
* @return boolean
*/
public function current_url_match($pattern) {
return boolval(preg_match($pattern, $this->current_url));
}
} }
# vim: tabstop=2 shiftwidth=2 softtabstop=2 expandtab # vim: tabstop=2 shiftwidth=2 softtabstop=2 expandtab