Complex classes like DateTime often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DateTime, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 29 | class DateTime |
||
| 30 | { |
||
| 31 | /** |
||
| 32 | * Getter Interface |
||
| 33 | * |
||
| 34 | * @var Getter\GetterInterface |
||
| 35 | * @since 2.0.0 |
||
| 36 | */ |
||
| 37 | private static $getter; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Parser Interface |
||
| 41 | * |
||
| 42 | * @var Parser\ParserInterface |
||
| 43 | * @since 2.0.0 |
||
| 44 | */ |
||
| 45 | private static $parser; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Since Interface |
||
| 49 | * |
||
| 50 | * @var Since\SinceInterface |
||
| 51 | * @since 2.0.0 |
||
| 52 | */ |
||
| 53 | private static $since; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Translator object |
||
| 57 | * |
||
| 58 | * @var Translator\AbstractTranslator |
||
| 59 | * @since 2.0.0 |
||
| 60 | */ |
||
| 61 | private static $translator; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Strategy Interface |
||
| 65 | * |
||
| 66 | * @var Strategy\StrategyInterface |
||
| 67 | * @since 2.0.0 |
||
| 68 | */ |
||
| 69 | private $strategy; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * PHP DateTime object |
||
| 73 | * |
||
| 74 | * @var \DateTime |
||
| 75 | * @since 2.0.0 |
||
| 76 | */ |
||
| 77 | private $datetime; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Constructor. |
||
| 81 | * |
||
| 82 | * @param mixed $datetime Either a Joomla\Date object, a PHP DateTime object |
||
| 83 | * or a string in a format accepted by strtotime(). |
||
| 84 | * @param \DateTimeZone $timezone The timezone. |
||
| 85 | * |
||
| 86 | * @since 2.0.0 |
||
| 87 | */ |
||
| 88 | 210 | public function __construct($datetime, \DateTimeZone $timezone = null) |
|
| 89 | { |
||
| 90 | 210 | if ($datetime instanceof \DateTime) |
|
| 91 | 210 | { |
|
| 92 | 198 | $this->datetime = clone $datetime; |
|
| 93 | 198 | } |
|
| 94 | 48 | elseif ($datetime instanceof Date) |
|
| 95 | { |
||
| 96 | 19 | $this->datetime = $datetime->getDateTime(); |
|
| 97 | 19 | } |
|
| 98 | else |
||
| 99 | { |
||
| 100 | 36 | $this->datetime = new \DateTime($datetime); |
|
| 101 | } |
||
| 102 | |||
| 103 | 210 | if (!is_null($timezone)) |
|
| 104 | 210 | { |
|
| 105 | 1 | $this->datetime->setTimezone($timezone); |
|
| 106 | 1 | } |
|
| 107 | 210 | } |
|
| 108 | |||
| 109 | /** |
||
| 110 | * Parses to DateTime object. |
||
| 111 | * |
||
| 112 | * @param string $name Name of the parser. |
||
| 113 | * @param mixed $value The value to parse. |
||
| 114 | * |
||
| 115 | * @return DateTime |
||
| 116 | * |
||
| 117 | * @since 2.0.0 |
||
| 118 | */ |
||
| 119 | 4 | public static function parse($name, $value) |
|
| 120 | { |
||
| 121 | 4 | return static::getParser()->parse($name, $value); |
|
|
|
|||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Creates a DateTime object from the given format. |
||
| 126 | * |
||
| 127 | * @param string $format Format accepted by date(). |
||
| 128 | * @param string $time String representing the time. |
||
| 129 | * @param \DateTimeZone $timezone The timezone. |
||
| 130 | * |
||
| 131 | * @return DateTime |
||
| 132 | * |
||
| 133 | * @since 2.0.0 |
||
| 134 | */ |
||
| 135 | 37 | public static function createFromFormat($format, $time, \DateTimeZone $timezone = null) |
|
| 136 | { |
||
| 137 | 37 | $datetime = is_null($timezone) ? \DateTime::createFromFormat($format, $time) : \DateTime::createFromFormat($format, $time, $timezone); |
|
| 138 | |||
| 139 | 37 | return new static($datetime); |
|
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Creates a DateTime object. |
||
| 144 | * |
||
| 145 | * @param integer $year The year. |
||
| 146 | * @param integer $month The month. |
||
| 147 | * @param integer $day The day of the month. |
||
| 148 | * @param integer $hour The hour. |
||
| 149 | * @param integer $minute The minute. |
||
| 150 | * @param integer $second The second. |
||
| 151 | * @param \DateTimeZone $timezone The timezone. |
||
| 152 | * |
||
| 153 | * @return DateTime |
||
| 154 | * |
||
| 155 | * @since 2.0.0 |
||
| 156 | */ |
||
| 157 | 37 | public static function create($year, $month = '01', $day = '01', $hour = '00', $minute = '00', $second = '00', \DateTimeZone $timezone = null) |
|
| 158 | { |
||
| 159 | 37 | $time = sprintf('%04s-%02s-%02s %02s:%02s:%02s', $year, $month, $day, $hour, $minute, $second); |
|
| 160 | |||
| 161 | 37 | return static::createFromFormat('Y-m-d H:i:s', $time, $timezone); |
|
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Creates a DateTime object with time of the midnight. |
||
| 166 | * |
||
| 167 | * @param integer $year The year. |
||
| 168 | * @param integer $month The month. |
||
| 169 | * @param integer $day The day of the month. |
||
| 170 | * @param \DateTimeZone $timezone The timezone. |
||
| 171 | * |
||
| 172 | * @return DateTime |
||
| 173 | * |
||
| 174 | * @since 2.0.0 |
||
| 175 | */ |
||
| 176 | 26 | public static function createFromDate($year, $month = '01', $day = '01', \DateTimeZone $timezone = null) |
|
| 177 | { |
||
| 178 | 26 | return static::create($year, $month, $day, '00', '00', '00', $timezone); |
|
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Creates a DateTime object with date of today. |
||
| 183 | * |
||
| 184 | * @param integer $hour The hour. |
||
| 185 | * @param integer $minute The minute. |
||
| 186 | * @param integer $second The second. |
||
| 187 | * @param \DateTimeZone $timezone The timezone. |
||
| 188 | * |
||
| 189 | * @return DateTime |
||
| 190 | * |
||
| 191 | * @since 2.0.0 |
||
| 192 | */ |
||
| 193 | 5 | public static function createFromTime($hour = '00', $minute = '00', $second = '00', \DateTimeZone $timezone = null) |
|
| 194 | { |
||
| 195 | 5 | return static::create(date('Y'), date('m'), date('d'), $hour, $minute, $second, $timezone); |
|
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Creates a DateTime object which represents now. |
||
| 200 | * |
||
| 201 | * @param \DateTimeZone $timezone The timezone. |
||
| 202 | * |
||
| 203 | * @return DateTime |
||
| 204 | * |
||
| 205 | * @since 2.0.0 |
||
| 206 | */ |
||
| 207 | 2 | public static function now(\DateTimeZone $timezone = null) |
|
| 208 | { |
||
| 209 | 2 | return static::createFromTime(date('H'), date('i'), date('s'), $timezone); |
|
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Creates a DateTime object which represents today. |
||
| 214 | * |
||
| 215 | * @param \DateTimeZone $timezone The timezone. |
||
| 216 | * |
||
| 217 | * @return DateTime |
||
| 218 | * |
||
| 219 | * @since 2.0.0 |
||
| 220 | */ |
||
| 221 | 23 | public static function today(\DateTimeZone $timezone = null) |
|
| 222 | { |
||
| 223 | 23 | return static::createFromDate(date('Y'), date('m'), date('d'), $timezone); |
|
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Creates a DateTime object which represents tomorrow. |
||
| 228 | * |
||
| 229 | * @param \DateTimeZone $timezone The timezone. |
||
| 230 | * |
||
| 231 | * @return DateTime |
||
| 232 | * |
||
| 233 | * @since 2.0.0 |
||
| 234 | */ |
||
| 235 | 11 | public static function tomorrow(\DateTimeZone $timezone = null) |
|
| 236 | { |
||
| 237 | 11 | $today = static::today($timezone); |
|
| 238 | |||
| 239 | 11 | return $today->addDays(1); |
|
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Creates a DateTime object which represents yesterday. |
||
| 244 | * |
||
| 245 | * @param \DateTimeZone $timezone The timezone. |
||
| 246 | * |
||
| 247 | * @return DateTime |
||
| 248 | * |
||
| 249 | * @since 2.0.0 |
||
| 250 | */ |
||
| 251 | 11 | public static function yesterday(\DateTimeZone $timezone = null) |
|
| 252 | { |
||
| 253 | 11 | $today = static::today($timezone); |
|
| 254 | |||
| 255 | 11 | return $today->subDays(1); |
|
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Checks if the current date is after the date given as parameter. |
||
| 260 | * |
||
| 261 | * @param DateTime $datetime The date to compare to. |
||
| 262 | * |
||
| 263 | * @return boolean |
||
| 264 | * |
||
| 265 | * @since 2.0.0 |
||
| 266 | */ |
||
| 267 | 224 | public function isAfter(DateTime $datetime) |
|
| 268 | { |
||
| 269 | 224 | return $this->datetime > $datetime->datetime; |
|
| 270 | } |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Checks if the current date is before the date given as parameter. |
||
| 274 | * |
||
| 275 | * @param DateTime $datetime The date to compare to. |
||
| 276 | * |
||
| 277 | * @return boolean |
||
| 278 | * |
||
| 279 | * @since 2.0.0 |
||
| 280 | */ |
||
| 281 | 96 | public function isBefore(DateTime $datetime) |
|
| 282 | { |
||
| 283 | 96 | return $this->datetime < $datetime->datetime; |
|
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Checks if the current date is equals to the date given as parameter. |
||
| 288 | * |
||
| 289 | * @param DateTime $datetime The date to compare to. |
||
| 290 | * |
||
| 291 | * @return boolean |
||
| 292 | * |
||
| 293 | * @since 2.0.0 |
||
| 294 | */ |
||
| 295 | 18 | public function equals(DateTime $datetime) |
|
| 296 | { |
||
| 297 | 18 | return $this->datetime == $datetime->datetime; |
|
| 298 | } |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Returns the difference between two objects. |
||
| 302 | * |
||
| 303 | * @param DateTime $datetime The date to compare to. |
||
| 304 | * @param boolean $absolute Should the interval be forced to be positive? |
||
| 305 | * |
||
| 306 | * @return DateInterval |
||
| 307 | * |
||
| 308 | * @since 2.0.0 |
||
| 309 | */ |
||
| 310 | 144 | public function diff(DateTime $datetime, $absolute = false) |
|
| 311 | { |
||
| 312 | 144 | return new DateInterval($this->datetime->diff($datetime->datetime, $absolute)); |
|
| 313 | } |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Returns a new DateTime object by adding an interval to the current one. |
||
| 317 | * |
||
| 318 | * @param DateInterval $interval The interval to be added. |
||
| 319 | * |
||
| 320 | * @return DateTime |
||
| 321 | * |
||
| 322 | * @since 2.0.0 |
||
| 323 | */ |
||
| 324 | 101 | public function add(DateInterval $interval) |
|
| 325 | { |
||
| 326 | 101 | return $this->modify( |
|
| 327 | function (\DateTime $datetime) use ($interval) |
||
| 328 | { |
||
| 329 | 101 | $datetime->add($interval->getDateInterval()); |
|
| 330 | 101 | } |
|
| 331 | 101 | ); |
|
| 332 | } |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Returns a new DateTime object by subtracting an interval from the current one. |
||
| 336 | * |
||
| 337 | * @param DateInterval $interval The interval to be subtracted. |
||
| 338 | * |
||
| 339 | * @return DateTime |
||
| 340 | * |
||
| 341 | * @since 2.0.0 |
||
| 342 | */ |
||
| 343 | 97 | public function sub(DateInterval $interval) |
|
| 344 | { |
||
| 345 | 97 | return $this->modify( |
|
| 346 | 97 | function (\DateTime $datetime) use ($interval) |
|
| 347 | { |
||
| 348 | 97 | $datetime->sub($interval->getDateInterval()); |
|
| 349 | 97 | } |
|
| 350 | 97 | ); |
|
| 351 | } |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Returns a new DateTime object by adding the specified number of days to the current one. |
||
| 355 | * |
||
| 356 | * @param integer $value Number of days to be added. |
||
| 357 | * |
||
| 358 | * @return DateTime |
||
| 359 | * |
||
| 360 | * @since 2.0.0 |
||
| 361 | */ |
||
| 362 | 35 | public function addDays($value) |
|
| 363 | { |
||
| 364 | 35 | return $this->calc($value, 'P%dD'); |
|
| 365 | } |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Returns a new DateTime object by subtracting the specified number of days from the current one. |
||
| 369 | * |
||
| 370 | * @param integer $value Number of days to be subtracted. |
||
| 371 | * |
||
| 372 | * @return DateTime |
||
| 373 | * |
||
| 374 | * @since 2.0.0 |
||
| 375 | */ |
||
| 376 | 19 | public function subDays($value) |
|
| 377 | { |
||
| 378 | 19 | return $this->addDays(-intval($value)); |
|
| 379 | } |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Returns a new DateTime object by adding the specified number of weeks to the current one. |
||
| 383 | * |
||
| 384 | * @param integer $value Number of weeks to be added. |
||
| 385 | * |
||
| 386 | * @return DateTime |
||
| 387 | * |
||
| 388 | * @since 2.0.0 |
||
| 389 | */ |
||
| 390 | 16 | public function addWeeks($value) |
|
| 391 | { |
||
| 392 | 16 | return $this->calc($value, 'P%dW'); |
|
| 393 | } |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Returns a new DateTime object by subtracting the specified number of weeks from the current one. |
||
| 397 | * |
||
| 398 | * @param integer $value Number of weeks to be subtracted. |
||
| 399 | * |
||
| 400 | * @return DateTime |
||
| 401 | * |
||
| 402 | * @since 2.0.0 |
||
| 403 | */ |
||
| 404 | 8 | public function subWeeks($value) |
|
| 405 | { |
||
| 406 | 8 | return $this->addWeeks(-intval($value)); |
|
| 407 | } |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Returns a new DateTime object by adding the specified number of months to the current one. |
||
| 411 | * |
||
| 412 | * @param integer $value Number of months to be added. |
||
| 413 | * |
||
| 414 | * @return DateTime |
||
| 415 | * |
||
| 416 | * @since 2.0.0 |
||
| 417 | */ |
||
| 418 | 48 | public function addMonths($value) |
|
| 419 | { |
||
| 420 | 48 | return $this->fixMonth($this->calc($value, 'P%dM')); |
|
| 421 | } |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Returns a new DateTime object by subtracting the specified number of months from the current one. |
||
| 425 | * |
||
| 426 | * @param integer $value Number of months to be subtracted. |
||
| 427 | * |
||
| 428 | * @return DateTime |
||
| 429 | * |
||
| 430 | * @since 2.0.0 |
||
| 431 | */ |
||
| 432 | 34 | public function subMonths($value) |
|
| 433 | { |
||
| 434 | 34 | return $this->addMonths(-intval($value)); |
|
| 435 | } |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Returns a new DateTime object by adding the specified number of years to the current one. |
||
| 439 | * |
||
| 440 | * @param integer $value Number of years to be added. |
||
| 441 | * |
||
| 442 | * @return DateTime |
||
| 443 | * |
||
| 444 | * @since 2.0.0 |
||
| 445 | */ |
||
| 446 | 16 | public function addYears($value) |
|
| 447 | { |
||
| 448 | 16 | return $this->fixMonth($this->calc($value, 'P%dY')); |
|
| 449 | } |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Returns a new DateTime object by subtracting the specified number of years from the current one. |
||
| 453 | * |
||
| 454 | * @param integer $value Number of years to be subtracted. |
||
| 455 | * |
||
| 456 | * @return DateTime |
||
| 457 | * |
||
| 458 | * @since 2.0.0 |
||
| 459 | */ |
||
| 460 | 8 | public function subYears($value) |
|
| 461 | { |
||
| 462 | 8 | return $this->addYears(-intval($value)); |
|
| 463 | } |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Returns a new DateTime object by adding the specified number of seconds to the current one. |
||
| 467 | * |
||
| 468 | * @param integer $value Number of seconds to be added. |
||
| 469 | * |
||
| 470 | * @return DateTime |
||
| 471 | * |
||
| 472 | * @since 2.0.0 |
||
| 473 | */ |
||
| 474 | 8 | public function addSeconds($value) |
|
| 475 | { |
||
| 476 | 8 | return $this->calc($value, 'PT%dS'); |
|
| 477 | } |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Returns a new DateTime object by subtracting the specified number of seconds from the current one. |
||
| 481 | * |
||
| 482 | * @param integer $value Number of seconds to be subtracted. |
||
| 483 | * |
||
| 484 | * @return DateTime |
||
| 485 | * |
||
| 486 | * @since 2.0.0 |
||
| 487 | */ |
||
| 488 | 4 | public function subSeconds($value) |
|
| 489 | { |
||
| 490 | 4 | return $this->addSeconds(-intval($value)); |
|
| 491 | } |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Returns a new DateTime object by adding the specified number of minutes to the current one. |
||
| 495 | * |
||
| 496 | * @param integer $value Number of minutes to be added. |
||
| 497 | * |
||
| 498 | * @return DateTime |
||
| 499 | * |
||
| 500 | * @since 2.0.0 |
||
| 501 | */ |
||
| 502 | 8 | public function addMinutes($value) |
|
| 503 | { |
||
| 504 | 8 | return $this->calc($value, 'PT%dM'); |
|
| 505 | } |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Returns a new DateTime object by subtracting the specified number of minutes from the current one. |
||
| 509 | * |
||
| 510 | * @param integer $value Number of minutes to be subtracted. |
||
| 511 | * |
||
| 512 | * @return DateTime |
||
| 513 | * |
||
| 514 | * @since 2.0.0 |
||
| 515 | */ |
||
| 516 | 4 | public function subMinutes($value) |
|
| 517 | { |
||
| 518 | 4 | return $this->addMinutes(-intval($value)); |
|
| 519 | } |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Returns a new DateTime object by adding the specified number of hours to the current one. |
||
| 523 | * |
||
| 524 | * @param integer $value Number of hours to be added. |
||
| 525 | * |
||
| 526 | * @return DateTime |
||
| 527 | * |
||
| 528 | * @since 2.0.0 |
||
| 529 | */ |
||
| 530 | 8 | public function addHours($value) |
|
| 531 | { |
||
| 532 | 8 | return $this->calc($value, 'PT%dH'); |
|
| 533 | } |
||
| 534 | |||
| 535 | /** |
||
| 536 | * Returns a new DateTime object by subtracting the specified number of hours from the current one. |
||
| 537 | * |
||
| 538 | * @param integer $value Number of hours to be subtracted. |
||
| 539 | * |
||
| 540 | * @return DateTime |
||
| 541 | * |
||
| 542 | * @since 2.0.0 |
||
| 543 | */ |
||
| 544 | 4 | public function subHours($value) |
|
| 545 | { |
||
| 546 | 4 | return $this->addHours(-intval($value)); |
|
| 547 | } |
||
| 548 | |||
| 549 | /** |
||
| 550 | * Returns a new DateTime object representing the start of the current day. |
||
| 551 | * |
||
| 552 | * @return DateTime |
||
| 553 | * |
||
| 554 | * @since 2.0.0 |
||
| 555 | */ |
||
| 556 | 74 | public function startOfDay() |
|
| 557 | { |
||
| 558 | 74 | return $this->modify(array($this->getStrategy(), 'startOfDay')); |
|
| 559 | } |
||
| 560 | |||
| 561 | /** |
||
| 562 | * Returns a new DateTime object representing the end of the current day. |
||
| 563 | * |
||
| 564 | * @return DateTime |
||
| 565 | * |
||
| 566 | * @since 2.0.0 |
||
| 567 | */ |
||
| 568 | 7 | public function endOfDay() |
|
| 569 | { |
||
| 570 | 7 | return $this->modify(array($this->getStrategy(), 'endOfDay')); |
|
| 571 | } |
||
| 572 | |||
| 573 | /** |
||
| 574 | * Returns a new DateTime object representing the start of the current week. |
||
| 575 | * |
||
| 576 | * @return DateTime |
||
| 577 | * |
||
| 578 | * @since 2.0.0 |
||
| 579 | */ |
||
| 580 | 2 | public function startOfWeek() |
|
| 581 | { |
||
| 582 | 2 | $startOfDay = $this->startOfDay(); |
|
| 583 | |||
| 584 | 2 | return $startOfDay->modify(array($this->getStrategy(), 'startOfWeek')); |
|
| 585 | } |
||
| 586 | |||
| 587 | /** |
||
| 588 | * Returns a new DateTime object representing the end of the current week. |
||
| 589 | * |
||
| 590 | * @return DateTime |
||
| 591 | * |
||
| 592 | * @since 2.0.0 |
||
| 593 | */ |
||
| 594 | 2 | public function endOfWeek() |
|
| 595 | { |
||
| 596 | 2 | $endOfDay = $this->endOfDay(); |
|
| 597 | |||
| 598 | 2 | return $endOfDay->modify(array($this->getStrategy(), 'endOfWeek')); |
|
| 599 | } |
||
| 600 | |||
| 601 | /** |
||
| 602 | * Returns a new DateTime object representing the start of the current month. |
||
| 603 | * |
||
| 604 | * @return DateTime |
||
| 605 | * |
||
| 606 | * @since 2.0.0 |
||
| 607 | */ |
||
| 608 | 2 | public function startOfMonth() |
|
| 609 | { |
||
| 610 | 2 | $startOfDay = $this->startOfDay(); |
|
| 611 | |||
| 612 | 2 | return $startOfDay->modify(array($this->getStrategy(), 'startOfMonth')); |
|
| 613 | } |
||
| 614 | |||
| 615 | /** |
||
| 616 | * Returns a new DateTime object representing the end of the current month. |
||
| 617 | * |
||
| 618 | * @return DateTime |
||
| 619 | * |
||
| 620 | * @since 2.0.0 |
||
| 621 | */ |
||
| 622 | 2 | public function endOfMonth() |
|
| 623 | { |
||
| 624 | 2 | $endOfDay = $this->endOfDay(); |
|
| 625 | |||
| 626 | 2 | return $endOfDay->modify(array($this->getStrategy(), 'endOfMonth')); |
|
| 627 | } |
||
| 628 | |||
| 629 | /** |
||
| 630 | * Returns a new DateTime object representing the start of the current year. |
||
| 631 | * |
||
| 632 | * @return DateTime |
||
| 633 | * |
||
| 634 | * @since 2.0.0 |
||
| 635 | */ |
||
| 636 | 2 | public function startOfYear() |
|
| 637 | { |
||
| 638 | 2 | $startOfDay = $this->startOfDay(); |
|
| 639 | |||
| 640 | 2 | return $startOfDay->modify(array($this->getStrategy(), 'startOfYear')); |
|
| 641 | } |
||
| 642 | |||
| 643 | /** |
||
| 644 | * Returns a new DateTime object representing the end of the current year. |
||
| 645 | * |
||
| 646 | * @return DateTime |
||
| 647 | * |
||
| 648 | * @since 2.0.0 |
||
| 649 | */ |
||
| 650 | 2 | public function endOfYear() |
|
| 651 | { |
||
| 652 | 2 | $endOfDay = $this->endOfDay(); |
|
| 653 | |||
| 654 | 2 | return $endOfDay->modify(array($this->getStrategy(), 'endOfYear')); |
|
| 655 | } |
||
| 656 | |||
| 657 | /** |
||
| 658 | * Returns date formatted according to given format. |
||
| 659 | * |
||
| 660 | * @param string $format Format accepted by date(). |
||
| 661 | * |
||
| 662 | * @return string |
||
| 663 | * |
||
| 664 | * @since 2.0.0 |
||
| 665 | */ |
||
| 666 | 88 | public function format($format) |
|
| 667 | { |
||
| 668 | 88 | $replace = array(); |
|
| 669 | |||
| 670 | // Loop all format characters and check if we can translate them. |
||
| 671 | 88 | for ($i = 0; $i < strlen($format); $i++) |
|
| 672 | { |
||
| 673 | 88 | $character = $format[$i]; |
|
| 674 | |||
| 675 | // Check if we can replace it with a translated version. |
||
| 676 | 88 | if (in_array($character, array('D', 'l', 'F', 'M'))) |
|
| 677 | 88 | { |
|
| 678 | switch ($character) |
||
| 679 | { |
||
| 680 | 2 | case 'D': |
|
| 681 | 1 | $key = $this->datetime->format('l'); |
|
| 682 | 1 | break; |
|
| 683 | 2 | case 'M': |
|
| 684 | 1 | $key = $this->datetime->format('F'); |
|
| 685 | 1 | break; |
|
| 686 | 2 | default: |
|
| 687 | 2 | $key = $this->datetime->format($character); |
|
| 688 | 2 | } |
|
| 689 | |||
| 690 | 2 | $original = $this->datetime->format($character); |
|
| 691 | 2 | $translated = $this->getTranslator()->get(strtolower($key)); |
|
| 692 | |||
| 693 | // Short notations. |
||
| 694 | 2 | if (in_array($character, array('D', 'M'))) |
|
| 695 | 2 | { |
|
| 696 | 2 | $translated = substr($translated, 0, 3); |
|
| 697 | 2 | } |
|
| 698 | |||
| 699 | // Add to replace list. |
||
| 700 | 2 | if ($translated && $original != $translated) |
|
| 701 | 2 | { |
|
| 702 | 2 | $replace[$original] = $translated; |
|
| 703 | 2 | } |
|
| 704 | 2 | } |
|
| 705 | 88 | } |
|
| 706 | |||
| 707 | // Replace translations. |
||
| 708 | if ($replace) |
||
| 709 | 88 | { |
|
| 710 | 2 | return str_replace(array_keys($replace), array_values($replace), $this->datetime->format($format)); |
|
| 711 | } |
||
| 712 | |||
| 713 | 88 | return $this->datetime->format($format); |
|
| 714 | } |
||
| 715 | |||
| 716 | /** |
||
| 717 | * Returns the difference in a human readable format. |
||
| 718 | * |
||
| 719 | * @param DateTime $datetime The date to compare to. Default is null and this means that |
||
| 720 | * the current object will be compared to the current time. |
||
| 721 | * @param integer $detailLevel How much details do you want to get. |
||
| 722 | * |
||
| 723 | * @return string |
||
| 724 | * |
||
| 725 | * @since 2.0.0 |
||
| 726 | */ |
||
| 727 | 121 | public function since(DateTime $datetime = null, $detailLevel = 1) |
|
| 728 | { |
||
| 729 | 121 | return $this->getSince()->since($this, $datetime, $detailLevel); |
|
| 730 | } |
||
| 731 | |||
| 732 | /** |
||
| 733 | * Returns the almost difference in a human readable format. |
||
| 734 | * |
||
| 735 | * @param DateTime $datetime The date to compare to. Default is null and this means that |
||
| 736 | * the current object will be compared to the current time. |
||
| 737 | * |
||
| 738 | * @return string |
||
| 739 | * |
||
| 740 | * @since 2.0.0 |
||
| 741 | */ |
||
| 742 | 22 | public function sinceAlmost(DateTime $datetime = null) |
|
| 746 | |||
| 747 | /** |
||
| 748 | * Magic method to access properties of the date given by class to the format method. |
||
| 749 | * |
||
| 750 | * @param string $name The name of the property. |
||
| 751 | * |
||
| 752 | * @return mixed |
||
| 753 | * |
||
| 754 | * @since 2.0.0 |
||
| 755 | */ |
||
| 756 | 30 | public function __get($name) |
|
| 757 | { |
||
| 758 | 30 | return $this->getGetter()->get($this, $name); |
|
| 759 | } |
||
| 760 | |||
| 761 | /** |
||
| 762 | * Alias for __get. |
||
| 763 | * |
||
| 764 | * @param string $name The name of the property. |
||
| 765 | * |
||
| 766 | * @return mixed |
||
| 767 | * |
||
| 768 | * @since 2.0.0 |
||
| 769 | */ |
||
| 770 | 14 | public function get($name) |
|
| 771 | { |
||
| 772 | 14 | return $this->$name; |
|
| 773 | } |
||
| 774 | |||
| 775 | /** |
||
| 776 | * Returns the timezone offset. |
||
| 777 | * |
||
| 778 | * @return integer |
||
| 779 | * |
||
| 780 | * @since 2.0.0 |
||
| 781 | */ |
||
| 782 | 2 | public function getOffset() |
|
| 786 | |||
| 787 | /** |
||
| 788 | * Returns the Unix timestamp representing the date. |
||
| 789 | * |
||
| 790 | * @return integer |
||
| 791 | * |
||
| 792 | * @since 2.0.0 |
||
| 793 | */ |
||
| 794 | 1 | public function getTimestamp() |
|
| 798 | |||
| 799 | /** |
||
| 800 | * Returns a DateTimeZone object. |
||
| 801 | * |
||
| 802 | * @return \DateTimeZone |
||
| 803 | * |
||
| 804 | * @since 2.0.0 |
||
| 805 | */ |
||
| 806 | 1 | public function getTimezone() |
|
| 810 | |||
| 811 | /** |
||
| 812 | * Returns a PHP DateTime object. |
||
| 813 | * |
||
| 814 | * @return \DateTime |
||
| 815 | * |
||
| 816 | * @since 2.0.0 |
||
| 817 | */ |
||
| 818 | 20 | public function getDateTime() |
|
| 822 | |||
| 823 | /** |
||
| 824 | * Sets the Since implementation. |
||
| 825 | * |
||
| 826 | * @param Since\SinceInterface $since The Since implementation. |
||
| 827 | * |
||
| 828 | * @return void |
||
| 829 | * |
||
| 830 | * @since 2.0.0 |
||
| 831 | */ |
||
| 832 | public static function setSince(Since\SinceInterface $since) |
||
| 836 | |||
| 837 | /** |
||
| 838 | * Sets the Translator implementation. |
||
| 839 | * |
||
| 840 | * @param Translator\AbstractTranslator $translator The Translator implementation. |
||
| 841 | * |
||
| 842 | * @return void |
||
| 843 | * |
||
| 844 | * @since 2.0.0 |
||
| 845 | */ |
||
| 846 | public static function setTranslator(Translator\AbstractTranslator $translator) |
||
| 850 | |||
| 851 | /** |
||
| 852 | * Sets the Getter implementation. |
||
| 853 | * |
||
| 854 | * @param Getter\GetterInterface $getter The Getter implementation. |
||
| 855 | * |
||
| 856 | * @return void |
||
| 857 | * |
||
| 858 | * @since 2.0.0 |
||
| 859 | */ |
||
| 860 | 15 | public static function setGetter(Getter\GetterInterface $getter) |
|
| 864 | |||
| 865 | /** |
||
| 866 | * Sets the Parser implementation. |
||
| 867 | * |
||
| 868 | * @param Parser\ParserInterface $parser The Parser implementation. |
||
| 869 | * |
||
| 870 | * @return void |
||
| 871 | * |
||
| 872 | * @since 2.0.0 |
||
| 873 | */ |
||
| 874 | 3 | public static function setParser(Parser\ParserInterface $parser) |
|
| 878 | |||
| 879 | /** |
||
| 880 | * Sets the locale. |
||
| 881 | * |
||
| 882 | * @param string $locale The locale to set. |
||
| 883 | * |
||
| 884 | * @return void |
||
| 885 | * |
||
| 886 | * @since 2.0.0 |
||
| 887 | */ |
||
| 888 | 279 | public static function setLocale($locale) |
|
| 892 | |||
| 893 | /** |
||
| 894 | * Gets the Translator implementation. |
||
| 895 | * |
||
| 896 | * @return Translator\AbstractTranslator |
||
| 897 | * |
||
| 898 | * @since 2.0.0 |
||
| 899 | */ |
||
| 900 | 281 | public static function getTranslator() |
|
| 909 | |||
| 910 | /** |
||
| 911 | * Sets the Strategy implementation. |
||
| 912 | * |
||
| 913 | * @param Strategy\StrategyInterface $strategy The Strategy implementation. |
||
| 914 | * |
||
| 915 | * @return void |
||
| 916 | * |
||
| 917 | * @since 2.0.0 |
||
| 918 | */ |
||
| 919 | protected function setStrategy(Strategy\StrategyInterface $strategy) |
||
| 923 | |||
| 924 | /** |
||
| 925 | * Gets the Strategy implementation. |
||
| 926 | * |
||
| 927 | * @return Strategy\StrategyInterface |
||
| 928 | * |
||
| 929 | * @since 2.0.0 |
||
| 930 | */ |
||
| 931 | 78 | protected function getStrategy() |
|
| 940 | |||
| 941 | /** |
||
| 942 | * Creates a DateTime by adding or subtacting interval. |
||
| 943 | * |
||
| 944 | * @param integer $value The value for the format. |
||
| 945 | * @param string $format The interval_spec for sprintf(), eg. 'P%sD'. |
||
| 946 | * |
||
| 947 | * @return DateTime |
||
| 948 | * |
||
| 949 | * @since 2.0.0 |
||
| 950 | */ |
||
| 951 | 131 | private function calc($value, $format) |
|
| 952 | { |
||
| 953 | 131 | $value = intval($value); |
|
| 954 | 131 | $spec = sprintf($format, abs($value)); |
|
| 955 | |||
| 956 | 131 | return $value > 0 ? $this->add(new DateInterval($spec)) : $this->sub(new DateInterval($spec)); |
|
| 957 | } |
||
| 958 | |||
| 959 | /** |
||
| 960 | * Creates a DateTime object by calling the given callable. |
||
| 961 | * |
||
| 962 | * @param callable $callable The callable with modifications of PHP DateTime object. |
||
| 963 | * |
||
| 964 | * @return DateTime |
||
| 965 | * |
||
| 966 | * @since 2.0.0 |
||
| 967 | */ |
||
| 968 | 178 | private function modify($callable) |
|
| 969 | { |
||
| 970 | 178 | $datetime = clone $this->datetime; |
|
| 971 | 178 | call_user_func_array($callable, array($datetime)); |
|
| 972 | |||
| 973 | 178 | return new static($datetime); |
|
| 974 | } |
||
| 975 | |||
| 976 | /** |
||
| 977 | * If a day has changed, sets the date on the last day of the previous month. |
||
| 978 | * |
||
| 979 | * @param DateTime $result A result of months or years addition |
||
| 980 | * |
||
| 981 | * @return DateTime |
||
| 982 | * |
||
| 983 | * @since 2.0.0 |
||
| 984 | */ |
||
| 985 | 56 | private function fixMonth(DateTime $result) |
|
| 995 | |||
| 996 | /** |
||
| 997 | * Gets the Since implementation. |
||
| 998 | * |
||
| 999 | * @return Since\SinceInterface |
||
| 1000 | * |
||
| 1001 | * @since 2.0.0 |
||
| 1002 | */ |
||
| 1003 | 143 | private static function getSince() |
|
| 1012 | |||
| 1013 | /** |
||
| 1014 | * Gets the Getter implementation. |
||
| 1015 | * |
||
| 1016 | * @return Getter\GetterInterface |
||
| 1017 | * |
||
| 1018 | * @since 2.0.0 |
||
| 1019 | */ |
||
| 1020 | 30 | private static function getGetter() |
|
| 1029 | |||
| 1030 | /** |
||
| 1031 | * Gets the Parser implementation. |
||
| 1032 | * |
||
| 1033 | * @return Parser\ParserInterface |
||
| 1034 | * |
||
| 1035 | * @since 2.0.0 |
||
| 1036 | */ |
||
| 1037 | 4 | private static function getParser() |
|
| 1046 | } |
||
| 1047 |
Let’s assume you have a class which uses late-static binding:
}
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClassto useselfinstead: