@@ 13-55 (lines=43) @@ | ||
10 | use function in_array; |
|
11 | use function strlen; |
|
12 | ||
13 | class Scope |
|
14 | { |
|
15 | /** |
|
16 | * @var string |
|
17 | */ |
|
18 | private $scope; |
|
19 | ||
20 | private function __construct(string $scope) |
|
21 | { |
|
22 | $this->scope = $scope; |
|
23 | } |
|
24 | ||
25 | /** |
|
26 | * @throws InvalidArgumentException |
|
27 | */ |
|
28 | public static function build(string $scope, Configuration $configuration): self |
|
29 | { |
|
30 | $scope = trim($scope); |
|
31 | $scopeLength = strlen($scope); |
|
32 | ||
33 | if ($scopeLength > $configuration->maxLengthScope() || $scopeLength < $configuration->minLengthScope()) { |
|
34 | $errorMessage = sprintf("Invalid length for scope: '%s'. Must be between %s and %s", |
|
35 | $scope, |
|
36 | $configuration->minLengthScope(), |
|
37 | $configuration->maxLengthScope() |
|
38 | ); |
|
39 | throw new InvalidArgumentException($errorMessage); |
|
40 | } |
|
41 | ||
42 | if (!$configuration->acceptExtraScope() && !in_array($scope, $configuration->scopes(), true)) { |
|
43 | $validScopes = implode(', ', $configuration->scopes()); |
|
44 | throw new InvalidArgumentException("Invalid scope: '$scope'. Valid scopes are: [$validScopes]"); |
|
45 | } |
|
46 | ||
47 | ||
48 | return new self($scope); |
|
49 | } |
|
50 | ||
51 | public function __toString(): string |
|
52 | { |
|
53 | return "($this->scope)"; |
|
54 | } |
|
55 | } |
@@ 14-55 (lines=42) @@ | ||
11 | use function sprintf; |
|
12 | use function strlen; |
|
13 | ||
14 | class Type |
|
15 | { |
|
16 | /** |
|
17 | * @var string |
|
18 | */ |
|
19 | private $type; |
|
20 | ||
21 | private function __construct(string $type) |
|
22 | { |
|
23 | $this->type = $type; |
|
24 | } |
|
25 | ||
26 | /** |
|
27 | * @throws InvalidArgumentException |
|
28 | */ |
|
29 | public static function build(string $type, Configuration $configuration): self |
|
30 | { |
|
31 | $type = trim($type); |
|
32 | $typeLength = strlen($type); |
|
33 | ||
34 | if ($typeLength > $configuration->maxLengthType() || $typeLength < $configuration->minLengthType()) { |
|
35 | $errorMessage = sprintf("Invalid length for type: '%s'. Must be between %s and %s", |
|
36 | $type, |
|
37 | $configuration->minLengthType(), |
|
38 | $configuration->maxLengthType() |
|
39 | ); |
|
40 | throw new InvalidArgumentException($errorMessage); |
|
41 | } |
|
42 | ||
43 | if (!$configuration->acceptExtraType() && !in_array($type, $configuration->types(), true)) { |
|
44 | $validTypes = implode(', ', $configuration->types()); |
|
45 | throw new InvalidArgumentException("Invalid type: '$type'. Valid types are: [$validTypes]"); |
|
46 | } |
|
47 | ||
48 | return new self($type); |
|
49 | } |
|
50 | ||
51 | public function __toString(): string |
|
52 | { |
|
53 | return $this->type; |
|
54 | } |
|
55 | } |