Skip to content

Commit ca14f10

Browse files
author
Carlo Pietrobattista
committed
object relational mapping component
1 parent 6645d1b commit ca14f10

File tree

1 file changed

+27
-57
lines changed

1 file changed

+27
-57
lines changed

PtcMapper.php

Lines changed: 27 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,12 @@ public function delete( )
6262
{
6363
$storage = static::$_storage[ get_called_class( ) ];
6464
static::_fireEvent( 'deleting' , array(
65-
&$this->_fields[ $storage[ 'uniqueKey' ] ] , &$this->_fields ) );
65+
&$this->_fields[ static::$_uniqueKey ] , &$this->_fields ) );
6666
$result = static::_getQB( )->table( $storage[ 'table' ] )
67-
->delete( $this->_fields[ $storage[ 'uniqueKey' ] ] )
67+
->delete( $this->_fields[ static::$_uniqueKey ] )
6868
->run( );
6969
static::_fireEvent( 'deleted' ,
70-
array( &$this->_fields[ $storage[ 'uniqueKey' ] ] ,
71-
&$this->_fields , &$result ) );
70+
array( &$this->_fields[ static::$_uniqueKey ] , &$this->_fields , &$result ) );
7271
//$this->reset( ); // reset fields
7372
return $result;
7473
}
@@ -87,13 +86,12 @@ public function save( )
8786
static::_mapFields( );
8887
$values = $this->_fields;
8988
static::_fireEvent( 'saving' , array( &$values ) );
90-
if ( array_key_exists( $storage[ 'uniqueKey' ] , $this->_fields ) ) // update record
89+
if ( array_key_exists( static::$_uniqueKey , $this->_fields ) ) // update record
9190
{
9291
static::_fireEvent( 'updating' , array( &$values ) );
93-
unset( $values[ $storage[ 'uniqueKey' ] ] );
92+
unset( $values[ static::$_uniqueKey ] );
9493
$result = static::_getQB( )->table( $storage[ 'table' ] )
95-
->update( $values ,
96-
$this->_fields[ $storage[ 'uniqueKey' ] ] )
94+
->update( $values , $this->_fields[ static::$_uniqueKey ] )
9795
->run( );
9896
static::_fireEvent( 'updated' , array( &$values , &$result ) );
9997
}
@@ -104,7 +102,7 @@ public function save( )
104102
->insert( $this->_fields )->run( );
105103
static::_fireEvent( 'inserted' , array( &$values , &$result ) );
106104
}
107-
static::_fireEvent( 'saved' , array( &$values , &$result ) );
105+
static::_fireEvent( 'saved' , array( &$values , $result ) );
108106
//$this->reset( ); // reset fields
109107
return $result;
110108
}
@@ -170,7 +168,7 @@ public static function getColumns( )
170168
*/
171169
public static function observe( $class = null )
172170
{
173-
if ( !class_exists( $events_class = static::_getProperty( 'eventClass' ) ) )
171+
if ( !class_exists( $events_class = static::$_eventClass ) )
174172
{
175173
trigger_error( $events_class . ' NOT FOUND!' , E_USER_ERROR );
176174
return false;
@@ -214,8 +212,7 @@ public static function __callStatic( $method , $args )
214212
{
215213
$meth = explode( 'get_' , $method );
216214
if ( !static::_checkColumn( $meth[ 1 ] ) ){ return false; }
217-
$column = ( !array_key_exists( 1 , $args ) ) ?
218-
static::$_storage[ $class ][ 'uniqueKey' ] : $args[ 0 ];
215+
$column = ( !array_key_exists( 1 , $args ) ) ? static::$_uniqueKey : $args[ 0 ];
219216
$value = ( !array_key_exists( 1 , $args ) ) ? $args[ 0 ] : $args[ 1 ];
220217
return static::_getQB( )->table( static::$_storage[ $class ][ 'table' ] )
221218
->where( $column , '=' , $value )
@@ -227,7 +224,7 @@ public static function __callStatic( $method , $args )
227224
if ( !static::_checkColumn( $meth[ 1 ] ) ){ return false; }
228225
static::_fireEvent( array( 'saving' , 'updating' ) , array( &$meth , &$args ) );
229226
$result = static::_getQB( )->table( static::$_storage[ $class ][ 'table' ] )
230-
->where( static::$_storage[ $class ][ 'uniqueKey' ] , '=' , $args[ 1 ] )
227+
->where( static::$_uniqueKey , '=' , $args[ 1 ] )
231228
->update( array( $meth[ 1 ] => $args[ 0 ] ) )
232229
->run( );
233230
static::_fireEvent( array( 'updated' , 'saved' ) , array( &$meth , &$args , &$result ) );
@@ -303,14 +300,14 @@ protected static function _checkColumn( $column )
303300
protected static function _fireEvent( $event , $data )
304301
{
305302
$event = ( is_array( $event ) ) ? $event : array( $event );
306-
$events_class = static::_getProperty( 'eventClass' );
303+
$event_class = static::$_eventClass;
307304
if ( array_key_exists( $class = get_called_class( ) , static::$_observers ) )
308305
{
309306
foreach ( static::$_observers[ $class ] as $k => $v )
310307
{
311308
foreach ( $event as $ev )
312309
{
313-
if ( $v === $ev ){ $events_class::fire( $k , $data ); }
310+
if ( $v === $ev ){ $event_class::fire( $k , $data ); }
314311
}
315312
}
316313
}
@@ -320,9 +317,8 @@ protected static function _fireEvent( $event , $data )
320317
*/
321318
protected static function _getQB( )
322319
{
323-
$connectionManager = static::_getProperty( 'connectionManager' );
324-
$connectionName = static::_getProperty( 'connectionName' );
325-
return call_user_func( $connectionManager . '::getQB' , $connectionName );
320+
return call_user_func( static::$_connectionManager . '::getQB' ,
321+
static::$_connectionName );
326322
}
327323
/**
328324
*
@@ -332,20 +328,22 @@ protected static function _initialize( )
332328
$db = static::_getQB( );
333329
if ( !array_key_exists( $class = get_called_class( ) , static::$_storage ) )
334330
{
335-
$arr = array( );
336-
$arr[ 'uniqueKey' ] = static::_getProperty( 'uniqueKey' );
337-
$arr[ 'map' ] = static::_getProperty( 'map' );
338-
$arr[ 'table' ] = static::_getProperty( 'table' );
339-
$db->run( 'SHOW TABLES LIKE ?' , array( $arr[ 'table' ] ) );
331+
static::$_storage[ $class ] = array( );
332+
if ( static::$_table ){ static::$_storage[ $class ][ 'table' ] = static::$_table; }
333+
else
334+
{
335+
static::$_storage[ $class ][ 'table' ] = strpos( $class , '\\' ) ?
336+
strtolower( end( explode( '\\' . $class ) ) ) : strtolower( $class );
337+
}
338+
$db->run( 'SHOW TABLES LIKE ?' , array( static::$_storage[ $class ][ 'table' ] ) );
340339
if ( !$db->countRows( ) )
341340
{
342-
trigger_error( $arr[ 'table' ] .
343-
'does not exists, quitting now!' , E_USER_ERROR );
341+
trigger_error( 'Table ' . static::$_storage[ $class ][ 'table' ] .
342+
' does not exists, quitting now!' , E_USER_ERROR );
344343
return false;
345344
}
346-
static::$_storage[ $class ] = $arr;
347345
static::$_storage[ $class ][ 'columns' ] = static::getColumns( );
348-
346+
if ( method_exists( $class , 'boot' ) ){ static::boot( ); }
349347
}
350348
return $class;
351349
}
@@ -354,10 +352,9 @@ protected static function _initialize( )
354352
*/
355353
protected function _mapFields( )
356354
{
357-
$map = static::$_storage[ get_called_class( ) ][ 'map' ];
358-
if ( !empty( $map ) )
355+
if ( !empty( static::$_map ) )
359356
{
360-
foreach ( $map as $k => $v )
357+
foreach ( static::$_map as $k => $v )
361358
{
362359
if ( array_key_exists( $v , $this->_fields ) )
363360
{
@@ -367,31 +364,4 @@ protected function _mapFields( )
367364
}
368365
}
369366
}
370-
/**
371-
*
372-
*/
373-
protected static function _getProperty( $property , $found = false )
374-
{
375-
switch( $property )
376-
{
377-
case 'table' :
378-
if ( static::$_table ){ return static::$_table; }
379-
if ( strpos( $class , '\\' ) )
380-
{
381-
$class = end( explode( '\\' . $class ) );
382-
}
383-
return strtolower( $class );
384-
break;
385-
case 'map' : return static::$_map;
386-
break;
387-
case 'uniqueKey' : return static::$_uniqueKey;
388-
break;
389-
case 'connectionManager' : return static::$_connectionManager;
390-
break;
391-
case 'connectionName' : return static::$_connectionName;
392-
break;
393-
case 'eventClass' : return static::$_eventClass;
394-
default : return null;
395-
}
396-
}
397367
}

0 commit comments

Comments
 (0)