name = $name; $this->glue = $glue; $this->elements = array(); $this->append($elements); } /** * Magic function to convert the query element to a string. * * @return string The query element as string. */ public function __toString() { // check if the element is a function if (substr($this->name, -2) == '()') { // implode the elements between the parentheses, i.e. CONCAT_WS(' ', `a`, `b`) return PHP_EOL . substr($this->name, 0, -2) . '(' . implode($this->glue, $this->elements) . ')'; } else { // implode the elements after the element, i.e. SELECT `a`, `b` return PHP_EOL . $this->name . ' ' . implode($this->glue, $this->elements); } } /** * Appends element parts to the internal list. * * @param mixed $elements String or array. * * @return void */ public function append($elements) { if (is_array($elements)) { $this->elements = array_merge($this->elements, $elements); } else { $this->elements[] = $elements; } } /** * Gets the elements of this element. * * @return array The elements. */ public function getElements() { return $this->elements; } /** * Sets the name of this element. * * @param string $name Name of the element. * * @return self This object to support chaining. */ public function setName($name) { $this->name = $name; return $this; } }