Skip to content

Commit

Permalink
typos
Browse files Browse the repository at this point in the history
  • Loading branch information
rodber committed Jan 7, 2024
1 parent 7b7c69c commit 57d6771
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 33 deletions.
8 changes: 4 additions & 4 deletions src/Interfaces/RegexInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ public function noDelimitersNoAnchors(): string;
/**
* @return array<int, string>
*/
public function match(string $string): array;
public function match(string $value): array;

/**
* @return array<array<int, string>>
*/
public function matchAll(string $string): array;
public function matchAll(string $value): array;

public function assertMatch(string $string): void;
public function assertMatch(string $value): void;

public function assertMatchAll(string $string): void;
public function assertMatchAll(string $value): void;
}
72 changes: 43 additions & 29 deletions src/Regex.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ public function __construct(
$this->assertRegex();
$delimiter = $this->pattern[0];
$this->noDelimiters = trim($this->pattern, $delimiter);
$this->noDelimitersNoAnchors = strval(preg_replace('#^\^(.*)\$$#', '$1', $this->noDelimiters));
$this->noDelimitersNoAnchors = strval(
preg_replace('#^\^(.*)\$$#', '$1', $this->noDelimiters)
);
}

public function __toString(): string
Expand All @@ -56,64 +58,76 @@ public function noDelimitersNoAnchors(): string
return $this->noDelimitersNoAnchors;
}

public function match(string $string): array
public function match(string $value): array
{
try {
$match = preg_match($this->pattern, $string, $matches);
$match = preg_match($this->pattern, $value, $matches);
}
// @codeCoverageIgnoreStart
catch (PcreException $e) {
throw new RuntimeException(
(string) message('Unable to `%s%`', s: 'preg_match')
(string) message(
'Error `%function%` %message%',
function: 'preg_match',
message: $e->getMessage()
)
);
}
// @codeCoverageIgnoreEnd

return $match === 1 ? $matches : [];
}

public function assertMatch(string $string): void
public function assertMatch(string $value): void
{
if (! $this->match($string)) {
throw new NoMatchException(
(string) message(
'String `%string%` does not match regex `%pattern%`',
pattern: $this->pattern,
string: $string,
),
100
);
if ($this->match($value)) {
return;
}

throw new NoMatchException(
(string) message(
'String `%string%` does not match regex `%pattern%`',
pattern: $this->pattern,
string: $value,
),
100
);
}

public function matchAll(string $string): array
public function matchAll(string $value): array
{
try {
$match = preg_match_all($this->pattern, $string, $matches);
$match = preg_match_all($this->pattern, $value, $matches);
}
// @codeCoverageIgnoreStart
catch (PcreException $e) {
throw new RuntimeException(
(string) message('Unable to `%s%`', s: 'preg_match_all')
(string) message(
'Error `%function%` %message%',
function: 'preg_match_all',
message: $e->getMessage()
)
);
}
// @codeCoverageIgnoreEnd

return $match === 1 ? $matches : [];
}

public function assertMatchAll(string $string): void
public function assertMatchAll(string $value): void
{
if (! $this->matchAll($string)) {
throw new NoMatchException(
(string) message(
'String `%string%` does not match all regex `%pattern%`',
pattern: $this->pattern,
string: $string,
),
110
);
if ($this->matchAll($value)) {
return;
}

throw new NoMatchException(
(string) message(
'String `%string%` does not match all `%pattern%`',
pattern: $this->pattern,
string: $value,
),
110
);
}

private function assertRegex(): void
Expand All @@ -124,9 +138,9 @@ private function assertRegex(): void
throw new InvalidArgumentException(
previous: $e,
message: (string) message(
'Invalid regex string `%regex%` provided [%preg%]',
'Invalid regex string `%regex%` provided: %error%',
regex: $this->pattern,
preg: static::ERRORS[preg_last_error()],
error: static::ERRORS[preg_last_error()],
)
);
}
Expand Down

0 comments on commit 57d6771

Please sign in to comment.