Skip to content

Commit

Permalink
Added "only" and "except" arguments for rule filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
toonvandenbos committed Jul 24, 2024
1 parent c6e2516 commit 99ef657
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 11 deletions.
8 changes: 4 additions & 4 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ public function register()
*/
public function boot()
{
Str::macro('typography', function (string $value) {
return (new Typography($value))->handle();
Str::macro('typography', function (string $value, null|string|array $only = null, null|string|array $except = null) {
return (new Typography($value))->handle(only: $only, except: $except);
});
Stringable::macro('typography', function () {
return new static(Str::typography($this->value));
Stringable::macro('typography', function (null|string|array $only = null, null|string|array $except = null) {
return new static(Str::typography($this->value, $only, $except));
});
}
}
19 changes: 14 additions & 5 deletions src/Typography.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,20 @@ static public function remove(string $key): void
}

/**
* Remove a typographic rule handler.
* Return the requested handlers.
*/
static protected function getHandlers(): array
static protected function getHandlers(null|string|array $only = null, null|string|array $except = null): array
{
return array_reduce(static::$handlers, function($all, $handler) {
$only = ($only ? (is_array($only) ? $only : [$only]) : null);
$except = ($except ? (is_array($except) ? $except : [$except]) : null);

$handlers = match (true) {
!is_null($only) => array_filter(static::$handlers, fn($key) => in_array($key, $only), ARRAY_FILTER_USE_KEY),
!is_null($except) => array_filter(static::$handlers, fn($key) => !in_array($key, $except), ARRAY_FILTER_USE_KEY),
default => static::$handlers,
};

return array_reduce($handlers, function($all, $handler) {
[$regex, $callback] = $handler;
$all[$regex] = $callback;
return $all;
Expand All @@ -57,10 +66,10 @@ public function __construct(string $value)
/**
* Run the value transformations.
*/
public function handle(): string
public function handle(null|string|array $only = null, null|string|array $except = null): string
{
return preg_replace_callback_array(
static::getHandlers(),
static::getHandlers(only: $only, except: $except),
$this->value
);
}
Expand Down
50 changes: 48 additions & 2 deletions tests/Feature/TypographyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,55 @@
callback: fn(array $matches) => 'b',
);

expect((string) str('...Banana !')->typography())->toBe('…Bbnbnb !');
expect((string) str('...Banana !')->typography())
->toBe('…Bbnbnb !');

Typography::remove('a-to-b');

expect((string) str('...Banana !')->typography())->toBe('…Banana !');
expect((string) str('...Banana !')->typography())
->toBe('…Banana !');
});

it('can execute one of the declared rules', function() {
expect((string) str('...Banana !')->typography(only: 'hellip'))
->toBe('…Banana !');
});

it('can execute some of the declared rules', function() {
Typography::rule(
key: 'a-to-b',
regex: '/a/',
callback: fn(array $matches) => 'b',
);

expect((string) str('...Banana !')->typography(only: ['hellip','unbreakable-punctuation']))
->toBe('…Banana !');

Typography::remove('a-to-b');
});

it('can execute all of the declared rules except one', function() {
Typography::rule(
key: 'a-to-b',
regex: '/a/',
callback: fn(array $matches) => 'b',
);

expect((string) str('...Banana !')->typography(except: 'hellip'))
->toBe('...Bbnbnb !');

Typography::remove('a-to-b');
});

it('can execute all of the declared rules except some', function() {
Typography::rule(
key: 'a-to-b',
regex: '/a/',
callback: fn(array $matches) => 'b',
);

expect((string) str('...Banana !')->typography(except: ['hellip','unbreakable-punctuation']))
->toBe('...Bbnbnb !');

Typography::remove('a-to-b');
});

0 comments on commit 99ef657

Please sign in to comment.