-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.php
More file actions
180 lines (160 loc) · 3.72 KB
/
code.php
File metadata and controls
180 lines (160 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
/**
* @package Joomla.Component.Builder
*
* @created 4th September, 2022
* @author Llewellyn van der Merwe <https://dev.vdm.io>
* @git Joomla Component Builder <https://git.vdm.dev/joomla/Component-Builder>
* @copyright Copyright (C) 2015 Vast Development Method. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace VDM\Joomla\Componentbuilder\Search\Agent;
use VDM\Joomla\Utilities\ArrayHelper;
use VDM\Joomla\Utilities\ObjectHelper;
use VDM\Joomla\Componentbuilder\Search\Factory;
use VDM\Joomla\Componentbuilder\Search\Config;
use VDM\Joomla\Componentbuilder\Search\Agent\Search;
use VDM\Joomla\Componentbuilder\Search\Interfaces\FindInterface;
/**
* Search Agent Find
*
* @since 3.2.0
*/
class Find implements FindInterface
{
/**
* Found Values
*
* @var array
* @since 3.2.0
*/
protected array $found = [];
/**
* Search Config
*
* @var Config
* @since 3.2.0
*/
protected Config $config;
/**
* Search
*
* @var Search
* @since 3.2.0
*/
protected Search $search;
/**
* Constructor
*
* @param Config|null $config The search config object.
* @param Search|null $search The search object.
*
* @since 3.2.0
*/
public function __construct(?Config $config = null, ?Search $search = null)
{
$this->config = $config ?: Factory::_('Config');
$this->search = $search ?: Factory::_('Agent.Search');
}
/**
* Get found values
*
* @param string|null $table The table being searched
*
* @return array|null
* @since 3.2.0
*/
public function get(?string $table = null): ?array
{
// set the table name
if (empty($table))
{
$table = $this->config->table_name;
}
if (isset($this->found[$table]))
{
return $this->found[$table];
}
return null;
}
/**
* Search over an item fields
*
* @param object $item The item object of fields to search through
* @param int|null $id The item id
* @param string|null $table The table being searched
*
* @return void
* @since 3.2.0
*/
public function item(object $item, ?int $id =null, ?string $table = null)
{
// set the table name
if (empty($table))
{
$table = $this->config->table_name;
}
// set the item id
if (empty($id))
{
$id = $item->id;
}
if (ObjectHelper::check($item))
{
foreach ($item as $field => $value)
{
if ($field !== 'id' && $this->search->value($value, $id, $field, $table))
{
if (empty($this->found[$table][$id]))
{
$this->found[$table][$id] = new \stdClass();
$this->found[$table][$id]->id = $id;
}
$this->found[$table][$id]->{$field} = $value;
}
}
}
}
/**
* Search over an array of items
*
* @param array|null $items The array of items to search through
* @param string|null $table The table being searched
*
* @return void
* @since 3.2.0
*/
public function items(?array $items = null, ?string $table = null)
{
// set the table name
if (empty($table))
{
$table = $this->config->table_name;
}
if (ArrayHelper::check($items))
{
foreach ($items as $id => $item)
{
$this->item($item, $id, $table);
}
}
}
/**
* Reset all found values of a table
*
* @param string|null $table The table being searched
*
* @return void
* @since 3.2.0
*/
public function reset(?string $table = null)
{
// set the table name
if (empty($table))
{
$table = $this->config->table_name;
}
// empty or unset the table active values
unset($this->found[$table]);
}
}