1 <?php
2 /**
3 * @package Joomla.Platform
4 * @subpackage Database
5 *
6 * @copyright Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
7 * @license GNU General Public License version 2 or later; see LICENSE
8 */
9
10 defined('JPATH_PLATFORM') or die;
11
12 /**
13 * Query Element Class.
14 *
15 * @property-read string $name The name of the element.
16 * @property-read array $elements An array of elements.
17 * @property-read string $glue Glue piece.
18 *
19 * @since 11.1
20 */
21 class JDatabaseQueryElement
22 {
23 /**
24 * @var string The name of the element.
25 * @since 11.1
26 */
27 protected $name = null;
28
29 /**
30 * @var array An array of elements.
31 * @since 11.1
32 */
33 protected $elements = null;
34
35 /**
36 * @var string Glue piece.
37 * @since 11.1
38 */
39 protected $glue = null;
40
41 /**
42 * Constructor.
43 *
44 * @param string $name The name of the element.
45 * @param mixed $elements String or array.
46 * @param string $glue The glue for elements.
47 *
48 * @since 11.1
49 */
50 public function __construct($name, $elements, $glue = ',')
51 {
52 $this->elements = array();
53 $this->name = $name;
54 $this->glue = $glue;
55
56 $this->append($elements);
57 }
58
59 /**
60 * Magic function to convert the query element to a string.
61 *
62 * @return string
63 *
64 * @since 11.1
65 */
66 public function __toString()
67 {
68 if (substr($this->name, -2) == '()')
69 {
70 return PHP_EOL . substr($this->name, 0, -2) . '(' . implode($this->glue, $this->elements) . ')';
71 }
72 else
73 {
74 return PHP_EOL . $this->name . ' ' . implode($this->glue, $this->elements);
75 }
76 }
77
78 /**
79 * Appends element parts to the internal list.
80 *
81 * @param mixed $elements String or array.
82 *
83 * @return void
84 *
85 * @since 11.1
86 */
87 public function append($elements)
88 {
89 if (is_array($elements))
90 {
91 $this->elements = array_merge($this->elements, $elements);
92 }
93 else
94 {
95 $this->elements = array_merge($this->elements, array($elements));
96 }
97 }
98
99 /**
100 * Gets the elements of this element.
101 *
102 * @return array
103 *
104 * @since 11.1
105 */
106 public function getElements()
107 {
108 return $this->elements;
109 }
110
111 /**
112 * Sets the name of this element.
113 *
114 * @param string $name Name of the element.
115 *
116 * @return JDatabaseQueryElement Returns this object to allow chaining.
117 *
118 * @since 3.6
119 */
120 public function setName($name)
121 {
122 $this->name = $name;
123
124 return $this;
125 }
126
127 /**
128 * Method to provide deep copy support to nested objects and arrays
129 * when cloning.
130 *
131 * @return void
132 *
133 * @since 11.3
134 */
135 public function __clone()
136 {
137 foreach ($this as $k => $v)
138 {
139 if (is_object($v) || is_array($v))
140 {
141 $this->{$k} = unserialize(serialize($v));
142 }
143 }
144 }
145 }
146