IsSkuRule::execute()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
c 0
b 0
f 0
rs 9.7998
cc 3
nc 4
nop 2
1
<?php declare(strict_types=1);
2
3
namespace Sample\Validation;
4
5
/**
6
 * Copyright 2015-2020 [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\Validation\Contracts\Execution\ContextInterface;
22
use Limoncello\Validation\Rules\ExecuteRule;
23
use function is_int;
24
25
/**
26
 * @package Sample
27
 */
28
class IsSkuRule extends ExecuteRule
29
{
30
    /** @var string Message Template */
31
    const MESSAGE_TEMPLATE = 'The value should be a valid SKU.';
32
33
    /**
34
     * @param mixed            $value
35
     * @param ContextInterface $context
36
     *
37
     * @return array
38
     */
39
    public static function execute($value, ContextInterface $context): array
40
    {
41
        $idExists = is_int($value) === true && $value < 3;
42
43
        return $idExists === true ?
44
            static::createSuccessReply($value) :
45
            static::createErrorReply(
46
                $context,
47
                $value,
48
                Errors::IS_VALID_SKU,
49
                static::MESSAGE_TEMPLATE,
50
                []
51
            );
52
    }
53
}
54