-
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.
- Loading branch information
Showing
4 changed files
with
125 additions
and
84 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rudashi\Concerns; | ||
|
||
use Rudashi\Contracts\PatternContract; | ||
|
||
trait HasPatterns | ||
{ | ||
/** | ||
* Dynamically handle call into builder instance. | ||
* | ||
* @param array<int, callable|string|int> $arguments | ||
* | ||
* @throws \BadMethodCallException | ||
*/ | ||
public function __call(string $name, array $arguments): self | ||
{ | ||
foreach ($this->patterns as $pattern) { | ||
if ($pattern->getName() === $name || $pattern->alias() === $name) { | ||
return $this->pushToPattern($pattern->getPattern()); | ||
} | ||
} | ||
|
||
$this->throwBadMethodException($name); | ||
} | ||
|
||
/** | ||
* Dynamically call the registered pattern. | ||
*/ | ||
public function pattern(string $string): self | ||
{ | ||
return $this->__call($string, []); | ||
} | ||
|
||
/** | ||
* Register the given Patterns with the builder. | ||
* | ||
* @param array<int, class-string<\Rudashi\Contracts\PatternContract>> $patterns | ||
*/ | ||
private function registerPatterns(array $patterns): void | ||
{ | ||
array_map(function (string $pattern): void { | ||
if (! is_subclass_of($pattern, PatternContract::class)) { | ||
$this->throwArgumentException(sprintf('Class "%s" must implement PatternContract.', $pattern)); | ||
} | ||
|
||
$this->patterns[] = new $pattern(); | ||
}, $patterns); | ||
} | ||
} |
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,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rudashi\Concerns; | ||
|
||
trait Sanitizer | ||
{ | ||
/** | ||
* Sanitize the given expression value. | ||
*/ | ||
public static function sanitize(string|int $value): string | ||
{ | ||
$value = (string) $value; | ||
|
||
return $value !== '' ? preg_quote($value, '/') : $value; | ||
} | ||
} |
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,42 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rudashi\Concerns; | ||
|
||
use BadMethodCallException; | ||
use InvalidArgumentException; | ||
use LogicException; | ||
|
||
trait ThrowExceptions | ||
{ | ||
/** | ||
* Throws a bad method call exception for the given method. | ||
* | ||
* @throws \BadMethodCallException | ||
*/ | ||
protected function throwBadMethodException(string $method): never | ||
{ | ||
throw new BadMethodCallException(sprintf('Method "%s" does not exist in %s.', $method, self::class)); | ||
} | ||
|
||
/** | ||
* Throws a logic exception. | ||
* | ||
* @throws \LogicException | ||
*/ | ||
protected function throwLogicException(string $message): never | ||
{ | ||
throw new LogicException($message); | ||
} | ||
|
||
/** | ||
* Throws an invalid argument exception. | ||
* | ||
* @throws \InvalidArgumentException | ||
*/ | ||
protected function throwArgumentException(string $message): never | ||
{ | ||
throw new InvalidArgumentException($message); | ||
} | ||
} |
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