Skip to content

Commit 99ef657

Browse files
committed
Added "only" and "except" arguments for rule filtering
1 parent c6e2516 commit 99ef657

File tree

3 files changed

+66
-11
lines changed

3 files changed

+66
-11
lines changed

src/ServiceProvider.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ public function register()
3030
*/
3131
public function boot()
3232
{
33-
Str::macro('typography', function (string $value) {
34-
return (new Typography($value))->handle();
33+
Str::macro('typography', function (string $value, null|string|array $only = null, null|string|array $except = null) {
34+
return (new Typography($value))->handle(only: $only, except: $except);
3535
});
36-
Stringable::macro('typography', function () {
37-
return new static(Str::typography($this->value));
36+
Stringable::macro('typography', function (null|string|array $only = null, null|string|array $except = null) {
37+
return new static(Str::typography($this->value, $only, $except));
3838
});
3939
}
4040
}

src/Typography.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,20 @@ static public function remove(string $key): void
3535
}
3636

3737
/**
38-
* Remove a typographic rule handler.
38+
* Return the requested handlers.
3939
*/
40-
static protected function getHandlers(): array
40+
static protected function getHandlers(null|string|array $only = null, null|string|array $except = null): array
4141
{
42-
return array_reduce(static::$handlers, function($all, $handler) {
42+
$only = ($only ? (is_array($only) ? $only : [$only]) : null);
43+
$except = ($except ? (is_array($except) ? $except : [$except]) : null);
44+
45+
$handlers = match (true) {
46+
!is_null($only) => array_filter(static::$handlers, fn($key) => in_array($key, $only), ARRAY_FILTER_USE_KEY),
47+
!is_null($except) => array_filter(static::$handlers, fn($key) => !in_array($key, $except), ARRAY_FILTER_USE_KEY),
48+
default => static::$handlers,
49+
};
50+
51+
return array_reduce($handlers, function($all, $handler) {
4352
[$regex, $callback] = $handler;
4453
$all[$regex] = $callback;
4554
return $all;
@@ -57,10 +66,10 @@ public function __construct(string $value)
5766
/**
5867
* Run the value transformations.
5968
*/
60-
public function handle(): string
69+
public function handle(null|string|array $only = null, null|string|array $except = null): string
6170
{
6271
return preg_replace_callback_array(
63-
static::getHandlers(),
72+
static::getHandlers(only: $only, except: $except),
6473
$this->value
6574
);
6675
}

tests/Feature/TypographyTest.php

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,55 @@
4949
callback: fn(array $matches) => 'b',
5050
);
5151

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

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

56-
expect((string) str('...Banana !')->typography())->toBe('…Banana !');
57+
expect((string) str('...Banana !')->typography())
58+
->toBe('…Banana !');
59+
});
60+
61+
it('can execute one of the declared rules', function() {
62+
expect((string) str('...Banana !')->typography(only: 'hellip'))
63+
->toBe('…Banana !');
64+
});
65+
66+
it('can execute some of the declared rules', function() {
67+
Typography::rule(
68+
key: 'a-to-b',
69+
regex: '/a/',
70+
callback: fn(array $matches) => 'b',
71+
);
72+
73+
expect((string) str('...Banana !')->typography(only: ['hellip','unbreakable-punctuation']))
74+
->toBe('…Banana !');
75+
76+
Typography::remove('a-to-b');
77+
});
78+
79+
it('can execute all of the declared rules except one', function() {
80+
Typography::rule(
81+
key: 'a-to-b',
82+
regex: '/a/',
83+
callback: fn(array $matches) => 'b',
84+
);
85+
86+
expect((string) str('...Banana !')->typography(except: 'hellip'))
87+
->toBe('...Bbnbnb !');
88+
89+
Typography::remove('a-to-b');
90+
});
91+
92+
it('can execute all of the declared rules except some', function() {
93+
Typography::rule(
94+
key: 'a-to-b',
95+
regex: '/a/',
96+
callback: fn(array $matches) => 'b',
97+
);
98+
99+
expect((string) str('...Banana !')->typography(except: ['hellip','unbreakable-punctuation']))
100+
->toBe('...Bbnbnb !');
101+
102+
Typography::remove('a-to-b');
57103
});

0 commit comments

Comments
 (0)