@@ -14,92 +14,92 @@ |
||
| 14 | 14 | use Test\TestCase; |
| 15 | 15 | |
| 16 | 16 | class ValidatorTest extends TestCase { |
| 17 | - public function testValidate(): void { |
|
| 18 | - $v = new Validator(new Definitions()); |
|
| 19 | - $v->validate('test', []); |
|
| 20 | - $v->validate('test {string1} test {foo} test {bar}.', [ |
|
| 21 | - 'string1' => [ |
|
| 22 | - 'type' => 'user', |
|
| 23 | - 'id' => 'johndoe', |
|
| 24 | - 'name' => 'John Doe', |
|
| 25 | - ], |
|
| 26 | - 'foo' => [ |
|
| 27 | - 'type' => 'user-group', |
|
| 28 | - 'id' => 'sample', |
|
| 29 | - 'name' => 'Sample Group', |
|
| 30 | - ], |
|
| 31 | - 'bar' => [ |
|
| 32 | - 'type' => 'file', |
|
| 33 | - 'id' => '42', |
|
| 34 | - 'name' => 'test.txt', |
|
| 35 | - 'path' => 'path/to/test.txt', |
|
| 36 | - ], |
|
| 37 | - ]); |
|
| 38 | - $this->addToAssertionCount(2); |
|
| 17 | + public function testValidate(): void { |
|
| 18 | + $v = new Validator(new Definitions()); |
|
| 19 | + $v->validate('test', []); |
|
| 20 | + $v->validate('test {string1} test {foo} test {bar}.', [ |
|
| 21 | + 'string1' => [ |
|
| 22 | + 'type' => 'user', |
|
| 23 | + 'id' => 'johndoe', |
|
| 24 | + 'name' => 'John Doe', |
|
| 25 | + ], |
|
| 26 | + 'foo' => [ |
|
| 27 | + 'type' => 'user-group', |
|
| 28 | + 'id' => 'sample', |
|
| 29 | + 'name' => 'Sample Group', |
|
| 30 | + ], |
|
| 31 | + 'bar' => [ |
|
| 32 | + 'type' => 'file', |
|
| 33 | + 'id' => '42', |
|
| 34 | + 'name' => 'test.txt', |
|
| 35 | + 'path' => 'path/to/test.txt', |
|
| 36 | + ], |
|
| 37 | + ]); |
|
| 38 | + $this->addToAssertionCount(2); |
|
| 39 | 39 | |
| 40 | - $this->expectException(InvalidObjectExeption::class); |
|
| 40 | + $this->expectException(InvalidObjectExeption::class); |
|
| 41 | 41 | |
| 42 | - $this->expectExceptionMessage('Object for placeholder string1 is invalid, value 123 for key key is not a string'); |
|
| 43 | - $v->validate('test {string1} test.', [ |
|
| 44 | - 'string1' => [ |
|
| 45 | - 'type' => 'user', |
|
| 46 | - 'id' => 'johndoe', |
|
| 47 | - 'name' => 'John Doe', |
|
| 48 | - 'key' => 123, |
|
| 49 | - ], |
|
| 50 | - ]); |
|
| 42 | + $this->expectExceptionMessage('Object for placeholder string1 is invalid, value 123 for key key is not a string'); |
|
| 43 | + $v->validate('test {string1} test.', [ |
|
| 44 | + 'string1' => [ |
|
| 45 | + 'type' => 'user', |
|
| 46 | + 'id' => 'johndoe', |
|
| 47 | + 'name' => 'John Doe', |
|
| 48 | + 'key' => 123, |
|
| 49 | + ], |
|
| 50 | + ]); |
|
| 51 | 51 | |
| 52 | - $this->expectExceptionMessage('Object for placeholder string1 is invalid, key 456 is not a string'); |
|
| 53 | - $v->validate('test {string1} test.', [ |
|
| 54 | - 'string1' => [ |
|
| 55 | - 'type' => 'user', |
|
| 56 | - 'id' => 'johndoe', |
|
| 57 | - 'name' => 'John Doe', |
|
| 58 | - 456 => 'value', |
|
| 59 | - ], |
|
| 60 | - ]); |
|
| 61 | - } |
|
| 52 | + $this->expectExceptionMessage('Object for placeholder string1 is invalid, key 456 is not a string'); |
|
| 53 | + $v->validate('test {string1} test.', [ |
|
| 54 | + 'string1' => [ |
|
| 55 | + 'type' => 'user', |
|
| 56 | + 'id' => 'johndoe', |
|
| 57 | + 'name' => 'John Doe', |
|
| 58 | + 456 => 'value', |
|
| 59 | + ], |
|
| 60 | + ]); |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | - public static function dataValidateParameterKeys(): array { |
|
| 64 | - return [ |
|
| 65 | - 'not a string' => ['key' => 0, 'throws' => 'Parameter key is invalid'], |
|
| 66 | - '@ is not allowed' => ['key' => 'user@0', 'throws' => 'Parameter key is invalid'], |
|
| 67 | - '? is not allowed' => ['key' => 'user?0', 'throws' => 'Parameter key is invalid'], |
|
| 68 | - 'slash is not allowed' => ['key' => 'user/0', 'throws' => 'Parameter key is invalid'], |
|
| 69 | - 'backslash is not allowed' => ['key' => 'user\\0', 'throws' => 'Parameter key is invalid'], |
|
| 70 | - 'hash is not allowed' => ['key' => 'user#0', 'throws' => 'Parameter key is invalid'], |
|
| 71 | - 'space is not allowed' => ['key' => 'user 0', 'throws' => 'Parameter key is invalid'], |
|
| 72 | - 'has to start with letter, but is number' => ['key' => '0abc', 'throws' => 'Parameter key is invalid'], |
|
| 73 | - 'has to start with letter, but is dot' => ['key' => '.abc', 'throws' => 'Parameter key is invalid'], |
|
| 74 | - 'has to start with letter, but is slash' => ['key' => '-abc', 'throws' => 'Parameter key is invalid'], |
|
| 75 | - 'has to start with letter, but is underscore' => ['key' => '_abc', 'throws' => 'Parameter key is invalid'], |
|
| 76 | - ['key' => 'user-0', 'throws' => null], |
|
| 77 | - ['key' => 'user_0', 'throws' => null], |
|
| 78 | - ['key' => 'user.0', 'throws' => null], |
|
| 79 | - ['key' => 'a._-0', 'throws' => null], |
|
| 80 | - ]; |
|
| 81 | - } |
|
| 63 | + public static function dataValidateParameterKeys(): array { |
|
| 64 | + return [ |
|
| 65 | + 'not a string' => ['key' => 0, 'throws' => 'Parameter key is invalid'], |
|
| 66 | + '@ is not allowed' => ['key' => 'user@0', 'throws' => 'Parameter key is invalid'], |
|
| 67 | + '? is not allowed' => ['key' => 'user?0', 'throws' => 'Parameter key is invalid'], |
|
| 68 | + 'slash is not allowed' => ['key' => 'user/0', 'throws' => 'Parameter key is invalid'], |
|
| 69 | + 'backslash is not allowed' => ['key' => 'user\\0', 'throws' => 'Parameter key is invalid'], |
|
| 70 | + 'hash is not allowed' => ['key' => 'user#0', 'throws' => 'Parameter key is invalid'], |
|
| 71 | + 'space is not allowed' => ['key' => 'user 0', 'throws' => 'Parameter key is invalid'], |
|
| 72 | + 'has to start with letter, but is number' => ['key' => '0abc', 'throws' => 'Parameter key is invalid'], |
|
| 73 | + 'has to start with letter, but is dot' => ['key' => '.abc', 'throws' => 'Parameter key is invalid'], |
|
| 74 | + 'has to start with letter, but is slash' => ['key' => '-abc', 'throws' => 'Parameter key is invalid'], |
|
| 75 | + 'has to start with letter, but is underscore' => ['key' => '_abc', 'throws' => 'Parameter key is invalid'], |
|
| 76 | + ['key' => 'user-0', 'throws' => null], |
|
| 77 | + ['key' => 'user_0', 'throws' => null], |
|
| 78 | + ['key' => 'user.0', 'throws' => null], |
|
| 79 | + ['key' => 'a._-0', 'throws' => null], |
|
| 80 | + ]; |
|
| 81 | + } |
|
| 82 | 82 | |
| 83 | - /** |
|
| 84 | - * @dataProvider dataValidateParameterKeys |
|
| 85 | - */ |
|
| 86 | - public function testValidateParameterKeys(mixed $key, ?string $throws): void { |
|
| 83 | + /** |
|
| 84 | + * @dataProvider dataValidateParameterKeys |
|
| 85 | + */ |
|
| 86 | + public function testValidateParameterKeys(mixed $key, ?string $throws): void { |
|
| 87 | 87 | |
| 88 | - if ($throws !== null) { |
|
| 89 | - $this->expectExceptionMessage($throws); |
|
| 90 | - } |
|
| 88 | + if ($throws !== null) { |
|
| 89 | + $this->expectExceptionMessage($throws); |
|
| 90 | + } |
|
| 91 | 91 | |
| 92 | - $v = new Validator(new Definitions()); |
|
| 93 | - $v->validate('{' . $key . '}', [ |
|
| 94 | - $key => [ |
|
| 95 | - 'type' => 'highlight', |
|
| 96 | - 'id' => 'identifier', |
|
| 97 | - 'name' => 'Display name', |
|
| 98 | - ], |
|
| 99 | - ]); |
|
| 92 | + $v = new Validator(new Definitions()); |
|
| 93 | + $v->validate('{' . $key . '}', [ |
|
| 94 | + $key => [ |
|
| 95 | + 'type' => 'highlight', |
|
| 96 | + 'id' => 'identifier', |
|
| 97 | + 'name' => 'Display name', |
|
| 98 | + ], |
|
| 99 | + ]); |
|
| 100 | 100 | |
| 101 | - if ($throws === null) { |
|
| 102 | - $this->addToAssertionCount(1); |
|
| 103 | - } |
|
| 104 | - } |
|
| 101 | + if ($throws === null) { |
|
| 102 | + $this->addToAssertionCount(1); |
|
| 103 | + } |
|
| 104 | + } |
|
| 105 | 105 | } |
@@ -19,91 +19,91 @@ |
||
| 19 | 19 | * @since 11.0.0 |
| 20 | 20 | */ |
| 21 | 21 | class Validator implements IValidator { |
| 22 | - protected array $requiredParameters = []; |
|
| 23 | - |
|
| 24 | - public function __construct( |
|
| 25 | - protected Definitions $definitions, |
|
| 26 | - ) { |
|
| 27 | - } |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * @param string $subject |
|
| 31 | - * @param array<non-empty-string, RichObjectParameter> $parameters |
|
| 32 | - * @throws InvalidObjectExeption |
|
| 33 | - * @since 11.0.0 |
|
| 34 | - */ |
|
| 35 | - public function validate(string $subject, array $parameters): void { |
|
| 36 | - $matches = []; |
|
| 37 | - $result = preg_match_all('/\{(' . self::PLACEHOLDER_REGEX . ')\}/', $subject, $matches); |
|
| 38 | - |
|
| 39 | - if ($result === false) { |
|
| 40 | - throw new InvalidObjectExeption(); |
|
| 41 | - } |
|
| 42 | - |
|
| 43 | - if (!empty($matches[1])) { |
|
| 44 | - foreach ($matches[1] as $parameter) { |
|
| 45 | - if (!isset($parameters[$parameter])) { |
|
| 46 | - throw new InvalidObjectExeption('Parameter is undefined'); |
|
| 47 | - } |
|
| 48 | - } |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - foreach ($parameters as $placeholder => $parameter) { |
|
| 52 | - if (!\is_string($placeholder) || !preg_match('/^(' . self::PLACEHOLDER_REGEX . ')$/i', $placeholder)) { |
|
| 53 | - throw new InvalidObjectExeption('Parameter key is invalid'); |
|
| 54 | - } |
|
| 55 | - if (!\is_array($parameter)) { |
|
| 56 | - throw new InvalidObjectExeption('Parameter is malformed'); |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - $this->validateParameter($placeholder, $parameter); |
|
| 60 | - } |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * @param array $parameter |
|
| 65 | - * @throws InvalidObjectExeption |
|
| 66 | - */ |
|
| 67 | - protected function validateParameter(string $placeholder, array $parameter): void { |
|
| 68 | - if (!isset($parameter['type'])) { |
|
| 69 | - throw new InvalidObjectExeption('Object type is undefined'); |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - $definition = $this->definitions->getDefinition($parameter['type']); |
|
| 73 | - $requiredParameters = $this->getRequiredParameters($parameter['type'], $definition); |
|
| 74 | - |
|
| 75 | - $missingKeys = array_diff($requiredParameters, array_keys($parameter)); |
|
| 76 | - if (!empty($missingKeys)) { |
|
| 77 | - throw new InvalidObjectExeption('Object for placeholder ' . $placeholder . ' is invalid, missing keys:' . json_encode($missingKeys)); |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - foreach ($parameter as $key => $value) { |
|
| 81 | - if (!is_string($key)) { |
|
| 82 | - throw new InvalidObjectExeption('Object for placeholder ' . $placeholder . ' is invalid, key ' . $key . ' is not a string'); |
|
| 83 | - } |
|
| 84 | - if (!is_string($value)) { |
|
| 85 | - throw new InvalidObjectExeption('Object for placeholder ' . $placeholder . ' is invalid, value ' . $value . ' for key ' . $key . ' is not a string'); |
|
| 86 | - } |
|
| 87 | - } |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * @param string $type |
|
| 92 | - * @param array $definition |
|
| 93 | - * @return string[] |
|
| 94 | - */ |
|
| 95 | - protected function getRequiredParameters(string $type, array $definition): array { |
|
| 96 | - if (isset($this->requiredParameters[$type])) { |
|
| 97 | - return $this->requiredParameters[$type]; |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - $this->requiredParameters[$type] = []; |
|
| 101 | - foreach ($definition['parameters'] as $parameter => $data) { |
|
| 102 | - if ($data['required']) { |
|
| 103 | - $this->requiredParameters[$type][] = $parameter; |
|
| 104 | - } |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - return $this->requiredParameters[$type]; |
|
| 108 | - } |
|
| 22 | + protected array $requiredParameters = []; |
|
| 23 | + |
|
| 24 | + public function __construct( |
|
| 25 | + protected Definitions $definitions, |
|
| 26 | + ) { |
|
| 27 | + } |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * @param string $subject |
|
| 31 | + * @param array<non-empty-string, RichObjectParameter> $parameters |
|
| 32 | + * @throws InvalidObjectExeption |
|
| 33 | + * @since 11.0.0 |
|
| 34 | + */ |
|
| 35 | + public function validate(string $subject, array $parameters): void { |
|
| 36 | + $matches = []; |
|
| 37 | + $result = preg_match_all('/\{(' . self::PLACEHOLDER_REGEX . ')\}/', $subject, $matches); |
|
| 38 | + |
|
| 39 | + if ($result === false) { |
|
| 40 | + throw new InvalidObjectExeption(); |
|
| 41 | + } |
|
| 42 | + |
|
| 43 | + if (!empty($matches[1])) { |
|
| 44 | + foreach ($matches[1] as $parameter) { |
|
| 45 | + if (!isset($parameters[$parameter])) { |
|
| 46 | + throw new InvalidObjectExeption('Parameter is undefined'); |
|
| 47 | + } |
|
| 48 | + } |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + foreach ($parameters as $placeholder => $parameter) { |
|
| 52 | + if (!\is_string($placeholder) || !preg_match('/^(' . self::PLACEHOLDER_REGEX . ')$/i', $placeholder)) { |
|
| 53 | + throw new InvalidObjectExeption('Parameter key is invalid'); |
|
| 54 | + } |
|
| 55 | + if (!\is_array($parameter)) { |
|
| 56 | + throw new InvalidObjectExeption('Parameter is malformed'); |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + $this->validateParameter($placeholder, $parameter); |
|
| 60 | + } |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * @param array $parameter |
|
| 65 | + * @throws InvalidObjectExeption |
|
| 66 | + */ |
|
| 67 | + protected function validateParameter(string $placeholder, array $parameter): void { |
|
| 68 | + if (!isset($parameter['type'])) { |
|
| 69 | + throw new InvalidObjectExeption('Object type is undefined'); |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + $definition = $this->definitions->getDefinition($parameter['type']); |
|
| 73 | + $requiredParameters = $this->getRequiredParameters($parameter['type'], $definition); |
|
| 74 | + |
|
| 75 | + $missingKeys = array_diff($requiredParameters, array_keys($parameter)); |
|
| 76 | + if (!empty($missingKeys)) { |
|
| 77 | + throw new InvalidObjectExeption('Object for placeholder ' . $placeholder . ' is invalid, missing keys:' . json_encode($missingKeys)); |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + foreach ($parameter as $key => $value) { |
|
| 81 | + if (!is_string($key)) { |
|
| 82 | + throw new InvalidObjectExeption('Object for placeholder ' . $placeholder . ' is invalid, key ' . $key . ' is not a string'); |
|
| 83 | + } |
|
| 84 | + if (!is_string($value)) { |
|
| 85 | + throw new InvalidObjectExeption('Object for placeholder ' . $placeholder . ' is invalid, value ' . $value . ' for key ' . $key . ' is not a string'); |
|
| 86 | + } |
|
| 87 | + } |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * @param string $type |
|
| 92 | + * @param array $definition |
|
| 93 | + * @return string[] |
|
| 94 | + */ |
|
| 95 | + protected function getRequiredParameters(string $type, array $definition): array { |
|
| 96 | + if (isset($this->requiredParameters[$type])) { |
|
| 97 | + return $this->requiredParameters[$type]; |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + $this->requiredParameters[$type] = []; |
|
| 101 | + foreach ($definition['parameters'] as $parameter => $data) { |
|
| 102 | + if ($data['required']) { |
|
| 103 | + $this->requiredParameters[$type][] = $parameter; |
|
| 104 | + } |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + return $this->requiredParameters[$type]; |
|
| 108 | + } |
|
| 109 | 109 | } |
@@ -34,7 +34,7 @@ discard block |
||
| 34 | 34 | */ |
| 35 | 35 | public function validate(string $subject, array $parameters): void { |
| 36 | 36 | $matches = []; |
| 37 | - $result = preg_match_all('/\{(' . self::PLACEHOLDER_REGEX . ')\}/', $subject, $matches); |
|
| 37 | + $result = preg_match_all('/\{('.self::PLACEHOLDER_REGEX.')\}/', $subject, $matches); |
|
| 38 | 38 | |
| 39 | 39 | if ($result === false) { |
| 40 | 40 | throw new InvalidObjectExeption(); |
@@ -49,7 +49,7 @@ discard block |
||
| 49 | 49 | } |
| 50 | 50 | |
| 51 | 51 | foreach ($parameters as $placeholder => $parameter) { |
| 52 | - if (!\is_string($placeholder) || !preg_match('/^(' . self::PLACEHOLDER_REGEX . ')$/i', $placeholder)) { |
|
| 52 | + if (!\is_string($placeholder) || !preg_match('/^('.self::PLACEHOLDER_REGEX.')$/i', $placeholder)) { |
|
| 53 | 53 | throw new InvalidObjectExeption('Parameter key is invalid'); |
| 54 | 54 | } |
| 55 | 55 | if (!\is_array($parameter)) { |
@@ -74,15 +74,15 @@ discard block |
||
| 74 | 74 | |
| 75 | 75 | $missingKeys = array_diff($requiredParameters, array_keys($parameter)); |
| 76 | 76 | if (!empty($missingKeys)) { |
| 77 | - throw new InvalidObjectExeption('Object for placeholder ' . $placeholder . ' is invalid, missing keys:' . json_encode($missingKeys)); |
|
| 77 | + throw new InvalidObjectExeption('Object for placeholder '.$placeholder.' is invalid, missing keys:'.json_encode($missingKeys)); |
|
| 78 | 78 | } |
| 79 | 79 | |
| 80 | 80 | foreach ($parameter as $key => $value) { |
| 81 | 81 | if (!is_string($key)) { |
| 82 | - throw new InvalidObjectExeption('Object for placeholder ' . $placeholder . ' is invalid, key ' . $key . ' is not a string'); |
|
| 82 | + throw new InvalidObjectExeption('Object for placeholder '.$placeholder.' is invalid, key '.$key.' is not a string'); |
|
| 83 | 83 | } |
| 84 | 84 | if (!is_string($value)) { |
| 85 | - throw new InvalidObjectExeption('Object for placeholder ' . $placeholder . ' is invalid, value ' . $value . ' for key ' . $key . ' is not a string'); |
|
| 85 | + throw new InvalidObjectExeption('Object for placeholder '.$placeholder.' is invalid, value '.$value.' for key '.$key.' is not a string'); |
|
| 86 | 86 | } |
| 87 | 87 | } |
| 88 | 88 | } |