ICanBoogie/DateTime 2.0.x
  • Namespace
  • Class

Namespaces

  • ICanBoogie
    • DateTime

Traits

  • Readers
  • Shared
  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 
<?php

/*
 * This file is part of the ICanBoogie package.
 *
 * (c) Olivier Laviale <olivier.laviale@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace ICanBoogie\DateTime;

use ICanBoogie\DateTime;
use ICanBoogie\ImmutableDateTime;
use ICanBoogie\MutableDateTime;
use ICanBoogie\TimeZone;

/**
 * @property-read int $timestamp Unix timestamp.
 * @property-read int $year Year.
 * @property-read int $month Month of the year.
 * @property-read int $day Day of the month.
 * @property-read int $hour Hour of the day.
 * @property-read int $minute Minute of the hour.
 * @property-read int $second Second of the minute.
 * @property-read int $quarter Quarter of the year.
 * @property-read int $week Week of the year.
 * @property-read int $weekday Day of the week.
 * @property-read int $year_day Day of the year.
 *
 * @property-read DateTime $tomorrow A new instance representing the next day. Time is reset to 00:00:00.
 * @property-read DateTime $yesterday A new instance representing the previous day. Time is reset to 00:00:00.
 * @property-read DateTime $monday A new instance representing Monday of the week. Time is reset to 00:00:00.
 * @property-read DateTime $tuesday A new instance representing Tuesday of the week. Time is reset to 00:00:00.
 * @property-read DateTime $wednesday A new instance representing Wednesday of the week. Time is reset to 00:00:00.
 * @property-read DateTime $thursday A new instance representing Thursday of the week. Time is reset to 00:00:00.
 * @property-read DateTime $friday A new instance representing Friday of the week. Time is reset to 00:00:00.
 * @property-read DateTime $saturday A new instance representing Saturday of the week. Time is reset to 00:00:00.
 * @property-read DateTime $sunday A new instance representing Sunday of the week. Time is reset to 00:00:00.
 *
 * @property-read bool $is_monday `true` if the instance represents Monday.
 * @property-read bool $is_tuesday `true` if the instance represents Tuesday.
 * @property-read bool $is_wednesday `true` if the instance represents Wednesday.
 * @property-read bool $is_thursday `true` if the instance represents Thursday.
 * @property-read bool $is_friday `true` if the instance represents Friday.
 * @property-read bool $is_saturday `true` if the instance represents Saturday.
 * @property-read bool $is_sunday `true` if the instance represents Sunday.
 * @property-read bool $is_today `true` if the instance is today.
 * @property-read bool $is_past `true` if the instance lies in the past.
 * @property-read bool $is_future `true` if the instance lies in the future.
 * @property-read bool $is_empty `true` if the instance represents an empty date such as "0000-00-00" or "0000-00-00 00:00:00".
 *
 * @property-read string $as_atom The instance formatted according to {@link ATOM}.
 * @property-read string $as_cookie The instance formatted according to {@link COOKIE}.
 * @property-read string $as_iso8601 The instance formatted according to {@link ISO8601}.
 * @property-read string $as_rfc822 The instance formatted according to {@link RFC822}.
 * @property-read string $as_rfc850 The instance formatted according to {@link RFC850}.
 * @property-read string $as_rfc1036 The instance formatted according to {@link RFC1036}.
 * @property-read string $as_rfc1123 The instance formatted according to {@link RFC1123}.
 * @property-read string $as_rfc2822 The instance formatted according to {@link RFC2822}.
 * @property-read string $as_rfc3339 The instance formatted according to {@link RFC3339}.
 * @property-read string $as_rss The instance formatted according to {@link RSS}.
 * @property-read string $as_w3c The instance formatted according to {@link W3C}.
 * @property-read string $as_db The instance formatted according to {@link DB}.
 * @property-read string $as_number The instance formatted according to {@link NUMBER}.
 * @property-read string $as_date The instance formatted according to {@link DATE}.
 * @property-read string $as_time The instance formatted according to {@link TIME}.
 *
 * @property-read TimeZone $timezone The timezone of the instance.
 * @property-read TimeZone $tz The timezone of the instance.
 * @property-read bool $is_utc `true` if the instance is in the UTC timezone.
 * @property-read bool $is_local `true` if the instance is in the local timezone.
 * @property-read bool $is_dst `true` if time occurs during Daylight Saving Time in its time zone.
 *
 * @property-read ImmutableDateTime $immutable An immutable representation of the instance.
 * @property-read MutableDateTime $mutable A mutable representation of the instance.
 */
trait Readers
{
    /**
     * @param string $modify
     *
     * @return DateTime
     */
    abstract public function modify($modify);

