|
1
|
|
|
<?php declare (strict_types = 1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Limoncello\Flute\L10n; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Copyright 2015-2019 [email protected] |
|
7
|
|
|
* |
|
8
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
9
|
|
|
* you may not use this file except in compliance with the License. |
|
10
|
|
|
* You may obtain a copy of the License at |
|
11
|
|
|
* |
|
12
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
13
|
|
|
* |
|
14
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
15
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
16
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
17
|
|
|
* See the License for the specific language governing permissions and |
|
18
|
|
|
* limitations under the License. |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
use Limoncello\Flute\Contracts\Validation\ErrorCodes; |
|
22
|
|
|
use Limoncello\Validation\I18n\EnUsLocale; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @package Limoncello\Flute |
|
26
|
|
|
*/ |
|
27
|
|
|
class Validation |
|
28
|
|
|
{ |
|
29
|
|
|
/** @var string Namespace name for message keys. */ |
|
30
|
|
|
const NAMESPACE_NAME = 'Limoncello.Flute.Validation'; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var array |
|
34
|
|
|
*/ |
|
35
|
|
|
private static $messages = EnUsLocale::MESSAGES + [ |
|
36
|
|
|
ErrorCodes::TYPE_MISSING => 'JSON API type should be specified.', |
|
37
|
|
|
ErrorCodes::INVALID_ATTRIBUTES => 'JSON API attributes are invalid.', |
|
38
|
|
|
ErrorCodes::UNKNOWN_ATTRIBUTE => 'Unknown JSON API attribute.', |
|
39
|
|
|
ErrorCodes::INVALID_RELATIONSHIP_TYPE => 'The value should be a valid JSON API relationship type.', |
|
40
|
|
|
ErrorCodes::INVALID_RELATIONSHIP => 'Invalid JSON API relationship.', |
|
41
|
|
|
ErrorCodes::UNKNOWN_RELATIONSHIP => 'Unknown JSON API relationship.', |
|
42
|
|
|
ErrorCodes::EXIST_IN_DATABASE_SINGLE => 'The value should be a valid identifier.', |
|
43
|
|
|
ErrorCodes::EXIST_IN_DATABASE_MULTIPLE => 'The value should be valid identifiers.', |
|
44
|
|
|
ErrorCodes::UNIQUE_IN_DATABASE_SINGLE => 'The value should be a unique identifier.', |
|
45
|
|
|
ErrorCodes::INVALID_OPERATION_ARGUMENTS => 'Invalid Operation Arguments.', |
|
46
|
|
|
]; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param int $errorCode |
|
50
|
|
|
* |
|
51
|
|
|
* @return string |
|
52
|
|
|
*/ |
|
53
|
21 |
|
public static function convertCodeToDefaultMessage(int $errorCode): string |
|
54
|
|
|
{ |
|
55
|
21 |
|
return static::getMessages()[$errorCode]; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @inheritdoc |
|
60
|
|
|
*/ |
|
61
|
21 |
|
protected static function getMessages(): array |
|
62
|
|
|
{ |
|
63
|
21 |
|
return static::$messages; |
|
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClassto useselfinstead: