DatesComposer   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setInputs() 0 4 1
A composeComposite() 0 12 4
A dateYMD() 0 4 1
A dateYM() 0 4 1
A timeHi() 0 4 1
A timeHis() 0 4 1
1
<?php
2
namespace Tuum\Form\Lists;
3
4
use Tuum\Form\Data\Inputs;
5
use Tuum\Form\Dates;
6
use Tuum\Form\Tags\Composite;
7
8
class DatesComposer
9
{
10
    /**
11
     * @var Dates
12
     */
13
    private $dates;
14
15
    /**
16
     * @var Inputs
17
     */
18
    private $inputs;
19
20
    /**
21
     * definitions for composite form elements.
22
     *
23
     * @var array
24
     */
25
    public $composeDateTime = [
26
        'ymd' => [
27
            'fields' => [
28
                'y' => 'selYear',
29
                'm' => 'selMonth',
30
                'd' => 'selDay',
31
            ],
32
            'format' => '%1$s/%2$s/%3$s',
33
        ],
34
        'ym' => [
35
            'fields' => [
36
                'y' => 'selYear',
37
                'm' => 'selMonth',
38
            ],
39
            'format' => '%1$s/%2$s',
40
        ],
41
        'his' => [
42
            'fields' => [
43
                'h' => 'selHour',
44
                'i' => 'selMinute',
45
                's' => 'selSecond',
46
            ],
47
            'format' => '%1$s:%2$s:%3$s',
48
        ],
49
        'hi' => [
50
            'fields' => [
51
                'h' => 'selHour',
52
                'i' => 'selMinute',
53
            ],
54
            'format' => '%1$s:%2$s',
55
        ],
56
    ];
57
58
    /**
59
     * DatesComposer constructor.
60
     *
61
     * @param Dates $dates
62
     */
63
    public function __construct($dates)
64
    {
65
        $this->dates = $dates;
66
    }
67
68
    /**
69
     * @param Inputs $inputs
70
     */
71
    public function setInputs($inputs)
72
    {
73
        $this->inputs = $inputs;
74
    }
75
76
    /**
77
     * @param string $type
78
     * @param string $name
79
     * @param string $value
80
     * @param string $format
81
     * @return Composite
82
     */
83
    private function composeComposite($type, $name, $value, $format)
84
    {
85
        if (!$info = $this->composeDateTime[$type]) {
86
            throw new \BadMethodCallException;
87
        }
88
        $format = $format ?: $info['format'];
89
        $fields = [];
90
        foreach($info['fields'] as $key => $method) {
91
            $fields[$key] = $this->dates->$method($name);
92
        }
93
        return $this->dates->makeComposite($name, $fields, $format, $value);
94
    }
95
96
    /**
97
     * @param string      $name
98
     * @param string|null $value
99
     * @param string|null $ymd_format
100
     * @return Composite
101
     */
102
    public function dateYMD($name, $value = null, $ymd_format = null)
103
    {
104
        return $this->composeComposite('ymd', $name, $value, $ymd_format);
105
    }
106
107
    /**
108
     * @param string      $name
109
     * @param string|null $value
110
     * @param string|null $ym_format
111
     * @return Composite
112
     */
113
    public function dateYM($name, $value = null, $ym_format = null)
114
    {
115
        return $this->composeComposite('ym', $name, $value, $ym_format);
116
    }
117
118
    /**
119
     * @param string      $name
120
     * @param string|null $value
121
     * @param string|null $hi_format
122
     * @return Composite
123
     */
124
    public function timeHi($name, $value = null, $hi_format = null)
125
    {
126
        return $this->composeComposite('hi', $name, $value, $hi_format);
127
    }
128
129
    /**
130
     * @param string      $name
131
     * @param string|null $value
132
     * @param string|null $his_format
133
     * @return Composite
134
     */
135
    public function timeHis($name, $value = null, $his_format = null)
136
    {
137
        return $this->composeComposite('his', $name, $value, $his_format);
138
    }
139
140
}