Completed
Push — master ( 3c0ddf...aeaf53 )
by Roni
06:36
created

BnDateTime::create()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 19
rs 8.8571
c 1
b 0
f 0
cc 5
eloc 14
nc 5
nop 1
1
<?php
2
3
/*
4
 * This file is part of the EasyBanglaDate package.
5
 *
6
 * Copyright (c) 2015 Roni Saha
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
13
namespace EasyBanglaDate\Types;
14
15
use EasyBanglaDate\Common\BaseDateTime;
16
use EasyBanglaDate\Tools\Converter;
17
18
class BnDateTime  extends BaseDateTime
19
{
20
    /** @var  DateTime */
21
    private $_dateTime;
22
23
    /** @var  \DateTime */
24
    private $_phpDateTime;
25
26
    protected static $bnMonths = array(
27
        'F' => array('বৈশাখ','জ্যৈষ্ঠ','আষাঢ়','শ্রাবণ','ভাদ্র','আশ্বিন','কার্তিক','অগ্রহায়ণ','পৌষ','মাঘ','ফাল্গুন','চৈত্র'),
28
        'M' => array('বৈশাখ','জ্যৈষ্ঠ','আষাঢ়','শ্রাবণ','ভাদ্র','আশ্বিন','কার্তিক','অগ্র','পৌষ','মাঘ','ফাল্গুন','চৈত্র')
29
    );
30
31
    protected static $enMonths = array(
32
        'F' => array('Boishakh','Joishtha','Ashar','Srabon','Bhadra','Ashwin','Kartik','Ogrohayon','Poush','Magh','Falgun','Choitra'),
33
        'M' => array('Boi','Joi','Ash','Sra','Bha','Ash','Kar','Ogr','Pou','Mag','Fal','Cho')
34
    );
35
    protected static $daysInMonth = array('', 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 30, 30);
36
    protected static $enSuffix = array('th', 'st', 'nd', 'rd');
37
38
    protected static $parameterList = array('a', 'A', 'l', 'D');
39
40
    private $morning = 6;
41
42
    public static function create($time = 'now')
43
    {
44
        $dateTime = null;
45
46
        if (is_string($time)) {
47
            return new static($time);
48
        } elseif ($time instanceof BnDateTime) {
49
            return $time;
50
        } elseif ($time instanceof \DateTime) {
51
            $dateTime = new static();
52
            $dateTime->setTimestamp($time->getTimestamp());
53
            $dateTime->setTimezone($time->getTimezone());
54
        } elseif (is_int($time)) {
55
            $dateTime = new static();
56
            $dateTime->setTimestamp($time);
57
        }
58
59
        return $dateTime;
60
    }
61
62
    public function __construct($time = 'now', \DateTimeZone $timezone = null)
63
    {
64
        parent::__construct($time, $timezone);
65
        $this->_dateTime = new DateTime();
66
        $this->_phpDateTime = new \DateTime();
67
    }
68
69
    public function format($format)
70
    {
71
        $bnDate = $this->getBengaliDateMonthYear();
72
73
        $out = $this->replaceTimes($format);
74
        $out = $this->replaceTimePrefix($out);
75
        $out = $this->replaceBnSuffix($out, $bnDate);
76
        $out = $this->replaceDateNumbers($out, $bnDate);
77
        $out = $this->replaceMonthString($out, $bnDate, self::$bnMonths, '%s');
78
        $out = $this->replaceDays($out);
79
        $out = $this->replaceMeridian($out);
80
81
        return $this->translateNumbers($out);
82
    }
83
84
    public function enFormat($format)
85
    {
86
        $bnDate = $this->getBengaliDateMonthYear();
87
88
        $out = $this->replaceTimes($format);
89
        $out = $this->replaceDateNumbers($out, $bnDate);
90
        $out = $this->replaceToPlaceHolders($out);
91
        $out = $this->replaceMonthString($out, $bnDate, self::$enMonths, '{%s}');
92
        $out = str_replace('{S}', $this->getEnSuffix($bnDate['day']), $out);
93
94
        return $this->replacePlaceHolders($out);
95
    }
96
97
    /**
98
     * @param int $morning
99
     */
