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\CLDR;
13
14 use ICanBoogie\PropertyNotDefined;
15
16 /**
17 * A localized date time.
18 *
19 * <pre>
20 * <?php
21 *
22 * namespace ICanBoogie\CLDR;
23 *
24 * $ldt = new LocalizedDateTime(new \DateTime('2013-11-04 20:21:22 UTC'), $repository->locales['fr']);
25 *
26 * echo $ldt->as_full; // lundi 4 novembre 2013 20:21:22 UTC
27 * # or
28 * echo $ldt->format_as_full(); // lundi 4 novembre 2013 20:21:22 UTC
29 *
30 * echo $ldt->as_long; // 4 novembre 2013 20:21:22 UTC
31 * echo $ldt->as_medium; // 4 nov. 2013 20:21:22
32 * echo $ldt->as_short; // 04/11/2013 20:21
33 * </pre>
34 *
35 * @property-read string $as_full
36 * @property-read string $as_long
37 * @property-read string $as_medium
38 * @property-read string $as_short
39 *
40 * @method string format_as_full() format_as_full() Formats the instance according to the `full` datetime pattern.
41 * @method string format_as_long() format_as_long() Formats the instance according to the `long` datetime pattern.
42 * @method string format_as_medium() format_as_medium() Formats the instance according to the `medium` datetime pattern.
43 * @method string format_as_short() format_as_short() Formats the instance according to the `short` datetime pattern.
44 */
45 class LocalizedDateTime extends LocalizedObject
46 {
47 /**
48 * Returns the formatter.
49 *
50 * @return DateTimeFormatter
51 */
52 protected function get_formatter()
53 {
54 return $this->locale->calendar->datetime_formatter;
55 }
56
57 public function __get($property)
58 {
59 switch ($property)
60 {
61 case 'as_full':
62 case 'as_long':
63 case 'as_medium':
64 case 'as_short':
65 return call_user_func(array($this, 'format_' . $property));
66 }
67
68 try
69 {
70 return parent::__get($property);
71 }
72 catch (PropertyNotDefined $e)
73 {
74 return $this->target->$property;
75 }
76 }
77
78 public function __set($property, $value)
79 {
80 $this->target->$property = $value;
81 }
82
83 public function __call($method, $arguments)
84 {
85 switch ($method)
86 {
87 case 'format_as_full':
88 case 'format_as_long':
89 case 'format_as_medium':
90 case 'format_as_short':
91 return $this->format(substr($method, 10));
92 }
93
94 return call_user_func_array(array($this->target, $method), $arguments);
95 }
96
97 public function __toString()
98 {
99 return (string) $this->target;
100 }
101
102 public function format($pattern=null)
103 {
104 return $this->formatter->format($this->target, $pattern);
105 }
106 }