From 248ea089fab6661e8015ba3f3c9ca419590bf52d Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Mon, 24 Jul 2023 19:22:58 +0200 Subject: [PATCH] UrlRequest: add current_url_starts_with, current_url_ends_with and current_url_match helper methods --- src/UrlRequest.php | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/src/UrlRequest.php b/src/UrlRequest.php index c401078..5dc0cc1 100644 --- a/src/UrlRequest.php +++ b/src/UrlRequest.php @@ -9,7 +9,7 @@ namespace EesyPHP; * @property-read string|null $current_url; * @property-read string|null $current_full_url; * @property-read array|null $handler; - * @property-read bool $authenticated; + * @property-read bool|null $authenticated; * @property-read bool $api_mode; */ class UrlRequest { @@ -171,6 +171,37 @@ class UrlRequest { 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