1 <?php
  2 /**
  3  * @package     FrameworkOnFramework
  4  * @subpackage  database
  5  * @copyright   Copyright (C) 2010-2016 Nicholas K. Dionysopoulos / Akeeba Ltd. All rights reserved.
  6  * @license     GNU General Public License version 2 or later; see LICENSE.txt
  7  *
  8  * This file is adapted from the Joomla! Platform. It is used to iterate a database cursor returning FOFTable objects
  9  * instead of plain stdClass objects
 10  */
 11 
 12 // Protect from unauthorized access
 13 defined('FOF_INCLUDED') or die;
 14 
 15 /**
 16  * Query Element Class.
 17  *
 18  * @property-read    string  $name      The name of the element.
 19  * @property-read    array   $elements  An array of elements.
 20  * @property-read    string  $glue      Glue piece.
 21  *
 22  * @since  11.1
 23  */
 24 class FOFDatabaseQueryElement
 25 {
 26     /**
 27      * @var    string  The name of the element.
 28      * @since  11.1
 29      */
 30     protected $name = null;
 31 
 32     /**
 33      * @var    array  An array of elements.
 34      * @since  11.1
 35      */
 36     protected $elements = null;
 37 
 38     /**
 39      * @var    string  Glue piece.
 40      * @since  11.1
 41      */
 42     protected $glue = null;
 43 
 44     /**
 45      * Constructor.
 46      *
 47      * @param   string  $name      The name of the element.
 48      * @param   mixed   $elements  String or array.
 49      * @param   string  $glue      The glue for elements.
 50      *
 51      * @since   11.1
 52      */
 53     public function __construct($name, $elements, $glue = ',')
 54     {
 55         $this->elements = array();
 56         $this->name = $name;
 57         $this->glue = $glue;
 58 
 59         $this->append($elements);
 60     }
 61 
 62     /**
 63      * Magic function to convert the query element to a string.
 64      *
 65      * @return  string
 66      *
 67      * @since   11.1
 68      */
 69     public function __toString()
 70     {
 71         if (substr($this->name, -2) == '()')
 72         {
 73             return PHP_EOL . substr($this->name, 0, -2) . '(' . implode($this->glue, $this->elements) . ')';
 74         }
 75         else
 76         {
 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      * @since   11.1
 89      */
 90     public function append($elements)
 91     {
 92         if (is_array($elements))
 93         {
 94             $this->elements = array_merge($this->elements, $elements);
 95         }
 96         else
 97         {
 98             $this->elements = array_merge($this->elements, array($elements));
 99         }
100     }
101 
102     /**
103      * Gets the elements of this element.
104      *
105      * @return  array
106      *
107      * @since   11.1
108      */
109     public function getElements()
110     {
111         return $this->elements;
112     }
113 
114     /**
115      * Method to provide deep copy support to nested objects and arrays
116      * when cloning.
117      *
118      * @return  void
119      *
120      * @since   11.3
121      */
122     public function __clone()
123     {
124         foreach ($this as $k => $v)
125         {
126             if (is_object($v) || is_array($v))
127             {
128                 $this->{$k} = unserialize(serialize($v));
129             }
130         }
131     }
132 }
133