Field   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 75.76%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 0
dl 0
loc 91
ccs 25
cts 33
cp 0.7576
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A getFieldname() 0 4 1
A getCaption() 0 4 1
A getType() 0 4 1
A toDBAL() 0 4 1
A asArray() 0 8 1
B typeToDBAL() 0 19 7
1
<?php
2
declare(strict_types = 1);
3
namespace EngineWorks\Pivot;
4
5
use EngineWorks\DBAL\CommonTypes;
6
7
class Field
8
{
9
    const TEXT = 'TEXT';
10
    const NUMBER = 'NUMBER';
11
    const INT = 'INT';
12
    const DATE = 'DATE';
13
    const DATETIME = 'DATETIME';
14
    const TIME = 'TIME';
15
16
    /** @var string */
17
    private $fieldname;
18
    /** @var string */
19
    private $caption;
20
    /** @var string */
21
    private $type;
22
23
    /**
24
     * Field constructor.
25
     * @param string $fieldname
26
     * @param string $caption
27
     * @param string $type
28
     */
29 15
    public function __construct(string $fieldname, string $caption, string $type)
30
    {
31 15
        if (! in_array($type, [self::TEXT, self::INT, self::NUMBER, self::DATETIME, self::DATE, self::TIME])) {
32 1
            throw new \InvalidArgumentException('Invalid value for property type');
33
        }
34 14
        $this->fieldname = $fieldname;
35 14
        $this->caption = $caption;
36 14
        $this->type = $type;
37 14
    }
38
39 14
    public function getFieldname() : string
40
    {
41 14
        return $this->fieldname;
42
    }
43
44 11
    public function getCaption() : string
45
    {
46 11
        return $this->caption;
47
    }
48
49 1
    public function getType() : string
50
    {
51 1
        return $this->type;
52
    }
53
54
    /**
55
     * @return string
56
     */
57 3
    public function toDBAL() : string
58
    {
59 3
        return self::typeToDBAL($this->type);
60
    }
61
62
    /**
63
     * @return string[]
64
     */
65 4
    public function asArray() : array
66
    {
67
        return [
68 4
            'fieldname' => $this->fieldname,
69 4
            'caption' => $this->caption,
70 4
            'type' => $this->type,
71
        ];
72
    }
73
74
    /**
75
     * @param string $type
76
     * @return string
77
     */
78 3
    public static function typeToDBAL(string $type) : string
79
    {
80
        switch ($type) {
81 3
            case self::TEXT:
82 3
                return CommonTypes::TTEXT;
83 2
            case self::INT:
84
                return CommonTypes::TINT;
85 2
            case self::NUMBER:
86 2
                return CommonTypes::TNUMBER;
87
            case self::DATETIME:
88
                return CommonTypes::TDATETIME;
89
            case self::DATE:
90
                return CommonTypes::TDATE;
91
            case self::TIME:
92
                return CommonTypes::TTIME;
93
            default:
94
                return CommonTypes::TTEXT;
95
        }
96
    }
97
}
98