-
-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Introduce a Prefix value object (#1048)
- Loading branch information
Showing
4 changed files
with
94 additions
and
24 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
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,48 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the humbug/php-scoper package. | ||
* | ||
* Copyright (c) 2017 Théo FIDRY <theo.fidry@gmail.com>, | ||
* Pádraic Brady <padraic.brady@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Humbug\PhpScoper\Configuration; | ||
|
||
use Stringable; | ||
|
||
final readonly class Prefix implements Stringable | ||
{ | ||
/** | ||
* @var non-empty-string | ||
*/ | ||
private string $value; | ||
|
||
public function __construct(string $prefix) | ||
{ | ||
PrefixValidator::validate($prefix); | ||
|
||
$this->value = $prefix; | ||
} | ||
|
||
/** | ||
* @return non-empty-string | ||
*/ | ||
public function __toString(): string | ||
{ | ||
return $this->value; | ||
} | ||
|
||
/** | ||
* @return non-empty-string | ||
*/ | ||
public function toString(): string | ||
{ | ||
return (string) $this; | ||
} | ||
} |
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,39 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the humbug/php-scoper package. | ||
* | ||
* Copyright (c) 2017 Théo FIDRY <theo.fidry@gmail.com>, | ||
* Pádraic Brady <padraic.brady@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Humbug\PhpScoper\Configuration; | ||
|
||
use Humbug\PhpScoper\Configuration\Throwable\InvalidConfigurationValue; | ||
use function Safe\preg_match; | ||
|
||
final class PrefixValidator | ||
{ | ||
private const PREFIX_PATTERN = '/^[\p{L}\d_\\\\]+$/u'; | ||
|
||
/** | ||
* @phpstan-assert non-empty-string $prefix | ||
* | ||
* @throws InvalidConfigurationValue | ||
*/ | ||
public static function validate(string $prefix): void | ||
{ | ||
if (1 !== preg_match(self::PREFIX_PATTERN, $prefix)) { | ||
throw InvalidConfigurationValue::forInvalidPrefixPattern($prefix); | ||
} | ||
|
||
if (preg_match('/\\\{2,}/', $prefix)) { | ||
throw InvalidConfigurationValue::forInvalidNamespaceSeparator($prefix); | ||
} | ||
} | ||
} |
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