Completed
Push — 3.7 ( 53f5c3...014c99 )
by
unknown
05:16 queued 11s
created

MockDynamicAssignmentDBField   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 39
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A prepValueForDB() 0 10 3
A scalarValueOnly() 0 4 1
1
<?php
2
3
/**
4
 * This is a fake DB field specifically design to test dynamic value assignment. You can set `scalarValueOnly` in
5
 * the constructor. You can control whetever the field will try to do a dynamic assignment by specifing
6
 * `$dynamicAssignment` in nthe consturctor.
7
 *
8
 * If the field is set to false, it will try to do a plain assignment. This is so you can save the initial value no
9
 * matter what. If the field is set to true, it will try to do a dynamic assignment.
10
 */
11
class MockDynamicAssignmentDBField extends Boolean
12
{
13
14
    private $scalarOnly;
15
    private $dynamicAssignment;
16
17
    /**
18
     * @param string $name
19
     * @param boolean $scalarOnly Whether our fake field should be scalar only.
20
     * @param boolean $dynamicAssignment Whether our fake field will try to do a dynamic assignment.
21
     */
22
    public function __construct($name = '', $scalarOnly = false, $dynamicAssignment = false)
23
    {
24
        $this->scalarOnly = $scalarOnly;
25
        $this->dynamicAssignment = $dynamicAssignment;
26
        parent::__construct($name);
27
    }
28
29
    /**
30
     * If the field value and dynamicAssignment are true, we'll try to do a dynamic assignment
31
     * @param mixed $value
32
     * @return array|int|mixed
33
     */
34
    public function prepValueForDB($value)
35
    {
36
        if ($value) {
37
            return $this->dynamicAssignment
38
                ? array('ABS(?)' => array(1))
39
                : 1;
40
        }
41
42
        return 0;
43
    }
44
45
    public function scalarValueOnly()
46
    {
47
        return $this->scalarOnly;
48
    }
49
}
50