Uuid::__invoke()   B
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 13
cts 13
cp 1
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 13
nc 3
nop 2
crap 3
1
<?php
2
/**
3
 *
4
 * This file is part of Aura for PHP.
5
 *
6
 * @license http://opensource.org/licenses/bsd-license.php BSD
7
 *
8
 */
9
namespace Aura\Filter\Rule\Sanitize;
10
11
use Aura\Filter\Rule\AbstractUuid;
12
13
/**
14
 *
15
 * Forces the value to a canonical UUID.
16
 *
17
 * @package Aura.Filter
18
 *
19
 */
20
class Uuid extends AbstractUuid
21
{
22
    /**
23
     *
24
     * Forces the value to a canonical UUID.
25
     *
26
     * @param object $subject The subject to be filtered.
27
     *
28
     * @param string $field The subject field name.
29
     *
30
     * @return bool True if the value was sanitized, false if not.
31
     *
32
     */
33 7
    public function __invoke($subject, $field)
34
    {
35 7
        $value = $subject->$field;
36 7
        if ($this->isCanonical($value)) {
37
            // already a canonical value
38 1
            return true;
39
        }
40
41
        // force to hex only
42 6
        $value = preg_replace('/[^a-f0-9]/i', '', $subject->$field);
43 6
        if (! $this->isHexOnly($value)) {
44
            // not hex-only, cannot sanitize
45 2
            return false;
46
        }
47
48
        // add the dashes
49 4
        $subject->$field = substr($value, 0, 8) . '-'
50 4
                         . substr($value, 8, 4) . '-'
51 4
                         . substr($value, 12, 4) . '-'
52 4
                         . substr($value, 16, 4) . '-'
53 4
                         . substr($value, 20);
54
55
        // done!
56 4
        return true;
57
     }
58
}
59