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