Uuid::__invoke()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 12
nc 3
nop 2
dl 0
loc 22
ccs 13
cts 13
cp 1
crap 3
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
namespace Mbright\Validation\Rule\Sanitize;
4
5
use Mbright\Validation\Rule\AbstractUuidCase;
6
7
class Uuid extends AbstractUuidCase implements SanitizeRuleInterface
8
{
9
    /**
10
     * Forces the value to a canonical UUID.
11
     *
12
     * @param object $subject The subject to be filtered.
13
     * @param string $field The subject field name.
14
     *
15
     * @return bool True if the value was sanitized, false if not.
16
     */
17 21
    public function __invoke($subject, string $field): bool
18
    {
19 21
        $value = $subject->$field;
20 21
        if ($this->isCanonical($value)) {
21 3
            return true;
22
        }
23
24
        // force to hex only
25 18
        $value = preg_replace('/[^a-f0-9]/i', '', $subject->$field);
26 18
        if (!$this->isHexOnly($value)) {
27
            // not hex-only, cannot sanitize
28 6
            return false;
29
        }
30
31
        // add the dashes
32 12
        $subject->$field = substr($value, 0, 8) . '-'
33 12
            . substr($value, 8, 4) . '-'
34 12
            . substr($value, 12, 4) . '-'
35 12
            . substr($value, 16, 4) . '-'
36 12
            . substr($value, 20);
37
38 12
        return true;
39
    }
40
}
41