Completed
Push — master ( cf5e41...093e42 )
by Daniel
01:53
created

BankHoliday::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
/*
4
 * The MIT License
5
 *
6
 * Copyright 2016 Daniel Popiniuc
7
 *
8
 * Permission is hereby granted, free of charge, to any person obtaining a copy
9
 * of this software and associated documentation files (the "Software"), to deal
10
 * in the Software without restriction, including without limitation the rights
11
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
 * copies of the Software, and to permit persons to whom the Software is
13
 * furnished to do so, subject to the following conditions:
14
 *
15
 * The above copyright notice and this permission notice shall be included in
16
 * all copies or substantial portions of the Software.
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
 * THE SOFTWARE.
25
 */
26
27
namespace danielgp\bank_holidays;
28
29
/**
30
 * Description of BankHoliday
31
 *
32
 * @author Daniel Popiniuc
33
 */
34
class BankHoliday
35
{
36
37
    use Romanian;
38
39
    public function __construct()
40
    {
41
        $refDate = new \DateTime('now');
42
        $this->listBankHolidays($refDate);
43
        $this->listWorkingDaysM2M($refDate);
44
    }
45
46
    private function listBankHolidays($refDate)
47
    {
48
        $thisYearHolidays = $this->setHolidays($refDate);
49
        echo '<h1>For ' . $refDate->format('Y') . ' the Romanian bank holidays are:</h1>'
50
        . '<ul>';
51
        foreach ($thisYearHolidays as $value) {
52
            echo '<li>' . $value . ' --- ' . date('l, d F Y', $value) . '</li>';
53
        }
54
        echo '</ul>';
55
    }
56
57
    private function listWorkingDaysM2M($refDate)
58
    {
59
        echo '<h1>For ' . $refDate->format('Y') . ' the Romanian working days month by month are:</h1>'
60
        . '<ul>';
61
        $wkDaysInMonth = [];
62
        for ($crtMonth = 1; $crtMonth <= 12; $crtMonth++) {
63
            $crtDate = \DateTime::createFromFormat('Y-n-j', $refDate->format('Y') . '-' . $crtMonth . '-1');
64
            if ($crtDate !== false) {
65
                $wkDaysInMonth[] = $this->setWorkingDaysInMonth($crtDate);
66
                echo '<li>' . $crtDate->format('M Y') . ' = '
67
                . $this->setWorkingDaysInMonth($crtDate) . ' working days</li>';
68
            }
69
        }
70
        echo '</ul>';
71
        $this->overallStatistics($wkDaysInMonth, $refDate);
72
    }
73
74
    private function overallStatistics($wkDaysInMonth, $refDate)
75
    {
76
        echo '<p>Total # of working days in ' . $refDate->format('Y') . ' is '
77
        . array_sum($wkDaysInMonth) . ' days</p>';
78
        echo '<p>Average working days for ' . $refDate->format('Y') . ' is '
79
        . round((array_sum($wkDaysInMonth) / count($wkDaysInMonth)), 2) . ' days</p>';
80
    }
81
}
82