From 04c50779468cb06c9c18135a571d73e1ec2072c7 Mon Sep 17 00:00:00 2001 From: techmahedy Date: Fri, 30 Oct 2026 20:41:34 +0600 Subject: [PATCH 1/2] add string service contracts --- src/Phaseolies/Helpers/helpers.php | 8 +- src/Phaseolies/Launchers/FacadeLauncher.php | 7 +- .../Contracts/StringServiceInterface.php | 298 ++++++++++++++++++ src/Phaseolies/Support/Facades/Str.php | 2 +- src/Phaseolies/Support/StringService.php | 8 +- tests/StringServiceTest.php | 33 ++ 6 files changed, 346 insertions(+), 10 deletions(-) create mode 100644 src/Phaseolies/Support/Contracts/StringServiceInterface.php diff --git a/src/Phaseolies/Helpers/helpers.php b/src/Phaseolies/Helpers/helpers.php index 0617c08f..0014b577 100644 --- a/src/Phaseolies/Helpers/helpers.php +++ b/src/Phaseolies/Helpers/helpers.php @@ -3,7 +3,7 @@ use Phaseolies\Utilities\Paginator; use Phaseolies\Translation\Translator; use Phaseolies\Support\Contracts\UrlGeneratorInterface; -use Phaseolies\Support\StringService; +use Phaseolies\Support\Contracts\StringServiceInterface; use Phaseolies\Support\Facades\Log; use Phaseolies\Support\Facades\Crypt; use Phaseolies\Support\CookieJar; @@ -433,11 +433,11 @@ function is_auth(): bool if (!function_exists('str')) { /** - * Instance of the StringService class + * Instance of the string service * - * @return StringService + * @return StringServiceInterface */ - function str(): StringService + function str(): StringServiceInterface { return app('str'); } diff --git a/src/Phaseolies/Launchers/FacadeLauncher.php b/src/Phaseolies/Launchers/FacadeLauncher.php index 66394f60..4a562367 100644 --- a/src/Phaseolies/Launchers/FacadeLauncher.php +++ b/src/Phaseolies/Launchers/FacadeLauncher.php @@ -7,6 +7,7 @@ use Phaseolies\Support\Contracts\UrlGeneratorInterface; use Phaseolies\Support\ViteManager; use Phaseolies\Support\StringService; +use Phaseolies\Support\Contracts\StringServiceInterface; use Phaseolies\Support\Storage\StorageFileService; use Phaseolies\Support\Storage\Contracts\StorageInterface; use Phaseolies\Signals\SignalBus; @@ -116,9 +117,11 @@ public function register() return new Sanitizer([], []); }); - // Bind the 'str' service to a singleton instance of the StringService class. + // Bind the string service contract to a singleton implementation and + // retain the existing 'str' service name as an alias. // This handles string related helper jobs - $this->app->singleton('str', StringService::class); + $this->app->singleton(StringServiceInterface::class, StringService::class); + $this->app->alias(StringServiceInterface::class, 'str'); // Bind the 'cookie' service to a singleton instance of the CookieJar class. // This handles cookie related jobs diff --git a/src/Phaseolies/Support/Contracts/StringServiceInterface.php b/src/Phaseolies/Support/Contracts/StringServiceInterface.php new file mode 100644 index 00000000..c7829a82 --- /dev/null +++ b/src/Phaseolies/Support/Contracts/StringServiceInterface.php @@ -0,0 +1,298 @@ +assertFalse($this->stringService->isJson('Hello World')); $this->assertFalse($this->stringService->isJson('')); } + + public function testStringServiceConformsToTheContract() + { + $this->assertInstanceOf(StringServiceInterface::class, $this->stringService); + } + + public function testApplicationBindsTheStringContractAndLegacyServiceNameToTheSameSingleton() + { + $app = $this->getMockBuilder(Application::class)->disableOriginalConstructor()->onlyMethods([])->getMock(); + (new ReflectionClass(Container::class))->getConstructor()->invoke($app); + $previous = Container::getInstance(); + Container::setInstance($app); + + try { + $originalAppUrl = getenv('APP_URL'); + putenv('APP_URL=http://localhost'); + (new FacadeLauncher($app))->register(); + + $contract = $app->get(StringServiceInterface::class); + + $this->assertInstanceOf(StringService::class, $contract); + $this->assertSame($contract, $app->get('str')); + $this->assertSame($contract, str()); + } finally { + Container::setInstance($previous); + $originalAppUrl === false ? putenv('APP_URL') : putenv("APP_URL={$originalAppUrl}"); + } + } } From 5beb0dd4f3e9fdcc1ac64eb487c416923fcdcd27 Mon Sep 17 00:00:00 2001 From: techmahedy Date: Fri, 30 Oct 2026 20:46:17 +0600 Subject: [PATCH 2/2] fix doc comment --- .../Support/Contracts/StringServiceInterface.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/Phaseolies/Support/Contracts/StringServiceInterface.php b/src/Phaseolies/Support/Contracts/StringServiceInterface.php index c7829a82..cf4f4f33 100644 --- a/src/Phaseolies/Support/Contracts/StringServiceInterface.php +++ b/src/Phaseolies/Support/Contracts/StringServiceInterface.php @@ -60,7 +60,8 @@ public function camel(string $input): string; * @param string $string * @param int $visibleFromStart * @param int $visibleFromEnd - * @param string + * @param string $maskCharacter + * @return string */ public function mask(string $string, int $visibleFromStart = 1, int $visibleFromEnd = 1, string $maskCharacter = '*'): string; @@ -110,8 +111,7 @@ public function slug(string $input, string $separator = '-'): string; public function contains(string $haystack, string | array $needles, bool $ignoreCase = true): bool; /** - * Limit the number of words in a string, handling Unicode and preserving - * natural word boundaries (splitting on whitespace). + * Limit the number of words in a string, handling Unicode and preserving natural word boundaries * * @param string $string * @param int $words @@ -155,8 +155,7 @@ public function uuid(): string; public function startsWith(string $haystack, string | array $needles): bool; /** - * Check if a string ends with one or more given needles (case‐sensitive). - * This version is Unicode‐safe (works with any UTF‐8 text). + * Check if a string ends with one or more given needles (case-sensitive). * * @param string $haystack * @param string|array $needles