Removed dep on API
[ninja.git] / src / op5 / ninja_sdk / orm / ORMObjectGenerator.php
blob15cdbbeca10829f44989ef3de23cb55562f42898
1 <?php
3 class ORMObjectGenerator extends class_generator {
5 private $name;
6 private $structure;
7 private $associations;
9 /**
10 * Construct
12 * @return void
13 **/
14 public function __construct( $name, $structure ) {
15 $this->name = $name;
16 $this->structure = $structure[$name];
17 $this->key = $this->structure['key'];
18 $this->classname = 'Base'.$this->structure['class'];
20 $this->associations = array();
22 foreach( $structure as $table => $tbl_struct ) {
23 foreach( $tbl_struct['structure'] as $name => $type ) {
24 if( is_array( $type ) ) {
25 if( $type[0] == $this->structure['class'] ) {
26 $this->associations[] = array(
27 $table,
28 $tbl_struct['class'],
29 substr( $type[1], 0, -1 ) // Drop last _
36 $this->set_model();
39 /**
40 * Generate
42 * @return void
43 **/
44 public function generate($skip_generated_note = false) {
45 parent::generate($skip_generated_note);
46 $this->init_class( 'Object', array('abstract') );
47 $this->variable( '_table', $this->name, 'protected' );
48 $this->write();
50 /* Storage */
51 foreach( $this->structure['structure'] as $field => $type ) {
52 if( is_array($type) ) {
53 $this->{"storage_object"}( $field, $type );
54 } else {
55 $this->{"storage_$type"}( $field );
59 $this->generate_construct();
60 $this->generate_get_key();
62 /* Getters and setters */
63 foreach( $this->structure['structure'] as $field => $type ) {
64 if( is_array($type) ) {
65 $this->{"get_object"}( $field, $type );
66 } else {
67 $this->{"get_$type"}( $field );
71 foreach( $this->associations as $assoc ) {
72 $this->generate_association_get_set( $assoc[0], $assoc[1], $assoc[2] );
75 $this->finish_class();
78 /**
79 * Generates a class construct
81 * @return void
82 **/
83 private function generate_construct() {
84 $this->init_function( "__construct", array( 'values', 'prefix', 'export' ) );
85 $this->write( '$this->export = array();' );
86 $this->write( '$subobj_export = array();' );
87 $this->write( 'if($export === false) $export = array();'); //FIXME
88 $this->write( 'foreach( $export as $expcol ) {');
89 $this->write( '$parts = explode(".", $expcol, 2);');
90 $this->write( 'if(count($parts) == 2) {');
91 $this->write( 'if(!isset($subobj_export[$parts[0]])) {');
92 $this->write( '$subobj_export[$parts[0]] = array();');
93 $this->write( '}');
94 $this->write( '$subobj_export[$parts[0]][] = $parts[1];');
95 $this->write( '$this->export[] = $parts[0];');
96 $this->write( '} else {');
97 $this->write( '$this->export[] = $parts[0];');
98 $this->write( '}');
99 $this->write( '}');
100 $this->comment('If object fields exists, make sure the object only exists in the export array once');
101 $this->write( '$this->export = array_unique($this->export);');
102 foreach( $this->structure['structure'] as $field => $type ) {
103 $backend_name = $field;
104 if(isset($this->structure['rename']) && isset($this->structure['rename'][$field])) {
105 $backend_name = $this->structure['rename'][$field];
107 if( is_array($type) ) {
108 $this->{"fetch_object"}( $field, $backend_name, $type );
109 } else {
110 $this->{"fetch_$type"}( $field, $backend_name );
113 $this->write( 'parent::__construct( $values, $prefix, $export ); ');
114 $this->finish_function();
118 * Generate association get set
120 * @param $table string
121 * @param $class string
122 * @param field string
123 * @return void
125 private function generate_association_get_set($table, $class, $field) {
126 $this->init_function('get_'.$table.'_set');
127 $this->write('$set = '.$class.'Pool'.self::$model_suffix.'::all();');
128 foreach( $this->key as $key_field ) {
129 $this->write('$set = $set->reduce_by(%s,$this->get_'.$key_field.'(),"=");', $field.'.'.$key_field);
131 $this->write('return $set;');
132 $this->finish_function();
136 * Generate get key
138 * @return void
140 private function generate_get_key() {
141 $this->init_function("get_key");
142 $matchline = '$key = $this%s;';
143 $got = false;
144 foreach( $this->key as $keypart ) {
145 // Build getter sequence
146 $call = "";
147 foreach( explode('.',$keypart) as $part ) {
148 $call .= "->get_$part()";
151 // Use sprintf instead of embedded in write. write escapes
152 $this->write( sprintf( $matchline, $call ) );
153 $got = true;
154 $matchline = '$key .= ";".$this%s;';
156 if( $got ) {
157 $this->write('return $key;');
158 } else {
159 $this->write('return false;');
161 $this->finish_function();
165 * Writes a private variable to class
167 * @param $name string
168 * @param $type string Unused?
169 * @return void
171 private function storage_object( $name, $type ) {
172 $this->write( "private \$$name = false;" );
176 * Writes an object fetcher
178 * @param $name string
179 * @param $type string
180 * @return void
182 private function fetch_object( $name, $backend_name, $type ) {
183 list( $class, $prefix ) = $type;
184 // Livestatus handles only one level of prefixes... might change in future? (for example comments: service.host.name should be host.name
185 $this->write( "\$this->$name = new $class".self::$model_suffix."( \$values, %s, isset(\$subobj_export[%s]) ? \$subobj_export[%s] : array() );", $prefix, $backend_name, $name );
189 * Writes function getting object named $name
191 * @param $name string
192 * @param $type string Unused?
193 * @return void
195 private function get_object( $name, $type ) {
196 $this->init_function( "get_$name" );
197 $this->write( "return \$this->$name;" );
198 $this->finish_function();
202 * undocumented function
204 * @return void
206 private function storage_string( $name ) {
207 $this->write( "private \$$name = false;" );
211 * undocumented function
213 * @return void
215 private function fetch_string( $name, $backend_name ) {
216 $this->write( "if(array_key_exists(\$prefix.'$backend_name', \$values)) { ");
217 $this->write( "\$this->$name = (string)\$values[\$prefix.'$backend_name'];" );
218 $this->write( "}" );
222 * undocumented function
224 * @return void
226 private function get_string( $name ) {
227 $this->init_function( "get_$name" );
228 $this->write( "return \$this->$name;" );
229 $this->finish_function();
232 /* Time FIXME */
235 * undocumented function
237 * @return void
239 private function storage_time( $name ) {
240 $this->write( "private \$$name = false;" );
244 * undocumented function
246 * @return void
248 private function fetch_time( $name, $backend_name ) {
249 $this->write( "if(array_key_exists(\$prefix.'$backend_name', \$values)) { ");
250 $this->write( "\$this->$name = \$values[\$prefix.'$backend_name'];" );
251 $this->write( "}" );
255 * undocumented function
257 * @return void
259 private function get_time( $name ) {
260 $this->init_function( "get_$name" );
261 $this->write( "return \$this->$name;" );
262 $this->finish_function();
265 /* Int */
268 * undocumented function
270 * @return void
272 private function storage_int( $name ) {
273 $this->write( "private \$$name = false;" );
277 * undocumented function
279 * @return void
281 private function fetch_int( $name, $backend_name ) {
282 $this->write( "if(array_key_exists(\$prefix.'$backend_name', \$values)) {" );
283 $this->write( "\$this->$name = intval( \$values[\$prefix.'$backend_name'] );" );
284 $this->write( "}" );
288 * undocumented function
290 * @return void
292 private function get_int( $name ) {
293 $this->init_function( "get_$name" );
294 $this->write( "return \$this->$name;" );
295 $this->finish_function();
298 /* Float */
301 * undocumented function
303 * @return void
305 private function storage_float( $name ) {
306 $this->write( "private \$$name = false;" );
310 * undocumented function
312 * @return void
314 private function fetch_float( $name, $backend_name ) {
315 $this->write( "if(array_key_exists(\$prefix.'$backend_name', \$values)) {" );
316 $this->write( "\$this->$name = floatval( \$values[\$prefix.'$backend_name'] );" );
317 $this->write( "}" );
321 * undocumented function
323 * @return void
325 private function get_float( $name ) {
326 $this->init_function( "get_$name" );
327 $this->write( "return \$this->$name;" );
328 $this->finish_function();
331 /* List */
334 * undocumented function
336 * @return void
338 private function storage_list( $name ) {
339 $this->write( "private \$$name = false;" );
343 * undocumented function
345 * @return void
347 private function fetch_list( $name, $backend_name ) {
348 $this->write( "if(array_key_exists(\$prefix.'$backend_name', \$values)) {" );
349 $this->write( "\$this->$name = \$values[\$prefix.'$backend_name'];" );
350 $this->write( "}" );
354 * undocumented function
356 * @return void
358 private function get_list( $name ) {
359 $this->init_function( "get_$name" );
360 $this->write( "return \$this->$name;" );
361 $this->finish_function();
364 /* Dict */
367 * undocumented function
369 * @return void
371 private function storage_dict( $name ) {
372 $this->write( "private \$$name = false;" );
376 * undocumented function
378 * @return void
380 private function fetch_dict( $name, $backend_name ) {
381 $this->write( "if(array_key_exists(\$prefix.'$backend_name', \$values)) {" );
382 $this->write( "\$this->$name = \$values[\$prefix.'$backend_name'];" );
383 $this->write( "}" );
387 * undocumented function
389 * @return void
391 private function get_dict( $name ) {
392 $this->init_function( "get_$name" );
393 $this->write( "return \$this->$name;" );
394 $this->finish_function();