| 199 |
lars |
1 |
<?php namespace Clockwork\DataSource;
|
|
|
2 |
|
|
|
3 |
use Clockwork\Request\Request;
|
|
|
4 |
|
|
|
5 |
// Base data source class
|
|
|
6 |
class DataSource implements DataSourceInterface
|
|
|
7 |
{
|
|
|
8 |
// Array of filter functions
|
|
|
9 |
protected $filters = [];
|
|
|
10 |
|
|
|
11 |
// Adds collected data to the request and returns it, to be implemented by extending classes
|
|
|
12 |
public function resolve(Request $request)
|
|
|
13 |
{
|
|
|
14 |
return $request;
|
|
|
15 |
}
|
|
|
16 |
|
|
|
17 |
// Extends the request with an additional data, which is not required for normal use
|
|
|
18 |
public function extend(Request $request)
|
|
|
19 |
{
|
|
|
20 |
return $request;
|
|
|
21 |
}
|
|
|
22 |
|
|
|
23 |
// Reset the data source to an empty state, clearing any collected data
|
|
|
24 |
public function reset()
|
|
|
25 |
{
|
|
|
26 |
}
|
|
|
27 |
|
|
|
28 |
// Register a new filter
|
|
|
29 |
public function addFilter(\Closure $filter, $type = 'default')
|
|
|
30 |
{
|
|
|
31 |
$this->filters[$type] = isset($this->filters[$type])
|
|
|
32 |
? array_merge($this->filters[$type], [ $filter ]) : [ $filter ];
|
|
|
33 |
|
|
|
34 |
return $this;
|
|
|
35 |
}
|
|
|
36 |
|
|
|
37 |
// Clear all registered filters
|
|
|
38 |
public function clearFilters()
|
|
|
39 |
{
|
|
|
40 |
$this->filters = [];
|
|
|
41 |
|
|
|
42 |
return $this;
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
// Returns boolean whether the filterable passes all registered filters
|
|
|
46 |
protected function passesFilters($args, $type = 'default')
|
|
|
47 |
{
|
|
|
48 |
$filters = isset($this->filters[$type]) ? $this->filters[$type] : [];
|
|
|
49 |
|
|
|
50 |
foreach ($filters as $filter) {
|
|
|
51 |
if (! $filter(...$args)) return false;
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
return true;
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
// Censors passwords in an array, identified by key containing "pass" substring
|
|
|
58 |
public function removePasswords(array $data)
|
|
|
59 |
{
|
|
|
60 |
$keys = array_keys($data);
|
|
|
61 |
$values = array_map(function ($value, $key) {
|
|
|
62 |
return strpos($key, 'pass') !== false ? '*removed*' : $value;
|
|
|
63 |
}, $data, $keys);
|
|
|
64 |
|
|
|
65 |
return array_combine($keys, $values);
|
|
|
66 |
}
|
|
|
67 |
}
|