1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace Icybee\Modules\Editor;
13
14 use ICanBoogie\PropertyNotDefined;
15
16 17 18
19 class Collection implements \ArrayAccess, \IteratorAggregate
20 {
21 22 23 24 25 26 27 28 29 30
31 static public function prototype_get_editors(\ICanBoogie\Core $core)
32 {
33 $definitions = (array) $core->configs->synthesize('editors', 'merge');
34 $collection = new static($definitions);
35
36 new Collection\AlterEvent($collection);
37
38 return $collection;
39 }
40
41 protected $definitions;
42 protected $editors;
43
44 45 46
47 public function __construct(array $definitions=array())
48 {
49 $this->definitions = $definitions;
50 }
51
52 53 54
55 public function offsetExists($offset)
56 {
57 if ($offset == 'moo')
58 {
59 $offset = 'rte';
60 }
61 else if ($offset == 'adjustimage')
62 {
63 $offset = 'image';
64 }
65
66 return isset($this->definitions[$offset]);
67 }
68
69 70 71 72 73
74 public function offsetGet($offset)
75 {
76 if ($offset == 'moo')
77 {
78 $offset = 'rte';
79 }
80 else if ($offset == 'adjustimage')
81 {
82 $offset = 'image';
83 }
84
85 if (isset($this->editors[$offset]))
86 {
87 return $this->editors[$offset];
88 }
89
90 if (!$this->offsetExists($offset))
91 {
92 throw new EditorNotDefined($offset);
93 }
94
95 $class = $this->definitions[$offset];
96 $editor = new $class;
97
98 return $this->editors[$offset] = $editor;
99 }
100
101 102 103 104 105 106
107 public function offsetSet($id, $value)
108 {
109 if (isset($this->editors[$id]))
110 {
111 throw new EditorAlreadyInstantiated($id);
112 }
113
114 $this->definitions[$id] = $value;
115 }
116
117 118 119 120 121
122 public function offsetUnset($id)
123 {
124 if (isset($this->editors[$id]))
125 {
126 throw new EditorAlreadyInstantiated($id);
127 }
128
129 unset($this->definitions[$id]);
130 }
131
132 133 134
135 public function getIterator()
136 {
137 return new \ArrayIterator($this->definitions);
138 }
139 }
140
141 142 143 144 145
146 class EditorNotDefined extends \ICanBoogie\OffsetNotDefined
147 {
148 private $editor_id;
149
150 public function __construct($editor_id, $code=500, \Exception $previous=null)
151 {
152 $this->editor_id = $editor_id;
153
154 parent::__construct("Editor not defined: $editor_id.", $code, $previous);
155 }
156
157 public function __get($property)
158 {
159 if ($property == 'editor_id')
160 {
161 return $this->editor_id;
162 }
163
164 throw PropertyNotDefined(array($property, $this));
165 }
166 }
167
168 169 170 171 172
173 class EditorAlreadyInstantiated extends \RuntimeException
174 {
175 private $editor_id;
176
177 public function __construct($editor_id, $code=500, \Exception $previous=null)
178 {
179 $this->editor_id = $editor_id;
180
181 parent::__construct("An editor has already been instantiated: $editor_id.", $code, $previous);
182 }
183
184 public function __get($property)
185 {
186 if ($property == 'editor_id')
187 {
188 return $this->editor_id;
189 }
190
191 throw PropertyNotDefined(array($property, $this));
192 }
193 }
194
195 namespace Icybee\Modules\Editor\Collection;
196
197 198 199
200 class AlterEvent extends \ICanBoogie\Event
201 {
202 203 204 205 206
207 public function __construct(\Icybee\Modules\Editor\Collection $target)
208 {
209 parent::__construct($target, 'alter');
210 }
211 }