Completed
Push — master ( b5c840...3d5461 )
by Marcus
04:46
created

RegexValidatorCallback   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 43
c 0
b 0
f 0
wmc 4
lcom 0
cbo 1
ccs 0
cts 22
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 31 4
1
<?php
2
3
/**
4
 * TechDivision\Import\Callbacks\RegexValidatorCallback
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2019 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/techdivision/import
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Callbacks;
22
23
/**
24
 * Regex validator callback implementation.
25
 *
26
 * @author    Tim Wagner <[email protected]>
27
 * @copyright 2019 TechDivision GmbH <[email protected]>
28
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
29
 * @link      https://github.com/techdivision/import
30
 * @link      http://www.techdivision.com
31
 */
32
class RegexValidatorCallback extends IndexedArrayValidatorCallback
33
{
34
35
    /**
36
     * Will be invoked by a observer it has been registered for.
37
     *
38
     * @param string|null $attributeCode  The code of the attribute that has to be validated
39
     * @param string|null $attributeValue The attribute value to be validated
40
     *
41
     * @return mixed The modified value
42
     */
43
    public function handle($attributeCode = null, $attributeValue = null)
44
    {
45
46
        // the validations for the attribute with the given code
47
        $validations = $this->getValidations($attributeCode);
48
49
        // throw an exception if NO allowed values have been configured
50
        if (sizeof($validations) === 0) {
51
            throw new \InvalidArgumentException(
52
                sprintf('Missing configuration value for custom validation of attribute "%s"', $attributeCode)
53
            );
54
        }
55
56
        // if the passed value is in the array, return immediately
57
        foreach ($validations as $pattern) {
58
            // validates the attribute value against the passed patterns
59
            if (preg_match($pattern, $attributeValue)) {
60
                continue;
61
            }
62
63
            // throw an exception if the value is NOT in the array
64
            throw new \InvalidArgumentException(
65
                sprintf(
66
                    'Found invalid value "%s" for column "%s" (must match pattern: "%s")',
67
                    $attributeValue,
68
                    $attributeCode,
69
                    $pattern
70
                )
71
            );
72
        }
73
    }
74
}
75