Completed
Push — master ( 13a3cd...b4de29 )
by Sophie
02:50
created

MaintainsTransformations::withTransformations()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 1
1
<?php
2
3
namespace hemio\form\Trait_;
4
5
trait MaintainsTransformations {
6
7
    /**
8
     *
9
     * @var array
10
     */
11
    protected $valueTransformations = [];
12
13
    /**
14
     * 
15
     * @param mixed $id
16
     * @param callable $transformation
17
     */
18
    public function setValueTransformation($id, callable $transformation) {
19
        $this->valueTransformations[$id] = $transformation;
20
    }
21
22
    /**
23
     * Returns the given value with all transformations applied
24
     * 
25
     * @param mixed $value
26
     */
27
    public function withTransformations($value) {
28
        // null for not provided remains untouched
29
        if ($value === null)
30
            return null;
31
32
        foreach ($this->valueTransformations as $f)
33
            $value = $f($value);
34
35
        return $value;
36
    }
37
38
}
39