    /**
     * @param string $property
     *
     * @return mixed
     *
     * @throws \LogicException in attempt to obtain an undefined property.
     */
    public function __get($property)
    {
        if (strpos($property, 'as_') === 0)
        {
            return $this->{ 'format_' . $property }();
        }

        switch ($property)
        {
            case 'year':
                return (int) $this->format('Y');
            case 'month':
                return (int) $this->format('m');
            case 'day':
                return (int) $this->format('d');
            case 'hour':
                return (int) $this->format('H');
            case 'minute':
                return (int) $this->format('i');
            case 'second':
                return (int) $this->format('s');
            case 'quarter':
                return floor(($this->month - 1) / 3) + 1;
            case 'week':
                return (int) $this->format('W');
            case 'year_day':
                return (int) $this->format('z') + 1;
            case 'weekday':
                return (int) $this->format('w') ?: 7;

            case 'is_monday':
                return $this->weekday == 1;
            case 'is_tuesday':
                return $this->weekday == 2;
            case 'is_wednesday':
                return $this->weekday == 3;
            case 'is_thursday':
                return $this->weekday == 4;
            case 'is_friday':
                return $this->weekday == 5;
            case 'is_saturday':
                return $this->weekday == 6;
            case 'is_sunday':
                return $this->weekday == 7;
            case 'is_today':
                $now = new static('now', $this->tz);
                return $this->as_date === $now->as_date;
            case 'is_past':
                return $this < new static('now', $this->tz);
            case 'is_future':
                return $this > new static('now', $this->tz);
            case 'is_empty':
                return $this->year == -1 && $this->month == 11 && $this->day == 30;

            case 'utc':
            case 'local':
                $datetime = clone $this; // works for immutable and mutable
                return $datetime->setTimezone($property);
            case 'is_utc':
            case 'is_local':
                return $this->tz->$property;
        }

        $getter = "get_$property";

        if (!method_exists($this, $getter))
        {
            throw new \LogicException("Property is not defined: $property.");
        }

        return $this->{ 'get_' . $property }();
    }

    /**
     * @return int
     */
    protected function get_timestamp()
    {
        return $this->getTimestamp();
    }

    /**
     * Returns Monday of the week.
     *
     * @return ImmutableDateTime
     */
    protected function get_monday()
    {
        $datetime = clone $this;
        $day = $datetime->weekday;

        if ($day != 1)
        {
            $datetime = $datetime->modify('-' . ($day - 1) . ' day');
        }

        return $datetime->setTime(0, 0, 0);
    }

    /**
     * Returns Tuesday of the week.
     *
     * @return DateTime
     */
    protected function get_tuesday()
    {
        return $this->monday->modify('+1 day');
    }

    /**
     * Returns Wednesday of the week.
     *
     * @return DateTime
     */
    protected function get_wednesday()
    {
        return $this->monday->modify('+2 day');
    }

    /**
     * Returns Thursday of the week.
     *
     * @return DateTime
     */
    protected function get_thursday()
    {
        return $this->monday->modify('+3 day');
    }

    /**
     * Returns Friday of the week.
     *
     * @return DateTime
     */
    protected function get_friday()
    {
        return $this->monday->modify('+4 day');
    }

    /**
     * Returns Saturday of the week.
     *
     * @return DateTime
     */
    protected function get_saturday()
    {
        return $this->monday->modify('+5 day');
    }

    /**
     * Returns Sunday of the week.
     *
     * @return DateTime
     */
    protected function get_sunday()
    {
        $datetime = clone $this;
        $day = $this->weekday;

        if ($day != 7)
        {
            $datetime = $datetime->modify('+' . (7 - $day) . ' day');
        }

        return $datetime->setTime(0, 0, 0);
    }

    /**
     * @return DateTime
     */
    protected function get_tomorrow()
    {
        return $this
            ->modify('+1 day')
            ->setTime(0, 0, 0);
    }

    /**
     * @return DateTime
     */
    protected function get_yesterday()
    {
        return $this
            ->modify('-1 day')
            ->setTime(0, 0, 0);
    }

    /**
     * @return DateTime
     */
    protected function get_mutable()
    {
        return MutableDateTime::from($this);
    }

    /**
     * @return DateTime
     */
    protected function get_immutable()
    {
        return ImmutableDateTime::from($this);
    }

    /**
     * @return bool
     */
    protected function get_is_dst()
    {
        $timestamp = $this->timestamp;
        $transitions = $this->timezone->getTransitions($timestamp, $timestamp);

        return $transitions[0]['isdst'];
    }

    /**
     * @return TimeZone
     */
    protected function get_timezone()
    {
        return TimeZone::from($this->getTimezone());
    }

    /**
     * @return TimeZone
     */
    protected function get_tz()
    {
        return $this->get_timezone();
    }
}
ICanBoogie/DateTime 2.0.x API documentation generated by ApiGen