Completed
Push — feature/controller ( 08e254...534c3a )
by René
09:48
created

EntityProperty::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Zortje\MVC\Model\Table\Entity;
4
5
/**
6
 * Class EntityProperty
7
 *
8
 * @package Zortje\MVC\Model\Table\Entity
9
 */
10
class EntityProperty
11
{
12
13
    /**
14
     * @var string Entity property type
15
     */
16
    protected $type;
17
18
    /**
19
     * Format value according to entity property type
20
     *
21
     * @param mixed $value Value
22
     *
23
     * @return mixed Value
24
     */
25 5
    public function formatValueForEntity($value)
26
    {
27 5
        switch ($this->type) {
28
            case 'string':
29 1
                $value = "$value";
30 1
                break;
31
32
            case 'integer':
33 1
                $value = (int) $value;
34 1
                break;
35
36
            case 'float':
37 1
                $value = (float) $value;
38 1
                break;
39
40
            case 'Date':
41
            case 'DateTime':
42 2
                $value = new \DateTime($value);
43 2
                break;
44
        }
45
46 5
        return $value;
47
    }
48
49
    /**
50
     * Format value for insertion into the database
51
     *
52
     * @param mixed $value Value
53
     *
54
     * @return mixed Value
55
     */
56 2
    public function formatValueForDatabase($value)
57
    {
58 2
        switch ($this->type) {
59
            case 'Date':
60
                /**
61
                 * @var \DateTime $value
62
                 */
63 1
                $value = $value->format('Y-m-d');
64 1
                break;
65
66
            case 'DateTime':
67
                /**
68
                 * @var \DateTime $value
69
                 */
70 1
                $value = $value->format('Y-m-d H:i:s');
71 1
                break;
72
        }
73
74 2
        return $value;
75
    }
76
77
    /**
78
     * @param string $type
79
     */
80 1
    public function __construct($type)
81
    {
82 1
        $this->type = $type;
83 1
    }
84
}
85