-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from midnite81/feature/ensure-identifiers-are-e…
…xcaped-correctly Enhance Guardian identifier handling
- Loading branch information
Showing
4 changed files
with
221 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Midnite81\Guardian\Helpers; | ||
|
||
/** | ||
* Class Str | ||
* | ||
* A utility class for string manipulation operations. | ||
* | ||
* @internal This class is not part of the public API and may change without notice. | ||
*/ | ||
class Str | ||
{ | ||
/** | ||
* Constructor. | ||
* | ||
* @param string $string The initial string to manipulate. | ||
*/ | ||
public function __construct(protected string $string) | ||
{ | ||
} | ||
|
||
/** | ||
* Create a new instance of Str with the given string. | ||
* | ||
* @param string $string The string to manipulate. | ||
* @return Str | ||
*/ | ||
public static function of(string $string): Str | ||
{ | ||
return new Str($string); | ||
} | ||
|
||
/** | ||
* Get the current string value. | ||
* | ||
* @return string | ||
*/ | ||
public function toString(): string | ||
{ | ||
return $this->string; | ||
} | ||
|
||
/** | ||
* Convert the string to lowercase. | ||
* | ||
* @return $this | ||
*/ | ||
public function toLower(): Str | ||
{ | ||
$this->string = strtolower($this->string); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Remove duplicate occurrences of specified character(s). | ||
* | ||
* @param string|array<int, string> $characters The character(s) to remove duplicates of. | ||
* @return $this | ||
*/ | ||
public function removeDuplicateCharacters(string|array $characters = []): Str | ||
{ | ||
if (is_string($characters)) { | ||
$characters = [$characters]; | ||
} | ||
|
||
$pattern = '/(' . implode('|', array_map('preg_quote', $characters, array_fill(0, count($characters), '/'))) . ')\1+/u'; | ||
$this->string = (string) preg_replace($pattern, '$1', $this->string); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Remove the final character if it matches the specified character. | ||
* | ||
* @param string $character The character to remove if it's at the end. | ||
* @return $this | ||
*/ | ||
public function removeFinalCharIf(string $character): Str | ||
{ | ||
$this->string = rtrim($this->string, '_'); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Limit the string to a specified number of characters. | ||
* | ||
* @param int $numberOfCharacters The maximum number of characters to keep. | ||
* @return $this | ||
*/ | ||
public function limit(int $numberOfCharacters): Str | ||
{ | ||
$this->string = substr($this->string, 0, $numberOfCharacters); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Modify the string using a custom callback function. | ||
* | ||
* @param callable $callback A function that takes a string and returns a modified string. | ||
* @return $this | ||
*/ | ||
public function modify(callable $callback): Str | ||
{ | ||
$this->string = $callback($this->string); | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Midnite81\Guardian\Helpers\Str; | ||
|
||
it('creates a new instance with a given string', function () { | ||
$str = Str::of('Hello World'); | ||
expect($str)->toBeInstanceOf(Str::class); | ||
expect($str->toString())->toBe('Hello World'); | ||
}); | ||
|
||
it('converts string to lowercase', function () { | ||
$str = Str::of('HELLO WORLD'); | ||
expect($str->toLower()->toString())->toBe('hello world'); | ||
}); | ||
|
||
it('removes duplicate characters', function () { | ||
$str = Str::of('aabbccddee'); | ||
expect($str->removeDuplicateCharacters(['a', 'b'])->toString())->toBe('abccddee'); | ||
|
||
$str = Str::of('aabbccddee'); | ||
expect($str->removeDuplicateCharacters('a')->toString())->toBe('abbccddee'); | ||
|
||
$str = Str::of('a__b__c__'); | ||
expect($str->removeDuplicateCharacters('_')->toString())->toBe('a_b_c_'); | ||
}); | ||
|
||
it('removes final character if it matches', function () { | ||
$str = Str::of('Hello_'); | ||
expect($str->removeFinalCharIf('_')->toString())->toBe('Hello'); | ||
|
||
$str = Str::of('Hello'); | ||
expect($str->removeFinalCharIf('_')->toString())->toBe('Hello'); | ||
}); | ||
|
||
it('limits the string to specified number of characters', function () { | ||
$str = Str::of('Hello World'); | ||
expect($str->limit(5)->toString())->toBe('Hello'); | ||
|
||
$str = Str::of('Hi'); | ||
expect($str->limit(5)->toString())->toBe('Hi'); | ||
}); | ||
|
||
it('modifies the string using a custom callback', function () { | ||
$str = Str::of('hello world'); | ||
$result = $str->modify(function ($string) { | ||
return strtoupper($string); | ||
}); | ||
expect($result->toString())->toBe('HELLO WORLD'); | ||
}); | ||
|
||
it('chains multiple operations', function () { | ||
$str = Str::of('HELLO__WORLD__'); | ||
$result = $str->toLower() | ||
->removeDuplicateCharacters('_') | ||
->removeFinalCharIf('_') | ||
->limit(10); | ||
expect($result->toString())->toBe('hello_worl'); | ||
}); |