Generator   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 27
c 2
b 0
f 0
dl 0
loc 65
rs 10
wmc 12

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B monthsSelectGenerator() 0 44 11
1
<?php
2
3
namespace Months\Html;
4
5
class Generator
6
{
7
    /**
8
     * @param string $language
9
     * @param string $monthFormat | full for complete month name or small for brev name
10
     * @param string $valueFormat | number (default) or string
11
     */
12
    public function __construct(
13
        private string $language = 'pt_br',
14
        private string $monthFormat = 'full',
15
        private string $valueFormat = 'number'
16
    )
17
    {}
18
19
    /**
20
     * @param int $id
21
     * @param string $class
22
     * @param string $name
23
     * @param array $dataAtributes
24
     * @return string
25
     */
26
    public function monthsSelectGenerator(
27
        string $id,
28
        string $class,
29
        string $name,
30
        bool $currentSelected = false,
31
        array $dataAtributes = null)
32
    {
33
        $dataAttr = $dataAtributes ? implode(" ", $dataAtributes) : '';
34
35
        $currentYear = (new \DateTime('now'))->format('Y');
36
        $currentMonth = $currentSelected ? (new \DateTime('now'))->format('m') : '';
37
        $months = 12;
38
        $options = "";
39
        $formatter = '';
40
41
        if ($this->language == 'pt_br')
42
        {
43
            $formatter = \IntlDateFormatter::create(
44
                'pt_BR',
45
                \IntlDateFormatter::FULL,
46
                \IntlDateFormatter::NONE,
47
                'America/Sao_Paulo',
48
                \IntlDateFormatter::GREGORIAN,
49
                $this->monthFormat == 'small' ? 'MMM' : 'MMMM'
50
            );
51
        }
52
53
        for ($i = 1; $i <= $months; $i++)
54
        {
55
            $digit = $i < 10 ? '0' . $i : $i;
56
            $currentDate = "{$currentYear}-{$digit}-20";
57
            $selected = $currentMonth == $digit ? 'selected' : '';
58
            $formatedMonth = $formatter != ''
59
                ? ucwords($formatter->format((new \DateTime($currentDate))))
60
                : (new \DateTime($currentDate))->format($this->monthFormat == 'small' ? 'M' : 'F');
61
62
            $value = $this->valueFormat == 'number'
63
                ? $i
64
                : strtolower(str_replace("ç", 'c', $formatedMonth));
65
66
            $options .= "<option value='{$value}' {$selected}>{$formatedMonth}</option>";
67
        }
68
69
        return "<select id='{$id}' class='{$class}' name='{$name}' {$dataAttr}>{$options}</select>";
70
    }
71
}
72