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

MaintainsTransformations   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 0
dl 0
loc 34
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setValueTransformation() 0 3 1
A withTransformations() 0 10 3
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