| @@ -1,0 +1,123 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * @package VikWP - Libraries | |
| 4 | + * @subpackage adapter.database | |
| 5 | + * @author E4J s.r.l. | |
| 6 | + * @copyright Copyright (C) 2023 E4J s.r.l. All Rights Reserved. | |
| 7 | + * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL | |
| 8 | + * @link https://vikwp.com | |
| 9 | + */ | |
| 10 | + | |
| 11 | +// No direct access | |
| 12 | +defined('ABSPATH') or die('No script kiddies please!'); | |
| 13 | + | |
| 14 | +/** | |
| 15 | + * Query Element Class. | |
| 16 | + * | |
| 17 | + * @since 10.0 | |
| 18 | + */ | |
| 19 | +class JDatabaseQueryElement | |
| 20 | +{ | |
| 21 | + /** | |
| 22 | + * The name of the element. | |
| 23 | + * | |
| 24 | + * @var string | |
| 25 | + */ | |
| 26 | + protected $name = null; | |
| 27 | + | |
| 28 | + /** | |
| 29 | + * An array of elements. | |
| 30 | + * | |
| 31 | + * @var array | |
| 32 | + */ | |
| 33 | + protected $elements = null; | |
| 34 | + | |
| 35 | + /** | |
| 36 | + * Glue piece. | |
| 37 | + * | |
| 38 | + * @var string | |
| 39 | + */ | |
| 40 | + protected $glue = null; | |
| 41 | + | |
| 42 | + /** | |
| 43 | + * Class constructor. | |
| 44 | + * | |
| 45 | + * @param string $name The name of the element. | |
| 46 | + * @param mixed $elements String or array. | |
| 47 | + * @param string $glue The glue for elements. | |
| 48 | + * | |
| 49 | + * @uses append() | |
| 50 | + */ | |
| 51 | + public function __construct($name, $elements, $glue = ',') | |
| 52 | + { | |
| 53 | + $this->name = $name; | |
| 54 | + $this->glue = $glue; | |
| 55 | + | |
| 56 | + $this->elements = array(); | |
| 57 | + | |
| 58 | + $this->append($elements); | |
| 59 | + } | |
| 60 | + | |
| 61 | + /** | |
| 62 | + * Magic function to convert the query element to a string. | |
| 63 | + * | |
| 64 | + * @return string The query element as string. | |
| 65 | + */ | |
| 66 | + public function __toString() | |
| 67 | + { | |
| 68 | + // check if the element is a function | |
| 69 | + if (substr($this->name, -2) == '()') | |
| 70 | + { | |
| 71 | + // implode the elements between the parentheses, i.e. CONCAT_WS(' ', `a`, `b`) | |
| 72 | + return PHP_EOL . substr($this->name, 0, -2) . '(' . implode($this->glue, $this->elements) . ')'; | |
| 73 | + } | |
| 74 | + else | |
| 75 | + { | |
| 76 | + // implode the elements after the element, i.e. SELECT `a`, `b` | |
| 77 | + return PHP_EOL . $this->name . ' ' . implode($this->glue, $this->elements); | |
| 78 | + } | |
| 79 | + } | |
| 80 | + | |
| 81 | + /** | |
| 82 | + * Appends element parts to the internal list. | |
| 83 | + * | |
| 84 | + * @param mixed $elements String or array. | |
| 85 | + * | |
| 86 | + * @return void | |
| 87 | + */ | |
| 88 | + public function append($elements) | |
| 89 | + { | |
| 90 | + if (is_array($elements)) | |
| 91 | + { | |
| 92 | + $this->elements = array_merge($this->elements, $elements); | |
| 93 | + } | |
| 94 | + else | |
| 95 | + { | |
| 96 | + $this->elements[] = $elements; | |
| 97 | + } | |
| 98 | + } | |
| 99 | + | |
| 100 | + /** | |
| 101 | + * Gets the elements of this element. | |
| 102 | + * | |
| 103 | + * @return array The elements. | |
| 104 | + */ | |
| 105 | + public function getElements() | |
| 106 | + { | |
| 107 | + return $this->elements; | |
| 108 | + } | |
| 109 | + | |
| 110 | + /** | |
| 111 | + * Sets the name of this element. | |
| 112 | + * | |
| 113 | + * @param string $name Name of the element. | |
| 114 | + * | |
| 115 | + * @return self This object to support chaining. | |
| 116 | + */ | |
| 117 | + public function setName($name) | |
| 118 | + { | |
| 119 | + $this->name = $name; | |
| 120 | + | |
| 121 | + return $this; | |
| 122 | + } | |
| 123 | +} | |