Passed
Push — 4.1 ( cb7f15...ac53f7 )
by Maxime
08:41
created

MockDynamicAssignmentDBField::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\ORM\Tests\DataObjectTest;
4
5
use SilverStripe\Dev\TestOnly;
6
use SilverStripe\ORM\FieldType\DBBoolean;
7
use SilverStripe\ORM\FieldType\DBField;
8
9
/**
10
 * This is a fake DB field specifically design to test dynamic value assignment. You can set `scalarValueOnly` in
11
 * the constructor. You can control whetever the field will try to do a dynamic assignment by specifing
12
 * `$dynamicAssignment` in nthe consturctor.
13
 *
14
 * 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
15
 * matter what. If the field is set to true, it will try to do a dynamic assignment.
16
 */
17
class MockDynamicAssignmentDBField extends DBBoolean implements TestOnly
18
{
19
20
    private $scalarOnly;
21
    private $dynamicAssignment;
22
23
    /**
24
     * @param string $name
25
     * @param boolean $scalarOnly Whether our fake field should be scalar only.
26
     * @param boolean $dynamicAssignment Whether our fake field will try to do a dynamic assignment.
27
     */
28
    public function __construct($name = '', $scalarOnly = false, $dynamicAssignment = false)
29
    {
30
        $this->scalarOnly = $scalarOnly;
31
        $this->dynamicAssignment = $dynamicAssignment;
32
        parent::__construct($name);
33
    }
34
35
    /**
36
     * If the field value and $dynamicAssignment are true, we'll try to do a dynamic assignment.
37
     * @param $value
38
     * @return array|int
39
     */
40
    public function prepValueForDB($value)
41
    {
42
        if ($value) {
43
            return $this->dynamicAssignment
44
                ? ['ABS(?)' => [1]]
45
                : 1;
46
        }
47
48
        return 0;
49
    }
50
51
    public function scalarValueOnly()
52
    {
53
        return $this->scalarOnly;
54
    }
55
}
56