1<?php
2/**
3 * @package     Joomla.Administrator
4 * @subpackage  com_finder
5 *
6 * @copyright   Copyright (C) 2005 - 2020 Open Source Matters, Inc. All rights reserved.
7 * @license     GNU General Public License version 2 or later; see LICENSE.txt
8 */
9
10defined('_JEXEC') or die;
11
12JLoader::register('FinderIndexer', __DIR__ . '/indexer.php');
13
14/**
15 * Result class for the Finder indexer package.
16 *
17 * This class uses magic __get() and __set() methods to prevent properties
18 * being added that might confuse the system. All properties not explicitly
19 * declared will be pushed into the elements array and can be accessed
20 * explicitly using the getElement() method.
21 *
22 * @since  2.5
23 */
24class FinderIndexerResult
25{
26	/**
27	 * An array of extra result properties.
28	 *
29	 * @var    array
30	 * @since  2.5
31	 */
32	protected $elements = array();
33
34	/**
35	 * This array tells the indexer which properties should be indexed and what
36	 * weights to use for those properties.
37	 *
38	 * @var    array
39	 * @since  2.5
40	 */
41	protected $instructions = array(
42		FinderIndexer::TITLE_CONTEXT => array('title', 'subtitle', 'id'),
43		FinderIndexer::TEXT_CONTEXT  => array('summary', 'body'),
44		FinderIndexer::META_CONTEXT  => array('meta', 'list_price', 'sale_price'),
45		FinderIndexer::PATH_CONTEXT  => array('path', 'alias'),
46		FinderIndexer::MISC_CONTEXT  => array('comments'),
47	);
48
49	/**
50	 * The indexer will use this data to create taxonomy mapping entries for
51	 * the item so that it can be filtered by type, label, category,
52	 * or whatever.
53	 *
54	 * @var    array
55	 * @since  2.5
56	 */
57	protected $taxonomy = array();
58
59	/**
60	 * The content URL.
61	 *
62	 * @var    string
63	 * @since  2.5
64	 */
65	public $url;
66
67	/**
68	 * The content route.
69	 *
70	 * @var    string
71	 * @since  2.5
72	 */
73	public $route;
74
75	/**
76	 * The content title.
77	 *
78	 * @var    string
79	 * @since  2.5
80	 */
81	public $title;
82
83	/**
84	 * The content description.
85	 *
86	 * @var    string
87	 * @since  2.5
88	 */
89	public $description;
90
91	/**
92	 * The published state of the result.
93	 *
94	 * @var    integer
95	 * @since  2.5
96	 */
97	public $published;
98
99	/**
100	 * The content published state.
101	 *
102	 * @var    integer
103	 * @since  2.5
104	 */
105	public $state;
106
107	/**
108	 * The content access level.
109	 *
110	 * @var    integer
111	 * @since  2.5
112	 */
113	public $access;
114
115	/**
116	 * The content language.
117	 *
118	 * @var    string
119	 * @since  2.5
120	 */
121	public $language = '*';
122
123	/**
124	 * The publishing start date.
125	 *
126	 * @var    string
127	 * @since  2.5
128	 */
129	public $publish_start_date;
130
131	/**
132	 * The publishing end date.
133	 *
134	 * @var    string
135	 * @since  2.5
136	 */
137	public $publish_end_date;
138
139	/**
140	 * The generic start date.
141	 *
142	 * @var    string
143	 * @since  2.5
144	 */
145	public $start_date;
146
147	/**
148	 * The generic end date.
149	 *
150	 * @var    string
151	 * @since  2.5
152	 */
153	public $end_date;
154
155	/**
156	 * The item list price.
157	 *
158	 * @var    mixed
159	 * @since  2.5
160	 */
161	public $list_price;
162
163	/**
164	 * The item sale price.
165	 *
166	 * @var    mixed
167	 * @since  2.5
168	 */
169	public $sale_price;
170
171	/**
172	 * The content type id. This is set by the adapter.
173	 *
174	 * @var    integer
175	 * @since  2.5
176	 */
177	public $type_id;
178
179	/**
180	 * The default language for content.
181	 *
182	 * @var    string
183	 * @since  3.0.2
184	 */
185	public $defaultLanguage;
186
187	/**
188	 * Constructor
189	 *
190	 * @since   3.0.3
191	 */
192	public function __construct()
193	{
194		$this->defaultLanguage = JComponentHelper::getParams('com_languages')->get('site', 'en-GB');
195	}
196
197	/**
198	 * The magic set method is used to push additional values into the elements
199	 * array in order to preserve the cleanliness of the object.
200	 *
201	 * @param   string  $name   The name of the element.
202	 * @param   mixed   $value  The value of the element.
203	 *
204	 * @return  void
205	 *
206	 * @since   2.5
207	 */
208	public function __set($name, $value)
209	{
210		$this->setElement($name, $value);
211	}
212
213	/**
214	 * The magic get method is used to retrieve additional element values from the elements array.
215	 *
216	 * @param   string  $name  The name of the element.
217	 *
218	 * @return  mixed  The value of the element if set, null otherwise.
219	 *
220	 * @since   2.5
221	 */
222	public function __get($name)
223	{
224		return $this->getElement($name);
225	}
226
227	/**
228	 * The magic isset method is used to check the state of additional element values in the elements array.
229	 *
230	 * @param   string  $name  The name of the element.
231	 *
232	 * @return  boolean  True if set, false otherwise.
233	 *
234	 * @since   2.5
235	 */
236	public function __isset($name)
237	{
238		return isset($this->elements[$name]);
239	}
240
241	/**
242	 * The magic unset method is used to unset additional element values in the elements array.
243	 *
244	 * @param   string  $name  The name of the element.
245	 *
246	 * @return  void
247	 *
248	 * @since   2.5
249	 */
250	public function __unset($name)
251	{
252		unset($this->elements[$name]);
253	}
254
255	/**
256	 * Method to retrieve additional element values from the elements array.
257	 *
258	 * @param   string  $name  The name of the element.
259	 *
260	 * @return  mixed  The value of the element if set, null otherwise.
261	 *
262	 * @since   2.5
263	 */
264	public function getElement($name)
265	{
266		// Get the element value if set.
267		if (array_key_exists($name, $this->elements))
268		{
269			return $this->elements[$name];
270		}
271
272		return null;
273	}
274
275	/**
276	 * Method to retrieve all elements.
277	 *
278	 * @return  array  The elements
279	 *
280	 * @since   3.8.3
281	 */
282	public function getElements()
283	{
284		return $this->elements;
285	}
286
287	/**
288	 * Method to set additional element values in the elements array.
289	 *
290	 * @param   string  $name   The name of the element.
291	 * @param   mixed   $value  The value of the element.
292	 *
293	 * @return  void
294	 *
295	 * @since   2.5
296	 */
297	public function setElement($name, $value)
298	{
299		$this->elements[$name] = $value;
300	}
301
302	/**
303	 * Method to get all processing instructions.
304	 *
305	 * @return  array  An array of processing instructions.
306	 *
307	 * @since   2.5
308	 */
309	public function getInstructions()
310	{
311		return $this->instructions;
312	}
313
314	/**
315	 * Method to add a processing instruction for an item property.
316	 *
317	 * @param   string  $group     The group to associate the property with.
318	 * @param   string  $property  The property to process.
319	 *
320	 * @return  void
321	 *
322	 * @since   2.5
323	 */
324	public function addInstruction($group, $property)
325	{
326		// Check if the group exists. We can't add instructions for unknown groups.
327		// Check if the property exists in the group.
328		if (array_key_exists($group, $this->instructions) && !in_array($property, $this->instructions[$group], true))
329		{
330			// Add the property to the group.
331			$this->instructions[$group][] = $property;
332		}
333	}
334
335	/**
336	 * Method to remove a processing instruction for an item property.
337	 *
338	 * @param   string  $group     The group to associate the property with.
339	 * @param   string  $property  The property to process.
340	 *
341	 * @return  void
342	 *
343	 * @since   2.5
344	 */
345	public function removeInstruction($group, $property)
346	{
347		// Check if the group exists. We can't remove instructions for unknown groups.
348		if (array_key_exists($group, $this->instructions))
349		{
350			// Search for the property in the group.
351			$key = array_search($property, $this->instructions[$group]);
352
353			// If the property was found, remove it.
354			if ($key !== false)
355			{
356				unset($this->instructions[$group][$key]);
357			}
358		}
359	}
360
361	/**
362	 * Method to get the taxonomy maps for an item.
363	 *
364	 * @param   string  $branch  The taxonomy branch to get. [optional]
365	 *
366	 * @return  array  An array of taxonomy maps.
367	 *
368	 * @since   2.5
369	 */
370	public function getTaxonomy($branch = null)
371	{
372		// Get the taxonomy branch if available.
373		if ($branch !== null && isset($this->taxonomy[$branch]))
374		{
375			// Filter the input.
376			$branch = preg_replace('#[^\pL\pM\pN\p{Pi}\p{Pf}\'+-.,_]+#mui', ' ', $branch);
377
378			return $this->taxonomy[$branch];
379		}
380
381		return $this->taxonomy;
382	}
383
384	/**
385	 * Method to add a taxonomy map for an item.
386	 *
387	 * @param   string   $branch  The title of the taxonomy branch to add the node to.
388	 * @param   string   $title   The title of the taxonomy node.
389	 * @param   integer  $state   The published state of the taxonomy node. [optional]
390	 * @param   integer  $access  The access level of the taxonomy node. [optional]
391	 *
392	 * @return  void
393	 *
394	 * @since   2.5
395	 */
396	public function addTaxonomy($branch, $title, $state = 1, $access = 1)
397	{
398		// Filter the input.
399		$branch = preg_replace('#[^\pL\pM\pN\p{Pi}\p{Pf}\'+-.,_]+#mui', ' ', $branch);
400
401		// Create the taxonomy node.
402		$node = new JObject;
403		$node->title = $title;
404		$node->state = (int) $state;
405		$node->access = (int) $access;
406
407		// Add the node to the taxonomy branch.
408		$this->taxonomy[$branch][$node->title] = $node;
409	}
410
411	/**
412	 * Method to set the item language
413	 *
414	 * @return  void
415	 *
416	 * @since   3.0
417	 */
418	public function setLanguage()
419	{
420		if ($this->language == '')
421		{
422			$this->language = $this->defaultLanguage;
423		}
424	}
425}
426