100
    public function setMorning($morning)
101
    {
102
        $this->morning = $morning;
103
    }
104
105
    public function setDate($year, $month, $day)
106
    {
107
        $engTime = Converter::getEnglishTimeFromBanglaDate(
108
            $this->getNativeDateTimeObject(),
109
            array('day' => $day, 'month' => $month, 'year' => $year),
110
            $this->morning
111
        );
112
113
        $this->setTimestamp($engTime->getTimestamp());
114
115
        return $this;
116
    }
117
118
    /**
119
     * @return DateTime
120
     */
121
    public function getDateTime()
122
    {
123
        return $this->_dateTime
124
            ->setTimestamp($this->getTimestamp())
125
            ->setTimezone($this->getTimezone())
126
            ;
127
    }
128
129
    private function replaceToPlaceHolders($out) {
130
        $paramList = array_merge(self::$parameterList , array("S", "M", "F"));
131
132
        foreach($paramList as $item) {
133
            $out = str_replace($item, '{' . $item .'}', $out);
134
        }
135
136
        return $out;
137
    }
138
139
    private function replacePlaceHolders($out) {
140
141
        foreach(self::$parameterList as $item) {
142
            $out = str_replace('{' . $item .'}', $this->_format($item), $out);
143
        }
144
145
        return $out;
146
    }
147
148
    /**
149
     * @param $format
150
     * @param $bnDate
151
     * @return mixed
152
     */
153
    protected function replaceBnSuffix($format, $bnDate)
154
    {
155
        return str_replace('S', $this->getBnSuffix((int)$bnDate['day']), $format);
156
    }
157
158
    protected function replaceDateNumbers($format, $bnDate = array())
159
    {
160
        $format = str_replace('d', str_pad($bnDate['day'], 2, 0, STR_PAD_LEFT), $format);
161
        $format = str_replace('j', $bnDate['day'], $format);
162
        $format = str_replace('t', $this->getDayInMonth($bnDate['month']), $format);
163
        $format = str_replace('m', str_pad($bnDate['month'], 2, 0, STR_PAD_LEFT), $format);
164
        $format = str_replace('n', $bnDate['month'], $format);
165
        $format = str_replace('Y', $bnDate['year'], $format);
166
        $format = str_replace('y', substr($bnDate['year'], -2), $format);
167
168
        return $format;
169
    }
170
171
    /**
172
     * @return \DateTime
173
     */
174
    private function getNativeDateTimeObject()
175
    {
176
        return $this->_phpDateTime
177
            ->setTimestamp($this->getTimestamp())
178
            ->setTimezone($this->getTimezone())
179
            ;
180
    }
181
182
    private function getBengaliDateMonthYear()
183
    {
184
        return Converter::getBengaliDateMonthYear($this->getNativeDateTimeObject(), $this->morning);
185
    }
186
187
    private function getDayInMonth($month)
188
    {
189
        if($month == 11 && $this->_format('L')) {
190
            return 31;
191
        }
192
193
        return self::$daysInMonth[$month];
194
    }
195
196
    protected function getEnSuffix($num) {
197
198
        $index = $this->getSuffixArrayIndexFromNumber($num);
199
200
        if($index > 3) {
201
            $index = 0;
202
        }
203
204
        return self::$enSuffix[$index];
205
    }
206
207
    /**
208
     * @param $template
209
     * @param $bnDate
210
     * @param $monthArray
211
     * @param $keyTemplate
212
     * @return mixed
213
     */
214
    protected function replaceMonthString($template, $bnDate, $monthArray, $keyTemplate)
215
    {
216
        foreach(array('F','M') as $key){
217
            $template = str_replace(sprintf($keyTemplate, $key), $monthArray[$key][$bnDate['month'] - 1], $template);
218
        }
219
220
        return $template;
221
    }
222
223
    /**
224
     * @param $num
225
     * @return int
226
     */
227
    protected function getSuffixArrayIndexFromNumber($num)
228
    {
229
        if (in_array($num, array(11, 12, 13))) {
230
            return 0;
231
        }
232
233
        return ($num % 10);
234
    }
235
}