Date_Japanese_Era::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Conversion between Japanese Era / Gregorian calendar
4
 *
5
 * This module is inspired by Date::Japanese::Era (Perl module).
6
 *
7
 * PHP version 5.2
8
 *
9
 * Copyright (c) 2009-2010 Shinya Ohyanagi, All rights reserved.
10
 *
11
 * Redistribution and use in source and binary forms, with or without
12
 * modification, are permitted provided that the following conditions
13
 * are met:
14
 *
15
 *   * Redistributions of source code must retain the above copyright
16
 *     notice, this list of conditions and the following disclaimer.
17
 *
18
 *   * Redistributions in binary form must reproduce the above copyright
19
 *     notice, this list of conditions and the following disclaimer in
20
 *     the documentation and/or other materials provided with the
21
 *     distribution.
22
 *
23
 *   * Neither the name of Shinya Ohyanagi nor the names of his
24
 *     contributors may be used to endorse or promote products derived
25
 *     from this software without specific prior written permission.
26
 *
27
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
34
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
35
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38
 * POSSIBILITY OF SUCH DAMAGE.
39
 *
40
 * @category  Date
41
 * @package   Date_Japanese
42
 * @version   $id$
43
 * @copyright 2009-2010 Shinya Ohyanagi
44
 * @author    Shinya Ohyanagi <[email protected]>
45
 * @license   New BSD License
46
 * @link      http://search.cpan.org/~miyagawa/Date-Japanese-Era-0.06/
47
 */
48
49
namespace Date_Japanese_Era;
50
51
/**
52
 * Conversion between Japanese Era / Gregorian calendar
53
 *
54
 * <pre>
55
 *   This module is inspired by Date::Japanese::Era (Perl module).
56
 * </pre>
57
 *
58
 * @category  Date
59
 * @package   Date_Japanese
60
 * @version   $id$
61
 * @copyright 2009-2010 Shinya Ohyanagi
62
 * @author    Shinya Ohyanagi <[email protected]>
63
 * @license   New BSD License
64
 * @link      http://search.cpan.org/~miyagawa/Date-Japanese-Era-0.06/
65
 */
66
class Date_Japanese_Era
67
{
68
    /**
69
     * Version
70
     */
71
    const VERSION = '0.1.1';
72
73
    /**
74
     * Era name
75
     *
76
     * @var mixed
77
     * @access private
78
     */
79
    private $_name;
80
81
    /**
82
     * Ailas of $_name
83
     *
84
     * @var    mixed
85
     * @access private
86
     */
87
    private $_gengou;
88
89
    /**
90
     * Era year
91
     *
92
     * @var    mixed
93
     * @access private
94
     */
95
    private $_year;
96
97
    /**
98
     * Gregorian year
99
     *
100
     * @var    mixed
101
     * @access private
102
     */
103
    private $_gregorianYear;
104
105
    /**
106
     * Era name as ascii
107
     *
108
     * @var    mixed
109
     * @access private
110
     */
111
    private $_nameAscii;
112
113
    /**
114
     * __construct
115
     *
116
     * @param  array $args Args
117
     * @access public
118
     * @throws Date_Japanese_Era_Exception
119
     * @return void
120
     */
121
    public function __construct(array $args)
122
    {
123
        if (count($args) === 3) {
124
            $this->_fromYmd($args);
125
        } else if (count($args) === 2) {
126
            $this->_fromEra($args);
127
        } else {
128
            throw new Date_Japanese_Era_Exception(
129
                'Invalid number of arguments: ' . count($args)
130
            );
131
        }
132
    }
133
134
135
    /**
136
     * Get property
137
     *
138
     * @param  mixed $name Property name
139
     * @access public
140
     * @return mixed Property if exists
141
     */
142
    public function __get($name)
143
    {
144
        if (property_exists(__CLASS__, '_' . $name)) {
145
            return $this->{'_' . $name};
146
        }
147
        return null;
148
    }
149
150
    /**
151
     * Convert from Ymd
152
     *
153
     * @param  array $args
154
     * @access private
155
     * @throws Date_Japanese_Era_Exception Invalid date
156
     * @return void
157
     */
158
    private function _fromYmd(array $args)
159
    {
160
        list($year, $month, $day) = $args;
161
        if (checkdate($month, $day, $year) === false) {
162
            throw new Date_Japanese_Era_Exception('Invalid date.');
163
        }
164
165
        $era   = Date_Japanese_Era_Table::$ERA_TABLE;
166
        $table = array_reverse($era);
167
        foreach ($table as $key => $val) {
168
            $start = sprintf('%02d%02d%02d', $val[1], $val[2], $val[3]);
169
            $end   = sprintf('%02d%02d%02d', $val[4], $val[5], $val[6]);
170
            $ymd   = sprintf('%02d%02d%02d', $year, $month, $day);
171
172
            if ($ymd >= $start && $ymd <= $end) {
173
                $this->_name          = $key;
174
                $this->_year          = ($year - $val[1]) + 1;
175
                $this->_gengou        = $key;
176
                $this->_nameAscii     = $val[0];
177
                $this->_gregorianYear = $year;
178
                return;
179
            }
180
        }
181
        throw new Date_Japanese_Era_Exception(
182
            'Unsupported date: ' . implode('-', $args)
183
        );
184
    }
185
186
    /**
187
     * Convert from era
188
     *
189
     * @param  array $args Args
190
     * @access private
191
     * @throws Date_Japanese_Era_Exception Unsupported era name
192
     * @return void
193
     */
194
    private function _fromEra(array $args)
195
    {
196
        list($era, $year) = $args;
197
        $data = Date_Japanese_Era_Table::$ERA_TABLE;
198
        if (preg_match('/^[a-zA-Z]+$/', $era)) {
199
            $era = $this->_ascii2ja($era);
200
        }
201
202
        if (!isset($data[$era])) {
203
            throw new Date_Japanese_Era_Exception('Unknown era name: ' . $era);
204
        }
205
206
        $gYear = $data[$era][1] + $year - 1;
207
        if ($gYear > $data[$era][4]) {
208
            throw new Date_Japanese_Era_Exception(
209
                'Invalid combination of era and year: ' . $era . '-' . $year
210
            );
211
        }
212
        $this->_name          = $era;
213
        $this->_year          = $year;
214
        $this->_gengou        = $era;
215
        $this->_nameAscii     = $data[$era][0];
216
        $this->_gregorianYear = $gYear;
217
    }
218
219
220
    /**
221
     * Convert ascii era name to japanese
222
     *
223
     * @param  mixed $ascii Era name written in ascii
224
     * @access private
225
     * @return string Era name
226
     */
227
    private function _ascii2ja($ascii)
228
    {
229
        $era = Date_Japanese_Era_Table::eraJa2Ascii();
230
        return isset($era[$ascii][0]) ? $era[$ascii][0] : null;
231
    }
232
}
233