1
|
|
|
<?php namespace Sample\Validation; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2015-2016 [email protected] (www.neomerx.com) |
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
|
|
|
use Limoncello\Validation\Contracts\MessageCodes; |
20
|
|
|
use Limoncello\Validation\I18n\Locales\EnUsLocale; |
21
|
|
|
use Limoncello\Validation\I18n\Translator as BaseTranslator; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @package Sample |
25
|
|
|
*/ |
26
|
|
|
class Translator extends BaseTranslator |
27
|
|
|
{ |
28
|
|
|
/** Custom error code */ |
29
|
|
|
const IS_EMAIL = 1000001; |
30
|
|
|
|
31
|
|
|
/** Custom error code */ |
32
|
|
|
const IS_EXISTING_PAYMENT_PLAN = 1000002; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Constructor. |
36
|
|
|
*/ |
37
|
|
|
public function __construct() |
38
|
|
|
{ |
39
|
|
|
// sample replacements |
40
|
|
|
$replacements = [ |
41
|
|
|
'email' => 'Email address', |
42
|
|
|
'first_name' => 'First Name', |
43
|
|
|
'last_name' => 'Last Name', |
44
|
|
|
'payment_plan' => 'Payment plan', |
45
|
|
|
'interests' => 'Interests', |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
|
// sample custom messages |
49
|
|
|
$messages = EnUsLocale::getMessages() + [ |
50
|
|
|
static::IS_EMAIL => "The `{0}` value should be a valid email address.", |
|
|
|
|
51
|
|
|
static::IS_EXISTING_PAYMENT_PLAN => "The `{0}` value should be an existing payment plan.", |
|
|
|
|
52
|
|
|
]; |
53
|
|
|
|
54
|
|
|
parent::__construct(EnUsLocale::getLocaleCode(), $messages, $replacements, MessageCodes::INVALID_VALUE); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
PHP provides two ways to mark string literals. Either with single quotes
'literal'
or with double quotes"literal"
. The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (
\'
) and the backslash (\\
). Every other character is displayed as is.Double quoted string literals may contain other variables or more complex escape sequences.
will print an indented:
Single is Value
If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.
For more information on PHP string literals and available escape sequences see the PHP core documentation.