1 <?php
  2 /**
  3  * @package     Joomla.Libraries
  4  * @subpackage  Component
  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.txt
  8  */
  9 
 10 defined('JPATH_PLATFORM') or die;
 11 
 12 /**
 13  * View-configuration class for the view-based component router
 14  *
 15  * @since  3.5
 16  */
 17 class JComponentRouterViewconfiguration
 18 {
 19     /**
 20      * Name of the view
 21      *
 22      * @var    string
 23      * @since  3.5
 24      */
 25     public $name;
 26 
 27     /**
 28      * Key of the view
 29      *
 30      * @var    string
 31      * @since  3.5
 32      */
 33     public $key = false;
 34 
 35     /**
 36      * Parentview of this one
 37      *
 38      * @var    JComponentRouterViewconfiguration
 39      * @since  3.5
 40      */
 41     public $parent = false;
 42 
 43     /**
 44      * Key of the parentview
 45      *
 46      * @var    string
 47      * @since  3.5
 48      */
 49     public $parent_key = false;
 50 
 51     /**
 52      * Is this view nestable?
 53      *
 54      * @var    bool
 55      * @since  3.5
 56      */
 57     public $nestable = false;
 58 
 59     /**
 60      * Layouts that are supported by this view
 61      *
 62      * @var    array
 63      * @since  3.5
 64      */
 65     public $layouts = array('default');
 66 
 67     /**
 68      * Child-views of this view
 69      *
 70      * @var    JComponentRouterViewconfiguration[]
 71      * @since  3.5
 72      */
 73     public $children = array();
 74 
 75     /**
 76      * Keys used for this parent view by the child views
 77      *
 78      * @var    array
 79      * @since  3.5
 80      */
 81     public $child_keys = array();
 82 
 83     /**
 84      * Path of views from this one to the root view
 85      *
 86      * @var    array
 87      * @since  3.5
 88      */
 89     public $path = array();
 90 
 91     /**
 92      * Constructor for the View-configuration class
 93      *
 94      * @param   string  $name  Name of the view
 95      *
 96      * @since   3.5
 97      */
 98     public function __construct($name)
 99     {
100         $this->name   = $name;
101         $this->path[] = $name;
102     }
103 
104     /**
105      * Set the name of the view
106      *
107      * @param   string  $name  Name of the view
108      *
109      * @return  JComponentRouterViewconfiguration  This object for chaining
110      *
111      * @since   3.5
112      */
113     public function setName($name)
114     {
115         $this->name = $name;
116 
117         array_pop($this->path);
118         $this->path[] = $name;
119 
120         return $this;
121     }
122 
123     /**
124      * Set the key-identifier for the view
125      *
126      * @param   string  $key  Key of the view
127      *
128      * @return  JComponentRouterViewconfiguration  This object for chaining
129      *
130      * @since   3.5
131      */
132     public function setKey($key)
133     {
134         $this->key = $key;
135 
136         return $this;
137     }
138 
139     /**
140      * Set the parent view of this view
141      *
142      * @param   JComponentRouterViewconfiguration  $parent      Parent view object
143      * @param   string                             $parent_key  Key of the parent view in this context
144      *
145      * @return  JComponentRouterViewconfiguration  This object for chaining
146      *
147      * @since   3.5
148      */
149     public function setParent(JComponentRouterViewconfiguration $parent, $parent_key = false)
150     {
151         if ($this->parent)
152         {
153             $key = array_search($this, $this->parent->children);
154 
155             if ($key !== false)
156             {
157                 unset($this->parent->children[$key]);
158             }
159 
160             if ($this->parent_key)
161             {
162                 $child_key = array_search($this->parent_key, $this->parent->child_keys);
163                 unset($this->parent->child_keys[$child_key]);
164             }
165         }
166 
167         $this->parent       = $parent;
168         $parent->children[] = $this;
169 
170         $this->path   = $parent->path;
171         $this->path[] = $this->name;
172 
173         $this->parent_key = $parent_key;
174 
175         if ($parent_key)
176         {
177             $parent->child_keys[] = $parent_key;
178         }
179 
180         return $this;
181     }
182 
183     /**
184      * Set if this view is nestable or not
185      *
186      * @param   bool  $isNestable  If set to true, the view is nestable
187      *
188      * @return  JComponentRouterViewconfiguration  This object for chaining
189      *
190      * @since   3.5
191      */
192     public function setNestable($isNestable = true)
193     {
194         $this->nestable = (bool) $isNestable;
195 
196         return $this;
197     }
198 
199     /**
200      * Add a layout to this view
201      *
202      * @param   string  $layout  Layouts that this view supports
203      *
204      * @return  JComponentRouterViewconfiguration  This object for chaining
205      *
206      * @since   3.5
207      */
208     public function addLayout($layout)
209     {
210         $this->layouts[] = $layout;
211         $this->layouts   = array_unique($this->layouts);
212 
213         return $this;
214     }
215 
216     /**
217      * Remove a layout from this view
218      *
219      * @param   string  $layout  Layouts that this view supports
220      *
221      * @return  JComponentRouterViewconfiguration  This object for chaining
222      *
223      * @since   3.5
224      */
225     public function removeLayout($layout)
226     {
227         $key = array_search($layout, $this->layouts);
228 
229         if ($key !== false)
230         {
231             unset($this->layouts[$key]);
232         }
233 
234         return $this;
235     }
236 }
237