Completed
Push — master ( e43f1a...57db71 )
by Arman
26s queued 12s
created

Lists   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 24
c 1
b 0
f 0
dl 0
loc 73
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A contains() 0 14 4
A containsList() 0 11 3
A doesntContainsList() 0 11 3
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 2.4.0
13
 */
14
15
namespace Quantum\Libraries\Validation\Rules;
16
17
/**
18
 * Trait Lists
19
 * @package Quantum\Libraries\Validation\Rules
20
 */
21
trait Lists
22
{
23
24
    /**
25
     * Adds validation Error
26
     * @param string $field
27
     * @param string $rule
28
     * @param mixed|null $param
29
     */
30
    abstract protected function addError(string $field, string $rule, $param = null);
31
32
    /**
33
     * Verifies that a value is contained within the pre-defined value set
34
     * @param string $field
35
     * @param string $value
36
     * @param string $param
37
     */
38
    protected function contains(string $field, string $value, string $param)
39
    {
40
        if (!empty($value)) {
41
            $param = trim(strtolower($param));
42
            $value = trim(strtolower($value));
43
44
            if (preg_match_all('#\'(.+?)\'#', $param, $matches, PREG_PATTERN_ORDER)) {
45
                $param = $matches[1];
46
            } else {
47
                $param = explode(chr(32), $param);
48
            }
49
50
            if (!in_array($value, $param)) {
51
                $this->addError($field, 'contains', null);
52
            }
53
        }
54
    }
55
56
    /**
57
     * Verifies that a value is contained within the pre-defined value set.
58
     * @param string $field
59
     * @param string $value
60
     * @param array $param
61
     */
62
    protected function containsList(string $field, string $value, array $param)
63
    {
64
        if (!empty($value)) {
65
            $param = array_map(function ($param) {
66
                return trim(strtolower($param));
67
            }, $param);
68
69
            $value = trim(strtolower($value));
70
71
            if (!in_array($value, $param)) {
72
                $this->addError($field, 'containsList', 'null');
73
            }
74
        }
75
    }
76
77
    /**
78
     * Verifies that a value is not contained within the pre-defined value set.
79
     * @param string $field
80
     * @param string $value
81
     * @param array $param
82
     */
83
    protected function doesntContainsList(string $field, string $value, array $param)
84
    {
85
        if (!empty($value)) {
86
            $param = array_map(function ($param) {
87
                return trim(strtolower($param));
88
            }, $param);
89
90
            $value = trim(strtolower($value));
91
92
            if (in_array($value, $param)) {
93
                $this->addError($field, 'doesntContainsList', null);
94
            }
95
        }
96
    }
97
98
}