Uuid   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 32
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 22 3
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