-
-
Notifications
You must be signed in to change notification settings - Fork 333
Expand file tree
/
Copy pathDataSource.php
More file actions
66 lines (52 loc) · 1.51 KB
/
Copy pathDataSource.php
File metadata and controls
66 lines (52 loc) · 1.51 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
<?php namespace Clockwork\DataSource;
use Clockwork\Request\Request;
// Base data source class
class DataSource implements DataSourceInterface
{
// Array of filter functions
protected $filters = [];
// Adds collected data to the request and returns it, to be implemented by extending classes
public function resolve(Request $request)
{
return $request;
}
// Extends the request with an additional data, which is not required for normal use
public function extend(Request $request)
{
return $request;
}
// Reset the data source to an empty state, clearing any collected data
public function reset()
{
}
// Register a new filter
public function addFilter(\Closure $filter, $type = 'default')
{
$this->filters[$type] = array_merge($this->filters[$type] ?? [], [ $filter ]);
return $this;
}
// Clear all registered filters
public function clearFilters()
{
$this->filters = [];
return $this;
}
// Returns boolean whether the filterable passes all registered filters
protected function passesFilters($args, $type = 'default')
{
$filters = $this->filters[$type] ?? [];
foreach ($filters as $filter) {
if (! $filter(...$args)) return false;
}
return true;
}
// Censors passwords in an array, identified by key containing "pass" substring
public function removePasswords(array $data)
{
$keys = array_keys($data);
$values = array_map(function ($value, $key) {
return strpos($key, 'pass') !== false ? '*removed*' : $value;
}, $data, $keys);
return array_combine($keys, $values);
}
}