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 * Representation of a localized object.
18 *
19 * @property-read mixed $target The object to localize.
20 * @property-read Locale $locale The locale used by the formatter.
21 * @property-read mixed $formatter The formatter used to format the target object.
22 */
23 abstract class LocalizedObject
24 {
25 static public function from($source, Locale $locale, array $options=array())
26 {
27 return new static($source, $locale, $options);
28 }
29
30 /**
31 * The object to localize.
32 *
33 * @var mixed
34 */
35 protected $target;
36
37 /**
38 * The locale used by the formatter.
39 *
40 * @var Locale
41 */
42 protected $locale;
43
44 /**
45 * Options.
46 *
47 * @var array
48 */
49 protected $options;
50
51 /**
52 * Initializes the {@link $target], {@link $locale}, and {@link $options} perperties.
53 *
54 * @param mixed $target The object to localize.
55 * @param Locale $locale The locale used by the formatter.
56 * @param array $options Some options.
57 */
58 public function __construct($target, Locale $locale, array $options=array())
59 {
60 $this->target = $target;
61 $this->locale = $locale;
62 $this->options = $options;
63 }
64
65 /**
66 * The formatter used ot format the target object.
67 *
68 * @var mixed
69 */
70 private $formatter;
71
72 /**
73 * Support for the {@link $target}, {@link $locale}, and {@link $formatter} properties.
74 *
75 * @param string $property
76 *
77 * @throws PropertyNotDefined in attempt to get a property that is not supported.
78 *
79 * @return mixed
80 */
81 public function __get($property)
82 {
83 switch ($property)
84 {
85 case 'target':
86
87 return $this->target;
88
89 case 'locale':
90
91 return $this->locale;
92
93 case 'formatter':
94
95 if (!$this->formatter)
96 {
97 $this->formatter = $this->get_formatter();
98 }
99
100 return $this->formatter;
101 }
102
103 throw new PropertyNotDefined(array($property, $this));
104 }
105
106 /**
107 * Returns the formatter to use to format the target object.
108 *
109 * @return mixed
110 */
111 abstract protected function get_formatter();
112 }