3 class ORMObjectPoolGenerator
extends class_generator
{
9 * undocumented function
13 public function __construct( $name, $descr ) {
15 $this->structure
= $descr;
16 $this->objectclass
= $descr[$name]['class'].self
::$model_suffix;
17 $this->classname
= 'Base'.$descr[$name]['class'].'Pool';
22 * undocumented function
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();
40 * undocumented function
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();
52 * undocumented function
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] ) );
66 $this->write( 'return false;' );
67 $this->finish_function();
71 * undocumented function
75 private function lookup_class( $class ) {
76 foreach( $this->structure
as $table => $struct ) {
77 if( $struct['class'] == $class ) {
85 * undocumented function
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();
96 * undocumented function
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
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;');
119 $set_fetcher = 'return self::all()';
121 foreach($this->structure
[$this->name
]['key'] as $i => $field) {
122 $set_fetcher .= '->reduce_by(%s,$parts[%s],"=")';
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;');
136 $this->write('return false;');
137 $this->finish_function();