Skip to content

Commit

Permalink
Added first tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
rudashi committed Mar 6, 2024
1 parent 1d53035 commit e244816
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/Concerns/Tokens.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace Rudashi\Concerns;

trait Tokens
{
public function exactly(string $value): static
{
$this->pattern[] = $this->sanitize($value);

return $this;
}

public function letter(): static
{
$this->pattern[] = '[a-zA-Z]';

return $this;
}

public function letters(): static
{
$this->pattern[] = '[a-zA-Z]+';

return $this;
}

public function lowerLetter(): static
{
$this->pattern[] = '[a-z]';

return $this;
}

public function lowerLetters(): static
{
$this->pattern[] = '[a-z]+';

return $this;
}
}
9 changes: 9 additions & 0 deletions src/FluentBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@

namespace Rudashi;

use Rudashi\Concerns\Tokens;

class FluentBuilder
{
use Tokens;

protected string $context = '';
/**
* @var array<int, string>
Expand Down Expand Up @@ -35,4 +39,9 @@ public function setContext(string $string): static

return $this;
}

protected function sanitize(string $value): string
{
return $value !== '' ? preg_quote($value, '/') : $value;
}
}
38 changes: 38 additions & 0 deletions tests/Unit/TokensTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

it('can add a `exactly` token', function () {
$regex = fluentBuilder()->exactly('foo bar');

expect($regex->get())
->toBe('/foo bar/');
});

it('can add a `letter` token', function () {
$regex = fluentBuilder()->letter();

expect($regex->get())
->toBe('/[a-zA-Z]/');
});

it('can add a `letters` token', function () {
$regex = fluentBuilder()->letters();

expect($regex->get())
->toBe('/[a-zA-Z]+/');
});

it('can add a `lowerLetter` token', function () {
$regex = fluentBuilder()->lowerLetter();

expect($regex->get())
->toBe('/[a-z]/');
});

it('can add a `lowerLetters` token', function () {
$regex = fluentBuilder()->lowerLetters();

expect($regex->get())
->toBe('/[a-z]+/');
});

0 comments on commit e244816

Please sign in to comment.