1 <?php
2
3 /*
4 * This file is part of the ICanBoogie package.
5 *
6 * (c) Olivier Laviale <olivier.laviale@gmail.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace ICanBoogie\ActiveRecord;
13
14 use ICanBoogie\DateTime;
15
16 /**
17 * Provides support for datetime properties.
18 */
19 class DateTimePropertySupport
20 {
21 /**
22 * Sets the a datetime is a property.
23 *
24 * @param mixed $property Reference to the property to set.
25 * @param mixed $datetime Date and time.
26 */
27 static public function datetime_set(&$property, $datetime)
28 {
29 $property = $datetime;
30 }
31
32 /**
33 * Returns the {@link DateTime} instance of a property.
34 *
35 * @param mixed $property Reference to the property to return.
36 *
37 * @return DateTime The function always return a {@link DateTime} instance.
38 */
39 static public function datetime_get(&$property)
40 {
41 if ($property instanceof DateTime)
42 {
43 return $property;
44 }
45
46 return $property = $property === null ? DateTime::none() : new DateTime($property, 'utc');
47 }
48 }
49
50 /**
51 * Implements a `datetime` property.
52 */
53 trait DateTimeProperty
54 {
55 /**
56 * The date and time at which the record was created.
57 *
58 * @var string
59 */
60 private $datetime;
61
62 /**
63 * Returns the date and time at which the record was created.
64 *
65 * @return \ICanBoogie\DateTime
66 */
67 protected function get_datetime()
68 {
69 return DateTimePropertySupport::datetime_get($this->datetime);
70 }
71
72 /**
73 * Sets the date and time at which the record was created.
74 *
75 * @param mixed $value
76 */
77 protected function set_datetime($datetime)
78 {
79 DateTimePropertySupport::datetime_set($this->datetime, $datetime);
80 }
81 }