Removed dep on API
[ninja.git] / src / op5 / ninja_sdk / orm / ORMObjectPoolGenerator.php
blob3657fab652d92b4f4b54293aeb77d72dd569358a
1 <?php
3 class ORMObjectPoolGenerator extends class_generator {
4 private $name;
5 private $structure;
6 private $objectclass;
8 /**
9 * undocumented function
11 * @return void
12 **/
13 public function __construct( $name, $descr ) {
14 $this->name = $name;
15 $this->structure = $descr;
16 $this->objectclass = $descr[$name]['class'].self::$model_suffix;
17 $this->classname = 'Base'.$descr[$name]['class'].'Pool';
18 $this->set_model();
21 /**
22 * undocumented function
24 * @return void
25 **/
26 public function generate($skip_generated_note = false) {
27 parent::generate($skip_generated_note);
28 $this->init_class( 'ObjectPool', array('abstract') );
29 $this->variable('table',$this->name,'protected');
30 $this->generate_pool();
31 $this->generate_table_for_field();
32 $this->generate_setbuilder_all();
33 $this->generate_setbuilder_none();
34 $this->generate_set_by_key();
35 $this->generate_fetch_by_key();
36 $this->finish_class();
39 /**
40 * undocumented function
42 * @return void
43 **/
44 private function generate_pool() {
45 $this->init_function( 'pool', array('name'), 'static', array('name' => false));
46 $this->write( 'if( $name === false ) return new static();');
47 $this->write( 'return parent::pool($name);' );
48 $this->finish_function();
51 /**
52 * undocumented function
54 * @return void
55 **/
56 private function generate_table_for_field() {
57 $this->init_function( 'get_table_for_field', array('name') );
58 $this->write( 'switch($name) {' );
59 foreach( $this->structure[$this->name]['structure'] as $field => $type ) {
60 if( is_array( $type ) ) {
61 $this->write( 'case %s:', $field );
62 $this->write( 'return %s;', $this->lookup_class( $type[0] ) );
65 $this->write( '}' );
66 $this->write( 'return false;' );
67 $this->finish_function();
70 /**
71 * undocumented function
73 * @return void
74 **/
75 private function lookup_class( $class ) {
76 foreach( $this->structure as $table => $struct ) {
77 if( $struct['class'] == $class ) {
78 return $table;
81 return false;
84 /**
85 * undocumented function
87 * @return void
88 **/
89 private function generate_setbuilder_all() {
90 $this->init_function( 'all', array(), 'static' );
91 $this->write('return new '.$this->structure[$this->name]['class'].'Set'.self::$model_suffix.'(new LivestatusFilterAnd());');
92 $this->finish_function();
95 /**
96 * undocumented function
98 * @return void
99 **/
100 private function generate_setbuilder_none() {
101 $this->init_function( 'none', array(), 'static' );
102 $this->write('return new '.$this->structure[$this->name]['class'].'Set'.self::$model_suffix.'(new LivestatusFilterOr());');
103 $this->finish_function();
107 * undocumented function
109 * @return void
111 private function generate_set_by_key() {
112 $this->init_function( 'set_by_key', array('key'), 'static' );
114 $this->write('$parts = explode(";",$key);');
115 $this->write('if(count($parts) != %s) {', count($this->structure[$this->name]['key']));
116 $this->write( 'return false;');
117 $this->write('}');
119 $set_fetcher = 'return self::all()';
120 $args = array();
121 foreach($this->structure[$this->name]['key'] as $i => $field) {
122 $set_fetcher .= '->reduce_by(%s,$parts[%s],"=")';
123 $args[] = $field;
124 $args[] = $i;
126 array_unshift($args,$set_fetcher.';');
127 call_user_func_array(array($this,'write'), $args);
128 $this->finish_function();
131 private function generate_fetch_by_key() {
132 $this->init_function( 'fetch_by_key', array('key'), 'static' );
133 $this->write('foreach(self::set_by_key($key) as $obj) {');
134 $this->write( 'return $obj;');
135 $this->write('}');
136 $this->write('return false;');
137 $this->finish_function();