Example #1
0
 function table($table = null, $tablePrefix = null)
 {
     if ($table === null) {
         return $this->table;
     } else {
         if ($tablePrefix === null) {
             if (is_array($table)) {
                 if (Arr::is_assoc($table)) {
                     foreach ($table as $k => $t) {
                         $this->table($t, $k);
                     }
                 } else {
                     foreach ($table as $t) {
                         $this->table($t);
                     }
                 }
                 return;
             }
             $tablePrefix = $table;
         } elseif ($tablePrefix) {
             $table = new Alias\TableAlias($table, $tablePrefix);
         }
         if (isset($this->table[$tablePrefix])) {
             throw new \Exception('Table Alias "' . $tablePrefix . '" already exists in this query.');
         }
         $this->tables[$tablePrefix] = $table;
     }
     return $this;
 }
Example #2
0
 function _Set($k, $v)
 {
     if ($k === null || Number::is($k)) {
         if ($v instanceof IToSQL) {
             //Add(WhereAnd | WhereOr) -> Append
             //Add(Statement) -> WhereAnd -> Append
             $this->data[] = $v;
         } elseif (is_string($v)) {
             //Add(string) -> Statement -> Add
             $this->data[] = new WhereAND($v);
         } elseif (is_array($v)) {
             if (!$v) {
                 return;
                 //Empty array
             }
             if (Arr::is_assoc($v)) {
                 //Add(array(array('field'=>'value'))) -> Statement -> Add
                 foreach ($v as $k => $vv) {
                     $this->_Set($k, $vv);
                 }
             } elseif (isset($v[0]) && is_array($v[0])) {
                 foreach ($v as $vv) {
                     $this->_Set(null, $vv);
                 }
             } else {
                 //Add(array(expr1,comparison,expr2)) -> Statement -> Add
                 $op = null;
                 if (count($v) == 2) {
                     $op = '=';
                 }
                 if (count($v) == 3) {
                     $op = $v[1];
                     $v = array($v[0], $v[1]);
                 }
                 if ($op === null) {
                     throw new \Exception('Invalid array format');
                 }
                 $this->data[] = new Comparison($v[0], $v[1], $op, static::AUTO_NULL);
             }
         } else {
             throw new \Exception('Unknown format for add');
         }
     } else {
         //Assosiative simple syntax
         $op = '=';
         if ($v instanceof IComparison) {
             $op = '';
         }
         $this->data[] = WhereAND::fromAssign($k, $v, $op, static::AUTO_NULL);
     }
 }
Example #3
0
 function _Set($k, $v)
 {
     if ($k === null || \Radical\Basic\String\Number::is($k)) {
         if (is_array($v)) {
             if (Arr::is_assoc($v)) {
                 foreach ($v as $k => $vv) {
                     $this->_Add($k, $vv);
                 }
             } else {
                 foreach ($v as $k => $vv) {
                     $this->_Add(null, $vv);
                 }
             }
         } elseif ($v !== null) {
             //Add(expr)
             $this->data[] = $v;
         }
     } else {
         //Add(table,field) -> TableExpression
         $this->data[] = new TableExpression($v, $k);
     }
 }
Example #4
0
 function _Set($k, $order_by)
 {
     if ($k === null || \Radical\Basic\String\Number::is($k)) {
         if (is_string($order_by)) {
             $this->data[] = $order_by;
         } elseif ($order_by instanceof OrderBy) {
             //Add(OrderByPart)
             $this->data = $order_by;
         } elseif (is_array($order_by)) {
             if (Arr::is_assoc($order_by)) {
                 foreach ($order_by as $key => $order) {
                     $this->_Set($key, $order);
                 }
             } else {
                 foreach ($order_by as $o) {
                     if (is_array($o)) {
                         //Add(array(array(expr1,order1 = ASC),array(expr1,order2 = ASC)))
                         if (count($o) == 2) {
                             $this->_Set($o[0], $o[1]);
                         } else {
                             throw new \Exception('Unknown array format');
                         }
                     } else {
                         //Add(array(expr1,expr2))
                         $this->_Add(null, $o);
                     }
                 }
             }
         } else {
             throw new \Exception('Invalid order by call');
         }
     } else {
         //Add(expr,order)
         $this->data[] = new OrderByPart($k, $order_by);
     }
 }
