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