PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/station-games/vendor/cakephp/chronos/src/Traits/MagicPropertyTrait.php

https://gitlab.com/ViniciusP/project-games
PHP | 103 lines | 59 code | 15 blank | 29 comment | 3 complexity | 7bba454570135171c6002a6622519671 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  4. *
  5. * Licensed under The MIT License
  6. * Redistributions of files must retain the above copyright notice. Provides various operator methods for datetime
  7. * objects.
  8. *
  9. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  10. * @copyright Copyright (c) Brian Nesbitt <brian@nesbot.com>
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Cake\Chronos\Traits;
  15. use Cake\Chronos\ChronosInterface;
  16. use InvalidArgumentException;
  17. /**
  18. * Provides the magic methods that allow read access
  19. * to magic properties.
  20. */
  21. trait MagicPropertyTrait
  22. {
  23. /**
  24. * Get a part of the ChronosInterface object
  25. *
  26. * @param string $name The property name to read.
  27. * @return string|int|DateTimeZone The property value.
  28. * @throws InvalidArgumentException
  29. */
  30. public function __get($name)
  31. {
  32. switch (true) {
  33. case array_key_exists($name, $formats = [
  34. 'year' => 'Y',
  35. 'yearIso' => 'o',
  36. 'month' => 'n',
  37. 'day' => 'j',
  38. 'hour' => 'G',
  39. 'minute' => 'i',
  40. 'second' => 's',
  41. 'micro' => 'u',
  42. 'dayOfWeek' => 'N',
  43. 'dayOfYear' => 'z',
  44. 'weekOfYear' => 'W',
  45. 'daysInMonth' => 't',
  46. 'timestamp' => 'U',
  47. ]):
  48. return (int)$this->format($formats[$name]);
  49. case $name === 'weekOfMonth':
  50. return (int)ceil($this->day / ChronosInterface::DAYS_PER_WEEK);
  51. case $name === 'age':
  52. return (int)$this->diffInYears();
  53. case $name === 'quarter':
  54. return (int)ceil($this->month / 3);
  55. case $name === 'offset':
  56. return $this->getOffset();
  57. case $name === 'offsetHours':
  58. return $this->getOffset() / ChronosInterface::SECONDS_PER_MINUTE / ChronosInterface::MINUTES_PER_HOUR;
  59. case $name === 'dst':
  60. return $this->format('I') === '1';
  61. case $name === 'local':
  62. return $this->offset === $this->copy()->setTimezone(date_default_timezone_get())->offset;
  63. case $name === 'utc':
  64. return $this->offset === 0;
  65. case $name === 'timezone' || $name === 'tz':
  66. return $this->getTimezone();
  67. case $name === 'timezoneName' || $name === 'tzName':
  68. return $this->getTimezone()->getName();
  69. default:
  70. throw new InvalidArgumentException(sprintf("Unknown getter '%s'", $name));
  71. }
  72. }
  73. /**
  74. * Check if an attribute exists on the object
  75. *
  76. * @param string $name The property name to check.
  77. * @return bool Whether or not the property exists.
  78. */
  79. public function __isset($name)
  80. {
  81. try {
  82. $this->__get($name);
  83. } catch (InvalidArgumentException $e) {
  84. return false;
  85. }
  86. return true;
  87. }
  88. }