Skip to content

Instantly share code, notes, and snippets.

@curthard89
Created January 3, 2012 11:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save curthard89/1554502 to your computer and use it in GitHub Desktop.
Save curthard89/1554502 to your computer and use it in GitHub Desktop.
<?php
class lib_coredata_predicate extends lib_coredata_properties
{
/**
*
*/
const LIB_COREDATA_FILLER_ATTRIBUTE = '%@';
/**
* @var lib_coredata_entity $entity
*/
public $entity = null;
/**
* @var string $format
*/
public $format = null;
/**
* @var string $predicate_format
*/
public $predicate_format = null;
/**
* @var bool $error
*/
public $error = false;
/**
* @var array() $vars
*/
private $vars = array();
/**
* @var array
*/
public $requested_fields = array();
/**
* @var null|lib_coredata_entity
*/
public $child_entity = NULL;
/**
* @var bool
*/
public $debug = FALSE;
/**
*
*/
public function __construct()
{
$args = ( is_array( func_get_arg( 0 ) ) ? func_get_arg( 0 ) : func_get_args() );
$format = $args[0];
$args = array_slice( $args, 1, sizeof( $args ) - 1 );
$original_args = $args;
$i = 0;
for( $pos = 0; preg_match( '#(?<!\\\)(\%(\w|\@))#siU', $format, $match, PREG_OFFSET_CAPTURE, $pos ); )
{
$type = $match[2][0];
$args[$i] = mysql_real_escape_string( $args[$i] );
$format_type = 'string';
if( ! preg_match( '#(b|c|d|e|E|u|f|F|o)#', $type ) )
{
if( preg_match( '#^([0-9]+)$#', $type, $matches ) )
{
if( is_numeric( $original_args[$type] ) )
{
$args[$i] = $original_args[$type];
} else if( preg_match( '#([\'\'\"\"])#', $original_args[$type] ) ) {
$args[$i] = $original_args[$type];
} else {
$args[$i] = "'".$original_args[$type]."'";
}
} else {
$args[$i] = "'".$original_args[$i]."'";
}
} else {
$format_type = 'int';
}
$arg_pos = $match[0][1];
$arg_len = strlen( $match[0][0] );
$this->vars[] = $args[$i];
$temp_e = self::LIB_COREDATA_FILLER_ATTRIBUTE;
$format = substr_replace( $format, $temp_e, $arg_pos, $arg_len );
$pos = $arg_pos + strlen( $temp_e );
$i++;
}
$this->set_format( preg_replace( '#\\\%#i', '%', $format ) );
}
/**
* @param lib_coredata_entity $entity
* @param bool $flag
* @return lib_coredata_predicate
*/
public function set_entity( lib_coredata_entity $entity, $flag = TRUE )
{
$this->entity = $entity;
if( $flag )
{
$this->parse();
}
return $this;
}
/**
* @param lib_coredata_entity $entity
* @return lib_coredata_predicate
*/
public function set_child_entity( lib_coredata_entity $entity )
{
$this->child_entity = $entity;
return $this;
}
/**
* @return lib_coredata_entity|null
*/
public function entity()
{
return $this->entity;
}
/**
* @param string $format
* @return void
*/
private function set_format( $format )
{
$this->format = $format;
}
/**
* @return null|string
*/
private function format()
{
return $this->format;
}
/**
* @param string $format
* @return void
*/
private function set_predicate_format( $format )
{
$this->predicate_format = $format;
}
/**
* @return null|string
*/
public function predicate_format()
{
return $this->predicate_format;
}
/**
* @param bool $flag
* @return void
*/
public function set_error( $flag )
{
$this->error = $flag;
}
/**
* @return bool
*/
public function error()
{
return $this->error;
}
/**
* @return void
*/
public function parse()
{
$this->set_error( false );
if( ! $this->entity() )
{
$this->set_predicate_format( NULL );
return;
}
$exp = $this->format();
$length = strlen( $exp );
$in_quote = FALSE;
$mut_string = NULL;
for( $i = 0; $i < $length; $i++ )
{
$char = $exp[$i];
$replace = NULL;
if( preg_match( '#[\'|\"]#', $char ) )
{
$in_quote = ( $in_quote ? FALSE : TRUE );
}
if( $in_quote )
{
continue;
}
$last = FALSE;
if( preg_match( '#[^a-z0-9_\.]#', $char ) || ( ( $last = ( $i == ( $length - 1 ) ) ) && preg_match( '#[a-z0-9]+#', $mut_string ) ) )
{
if( $last )
{
$i++;
$mut_string .= $char;
}
$field = $mut_string;
if( ! $this->child_entity )
{
if( ( $str = $this->entity()->create_key( $field ) ) != NULL && $this->entity()->has_key( $str ) )
{
$replace = '`'.$str.'`';
}
} else {
if( substr( $field, 0, 4 ) == 'self' )
{
$tfield = substr( $field, 5, strlen( $field ) - 5 );
if( ( $str = $this->entity()->create_key( $tfield ) ) != NULL && $this->entity()->has_key( $str ) )
{
$replace = $this->entity()->name().'.'.'`'.$str.'`';
}
} else {
if( ( $str = $this->child_entity->create_key( $field ) ) != NULL && $this->child_entity->has_key( $str ) )
{
$replace = $this->child_entity->name().'.'.'`'.$str.'`';
}
}
}
if( $replace != NULL )
{
$exp = substr_replace( $exp, $replace, ( $i - strlen( $field ) ), strlen( $field ) );
$dif = ( strlen( $replace ) - strlen( $field ) );
$length += $dif;
$i += $dif;
}
$mut_string = NULL;
} else {
$mut_string .= $char;
}
}
$counter = 0;
for( $pos = 0; preg_match( '#'.self::LIB_COREDATA_FILLER_ATTRIBUTE.'#', $exp, $match, PREG_OFFSET_CAPTURE, $pos ); $counter++ )
{
$arg_len = strlen( $match[0][0] );
$replace = $this->vars[$counter];
$exp = substr_replace( $exp, $replace, $match[0][1], $arg_len );
$pos += $arg_len + strlen( $replace );
}
$this->set_predicate_format( $exp );
if( $this->debug )
{
echo $this->predicate_format();
exit;
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment