1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2014 SURFnet bv |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace Surfnet\StepupMiddleware\ManagementBundle\Validator; |
20
|
|
|
|
21
|
|
|
use Assert\Assertion as Assert; |
|
|
|
|
22
|
|
|
use Surfnet\StepupMiddleware\ManagementBundle\Exception\InvalidArgumentException; |
23
|
|
|
use Surfnet\StepupMiddleware\ManagementBundle\Validator\Assert as StepupAssert; |
24
|
|
|
|
25
|
|
|
final class EmailTemplatesConfigurationValidator implements ConfigurationValidatorInterface |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $requiredLocale; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param string $requiredLocale |
34
|
|
|
*/ |
35
|
|
|
public function __construct($requiredLocale) |
36
|
|
|
{ |
37
|
|
|
if (!is_string($requiredLocale)) { |
38
|
|
|
throw InvalidArgumentException::invalidType('string', 'defaultLocale', $requiredLocale); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$this->requiredLocale = $requiredLocale; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function validate(array $configuration, $propertyPath) |
45
|
|
|
{ |
46
|
|
|
$templateNames = [ |
47
|
|
|
'confirm_email', |
48
|
|
|
'registration_code_with_ras', |
49
|
|
|
'registration_code_with_ra_locations', |
50
|
|
|
'vetted', |
51
|
|
|
]; |
52
|
|
|
|
53
|
|
|
StepupAssert::keysMatch( |
54
|
|
|
$configuration, |
55
|
|
|
$templateNames, |
56
|
|
|
sprintf("Expected only templates '%s'", join(',', $templateNames)), |
57
|
|
|
$propertyPath |
58
|
|
|
); |
59
|
|
|
|
60
|
|
|
foreach ($templateNames as $templateName) { |
61
|
|
|
Assert::isArray( |
62
|
|
|
$configuration[$templateName], |
63
|
|
|
'Property "' . $templateName . '" must have an object as value', |
64
|
|
|
$propertyPath |
65
|
|
|
); |
66
|
|
|
|
67
|
|
|
$templatePropertyPath = $propertyPath . '.' . $templateName; |
68
|
|
|
|
69
|
|
|
Assert::keyExists( |
70
|
|
|
$configuration[$templateName], |
71
|
|
|
$this->requiredLocale, |
72
|
|
|
"Required property '" . $this->requiredLocale . "' is missing", |
73
|
|
|
$templatePropertyPath |
74
|
|
|
); |
75
|
|
|
|
76
|
|
|
foreach ($configuration[$templateName] as $locale => $template) { |
77
|
|
|
$localePropertyPath = $templatePropertyPath . '[' . $locale . ']'; |
78
|
|
|
Assert::string( |
79
|
|
|
$locale, |
80
|
|
|
'Locale must be string', |
81
|
|
|
$localePropertyPath |
82
|
|
|
); |
83
|
|
|
Assert::string( |
84
|
|
|
$template, |
85
|
|
|
"Property '" . $this->requiredLocale . "' must have a string as value", |
86
|
|
|
$localePropertyPath |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: