Newer
Older
<?php
namespace calderawp\CalderaFormsQuery\Features;
use calderawp\CalderaContainers\Container;
use calderawp\CalderaContainers\Interfaces\ServiceContainer;
use calderawp\CalderaFormsQuery\Delete\DeleteQueryBuilder;
use calderawp\CalderaFormsQuery\Delete\DoesDeleteQuery;
use calderawp\CalderaFormsQuery\DeleteQueries;
use calderawp\CalderaFormsQuery\MySqlBuilder;
use calderawp\CalderaFormsQuery\Delete\Entry as EntryDelete;
use \calderawp\CalderaFormsQuery\Delete\EntryValues as EntryValuesDelete;
use calderawp\CalderaFormsQuery\Select\DoesSelectQuery;
use \calderawp\CalderaFormsQuery\Select\Entry as EntrySelect;
use \calderawp\CalderaFormsQuery\Select\EntryValues as EntryValueSelect;
use calderawp\CalderaFormsQuery\Select\SelectQueryBuilder;
use calderawp\CalderaFormsQuery\SelectQueries;
class FeatureContainer extends Container
{
/**
* @var ServiceContainer
*/
protected $serviceContainer;
/**
* @var \wpdb
*/
protected $wpdb;
/**
* FeatureContainer constructor.
* @param ServiceContainer $serviceContainer
* @param \wpdb $wpdb
*/
public function __construct(ServiceContainer $serviceContainer, \wpdb $wpdb )
{
$this->serviceContainer = $serviceContainer;
$this->wpdb = $wpdb;
$this->bindServices();
}
/**
* Bind services to service container
*/
protected function bindServices()
{
//@TODO move these to service provider classes
$this->serviceContainer->singleton( MySqlBuilder::class, function(){
return new MySqlBuilder();
});
$this->serviceContainer->bind( SelectQueries::class, function (){
return new SelectQueries(
new EntrySelect(
$this->getBuilder(),
$this->entryTableName()
),
new EntryValueSelect(
$this->getBuilder(),
),
$this->wpdb
);
});
$this->serviceContainer->bind( DeleteQueries::class, function (){
return new DeleteQueries(
new EntryDelete(
$this->getBuilder(),
$this->entryTableName()
),
new EntryValuesDelete(
$this->getBuilder(),
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
),
$this->wpdb
);
});
$this->serviceContainer->singleton( Queries::class, function(){
return new Queries(
$this
->serviceContainer
->make( SelectQueries::class ),
$this
->serviceContainer
->make(DeleteQueries::class )
);
});
}
/**
* Get MySQL builder
*
* @return MySqlBuilder
*/
public function getBuilder()
{
return $this
->serviceContainer
->make( MySqlBuilder::class );
}
/**
* Get query runner
*
* @return Queries
*/
public function getQueries()
{
return $this
->serviceContainer
->make( Queries::class );
}
/**
* Select all entries and entry values by user ID
*
* @param int $userId
* @return array
*/
public function selectByUserId($userId)
{
$query = $this
->getQueries()
->entrySelect()
->queryByUserId($userId);
return $this->collectResults( $this->select( $query ) );
}
protected function entryValueTableName()
{
return "{$this->wpdb->prefix}cf_form_entry_values";
}
/**
* @return string
*/
{
return "{$this->wpdb->prefix}cf_form_entries";
}
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/**
* Collect results using Caldera_Forms_Entry_Entry and Caldera_Forms_Entry_Field to represent values
*
* @param \stdClass[] $entriesValues
* @return array
*/
private function collectResults($entriesValues)
{
$results = [];
foreach ($entriesValues as $entry) {
$entry = new \Caldera_Forms_Entry_Entry($entry);
$query = $this
->getQueries()
->entryValuesSelect()
->queryByEntryId($entry->id);
$entriesValues = $this->select($query);
$entryValuesPrepared = $this->collectEntryValues($entriesValues);
$results[] = [
'entry' => $entry,
'values' => $entryValuesPrepared
];
}
return $results;
}
/**
* Collect entry values as Caldera_Forms_Entry_Field objects
*
* @param \stdClass[] $entriesValues
* @return array
*/
private function collectEntryValues($entriesValues): array
{
$entryValuesPrepared = [];
if (!empty($entriesValues)) {
foreach ($entriesValues as $entryValue) {
$entryValuesPrepared[] = new \Caldera_Forms_Entry_Field($entryValue);
}
}
return $entryValuesPrepared;
}
/**
* Do a select query
*
* @param SelectQueryBuilder $query
* @return \stdClass[]
*/
private function select(SelectQueryBuilder $query)
{
return $this
->getQueries()
->select($query);
}
/**
* Do a delete query
*
* @param DeleteQueryBuilder $query
* @return \stdClass[]
*/
private function delete(DeleteQueryBuilder $query)
{
return $this->
getQueries()
->delete($query);
}