logo       

quoteInto improvment: msg#00006

Subject: quoteInto improvment
The quoteInto() method is a little limiting...

   public function quoteInto($text, $value)
   {
       return str_replace('?', $this->quote($value), $text);
   }

Really should be able to handle an array as a second argument, on top of it it would be nice if then could handle key => value arrays. Maybe something like:

class Db_Quote_Closure {
   public  $value;
   public  $db;
   private $idx = 0;

   public function __construct($db, $v) {
       $this->db    = $db;
       $this->value = $v;
   }

   public function doAssoc($matches) {
       return $this->db->quote($this->value[$matches[1]]);
   }

   public function doIndex($matches) {
       if (isset($this->value[$this->idx]))
           return $this->db->quote($this->value[$this->idx++]);
       return '';
   }
}


   static function quoteInto($str, $value) {
       if (is_array($value)) {
           $allNumbers = true;
           foreach (array_keys($value) as $k) {
               $allNumbers &= is_numeric($k);
           }

           $obj = new Db_Quote_Closure($db, $value);

           if ($allNumbers) {
return preg_replace_callback('/\?/', array($obj, 'doIndex'), $str);
           } else {
return preg_replace_callback('/:(\w+)/', array($obj, 'doAssoc'), $str);
           }
       } else {
           return str_replace('?', $db->quote($value), $str);
       }
   }



<Prev in Thread] Current Thread [Next in Thread>