AbstractList::getFormat()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
namespace Tuum\Form\Lists;
3
4
use ArrayIterator;
5
use Closure;
6
use IteratorAggregate;
7
use Traversable;
8
9
abstract class AbstractList implements IteratorAggregate
10
{
11
    /**
12
     * @var int
13
     */
14
    protected $start;
15
16
    /**
17
     * @var int
18
     */
19
    protected $end;
20
21
    /**
22
     * @var int
23
     */
24
    protected $step = 1;
25
26
    /**
27
     * @var Closure    format output string (i.e. '2' => 'February').
28
     */
29
    protected $format;
30
31
    /**
32
     * @var string     format string for values (i.e. '2' => '02').
33
     */
34
    public $formatValue = '%02d';
35
36
    /**
37
     * constructor
38
     *
39
     * @param int          $start
40
     * @param int          $end
41
     * @param int          $step
42
     * @param null|Closure $format
43
     */
44
    protected function __construct($start, $end, $step, $format = null)
45
    {
46
        $this->start  = $start;
47
        $this->end    = $end;
48
        $step         = $step ?: $this->step;
49
        $this->step   = (int) ($start < $end ? abs($step) : -abs($step));
50
        $this->format = $format;
51
    }
52
53
    /**
54
     * @param Closure $format
55
     * @return $this
56
     */
57
    public function setFormat($format)
58
    {
59
        $this->format = $format;
60
        return $this;
61
    }
62
63
    /**
64
     * @param mixed $value
65
     * @return string
66
     */
67
    public function format($value)
68
    {
69
        if ($format = $this->format) {
70
            return $format($value);
71
        }
72
        return $value;
73
    }
74
75
    /**
76
     * @return callable
77
     */
78
    public function getFormat()
79
    {
80
        return $this->format;
81
    }
82
83
    /**
84
     * @return array
85
     */
86
    protected function getList()
87
    {
88
        $years = [];
89
        $cmp   = function ($y) {
90
            if ($this->step > 0) {
91
                return $y <= $this->end;
92
            }
93
            if ($this->step < 0) {
94
                return $y >= $this->end;
95
            }
96
            return false;
97
        };
98
        for ($y = $this->start; $cmp($y); $y += $this->step) {
99
            $years[sprintf($this->formatValue,$y)] = $this->format($y);
100
        }
101
        return $years;
102
    }
103
104
    /**
105
     * @return Traversable
106
     */
107
    public function getIterator()
108
    {
109
        return new ArrayIterator($this->getList());
110
    }
111
112
    /**
113
     * use formatted print for output.
114
     *
115
     * @param string $fmt
116
     * @return $this
117
     */
118
    public function usePrintFormat($fmt)
119
    {
120
        return $this->setFormat(function($string) use($fmt) {
121
            return sprintf($fmt, $string);
122
        });
123
    }
124
}