Example #5
0
 private function _filter_htmlcorrector($text)
 {
     // Prepare tag lists.
     static $no_nesting, $single_use;
     if (!isset($no_nesting)) {
         // Tags which cannot be nested but are typically left unclosed.
         $no_nesting = Arr::map_assoc(array('li', 'p'));
         // Single use tags in HTML4
         $single_use = Arr::map_assoc(array('base', 'meta', 'link', 'hr', 'br', 'param', 'img', 'area', 'input', 'col', 'frame'));
     }
     // Properly entify angles.
     $text = preg_replace('@<(?=[^a-zA-Z!/]|$)@', '&lt;', $text);
     // Split tags from text.
     $split = preg_split('/<(!--.*?--|[^>]+?)>/s', $text, -1, PREG_SPLIT_DELIM_CAPTURE);
     // Note: PHP ensures the array consists of alternating delimiters and
     // literals
     // and begins and ends with a literal (inserting $null as required).
     $tag = false;
     // Odd/even counter. Tag or no tag.
     $stack = array();
     $output = '';
     foreach ($split as $value) {
         // Process HTML tags.
         if ($tag) {
             // Passthrough comments.
             if (substr($value, 0, 3) == '!--') {
                 $output .= '<' . $value . '>';
             } else {
                 list($tagname) = preg_split('/\\s/', strtolower($value), 2);
                 // Closing tag
                 if ($tagname[0] == '/') {
                     $tagname = substr($tagname, 1);
                     // Discard XHTML closing tags for single use tags.
                     if (!isset($single_use[$tagname])) {
                         // See if we possibly have a matching opening tag on
                         // the stack.
                         if (in_array($tagname, $stack)) {
                             // Close other tags lingering first.
                             do {
                                 $output .= '</' . $stack[0] . '>';
                             } while (array_shift($stack) != $tagname);
                         }
                         // Otherwise, discard it.
                     }
                 } else {
                     // See if we have an identical 'no nesting' tag already
                     // open and close it if found.
                     if (count($stack) && $stack[0] == $tagname && isset($no_nesting[$stack[0]])) {
                         $output .= '</' . array_shift($stack) . '>';
                     }
                     // Push non-single-use tags onto the stack
                     if (!isset($single_use[$tagname])) {
                         array_unshift($stack, $tagname);
                     } else {
                         $value = rtrim($value, ' /') . ' /';
                     }
                     $output .= '<' . $value . '>';
                 }
             }
         } else {
             // Passthrough all text.
             $output .= $value;
         }
         $tag = !$tag;
     }
     // Close remaining tags.
     while (count($stack) > 0) {
         $output .= '</' . array_shift($stack) . '>';
     }
     return $output;
 }
 /**
  * Gets values from an array where $value matches the value in the array.
  * 
  * Or if $value is a callback and $strict is false then where $value(value) is true.
  * 
  * @param mixed $callback
  * @param boolean $strict
  * @return array
  */
 function where($callback, $strict = false)
 {
     return Arr::where($callback, $this, $strict);
 }
Example #7
0
 /**
  * Gets a value from an array using a dot separated path.
  *
  *     // Get the value of $array['foo']['bar']
  *     $value = Path::get($array, 'foo.bar');
  *
  * Using a wildcard "*" will search intermediate arrays and return an array.
  *
  *     // Get the values of "color" in theme
  *     $colors = Path::get($array, 'theme.*.color');
  *
  *     // Using an array of keys
  *     $colors = Path::get($array, array('theme', '*', 'color'));
  *
  * @param   array   array to search
  * @param   mixed   key path string (delimiter separated) or array of keys
  * @param   mixed   default value if the path is not set
  * @param   string  key path delimiter
  * @return  mixed
  */
 public static function get($array, $path, $default = NULL, $delimiter = NULL)
 {
     if (!Arr::is_array($array)) {
         // This is not an array!
         return $default;
     }
     if (is_array($path)) {
         // The path has already been separated into keys
         $keys = $path;
     } else {
         if (array_key_exists($path, $array)) {
             // No need to do extra processing
             return $array[$path];
         }
         if ($delimiter === NULL) {
             // Use the default delimiter
             $delimiter = Arr::$delimiter;
         }
         // Remove starting delimiters and spaces
         $path = ltrim($path, "{$delimiter} ");
         // Remove ending delimiters, spaces, and wildcards
         $path = rtrim($path, "{$delimiter} *");
         // Split the keys by delimiter
         $keys = explode($delimiter, $path);
     }
     do {
         $key = array_shift($keys);
         if (ctype_digit($key)) {
             // Make the key an integer
             $key = (int) $key;
         }
         if (isset($array[$key])) {
             if ($keys) {
                 if (Arr::is_array($array[$key])) {
                     // Dig down into the next part of the path
                     $array = $array[$key];
                 } else {
                     // Unable to dig deeper
                     break;
                 }
             } else {
                 // Found the path requested
                 return $array[$key];
             }
         } elseif ($key === '*') {
             // Handle wildcards
             $values = array();
             foreach ($array as $arr) {
                 $value = Arr::path($arr, implode('.', $keys));
                 if ($value) {
                     $values[] = $value;
                 }
             }
             if ($values) {
                 // Found the values requested
                 return $values;
             } else {
                 // Unable to dig deeper
                 break;
             }
         } else {
             // Unable to dig deeper
             break;
         }
     } while ($keys);
     // Unable to find the value requested
     return $default;
 }