EnumTrait   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 47
rs 10
c 0
b 0
f 0
wmc 7
lcom 0
cbo 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B validateEnum() 0 18 7
addError() 0 1 ?
conjugationObject() 0 1 ?
1
<?php namespace CMPayments\SchemaValidator\Validators;
2
3
use CMPayments\SchemaValidator\Exceptions\ValidateException;
4
5
/**
6
 * Class EnumTrait
7
 *
8
 * @package CMPayments\SchemaValidator\Validators
9
 * @Author  Boy Wijnmaalen <[email protected]>
10
 */
11
trait EnumTrait
12
{
13
    /**
14
     * Validates $data against a specific $schema->enum
15
     *
16
     * @param $data
17
     * @param $schema
18
     * @param $path
19
     */
20
    public function validateEnum($data, $schema, $path)
21
    {
22
        if (!isset($schema->enum)) {
23
            return;
24
        }
25
26
        // if $data comes from an array than the action below has already been done in validateArray()
27
        $needle   = (isset($schema->caseSensitive) && !$schema->caseSensitive) ? strtolower($data) : $data;
28
        $haystack = (isset($schema->caseSensitive) && !$schema->caseSensitive) ? array_map('strtolower', $schema->enum) : $schema->enum;
29
30
        if (!in_array($needle, $haystack)) {
31
32
            $this->addError(
33
                ValidateException::ERROR_USER_ENUM_NEEDLE_NOT_FOUND_IN_HAYSTACK,
34
                [$path, $data, $this->conjugationObject(count($schema->enum), 'this specific value', 'one of these values'), implode('\', \'', $schema->enum)]
35
            );
36
        }
37
    }
38
39
    /**
40
     * @param int   $code
41
     * @param array $args
42
     *
43
     * @return mixed
44
     */
45
    abstract public function addError($code, array $args = []);
46
47
    /**
48
     * Returns a valid representation of 'items' (or other value)
49
     *
50
     * @param int    $count
51
     * @param string $single
52
     * @param string $plural
53
     *
54
     * @return string
55
     */
56
    abstract public function conjugationObject($count, $single = 'item', $plural = 'items');
57
}