-
Notifications
You must be signed in to change notification settings - Fork 2
/
helpers.php
112 lines (102 loc) · 2.94 KB
/
helpers.php
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
<?php
if (!function_exists('autocomplete_url')) {
/**
* Return a fully qualified autocomplete url
* @param string $relative_path
* @param bool $api
* @return string
*/
function autocomplete_url($relative_path='', $api=false) {
$url = $api ? constant("AUTOCOMPLETE_URL_API") : constant('AUTOCOMPLETE_URL');
$url = $url . '/' . ltrim($relative_path, '\/');
if (str_contains($url, '#')) {
$url = str_replace('#', constant('AUTOCOMPLETE_ATTRIBUTION') . '#', $url);
} else {
$url = $url . constant('AUTOCOMPLETE_ATTRIBUTION');
}
return $url;
}
}
if (! function_exists('value')) {
/**
* Return the default value of the given value.
*
* @param mixed $value
* @return mixed
*/
function value($value)
{
return $value instanceof Closure ? $value() : $value;
}
}
if (!function_exists('array_get')) {
/**
* Get an item from an array using "dot" notation.
*
* @param array $array
* @param string|int|null $key
* @param mixed $default
* @return mixed
*/
function array_get($array, $key, $default=null) {
if (! is_array($array)) {
return value($default);
}
if (is_null($key)) {
return $array;
}
if (array_key_exists($key, $array)) {
return $array[$key];
}
if (strpos($key, '.') === false) {
return $array[$key] ?? value($default);
}
foreach (explode('.', $key) as $segment) {
if (is_array($array) && array_key_exists($segment, $array)) {
$array = $array[$segment];
} else {
return value($default);
}
}
return $array;
}
}
if (!function_exists('sanitize_input')) {
/**
* Sanitize the given input
* @param $data
* @return string
*/
function sanitize_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
}
if (!function_exists('validate_data')) {
/**
* Validate the given error, failure will return messages
* @param $data
* @param $validators
* @param array $messages
* @return array
*/
function validate_data($data, $validators, $messages=[]) {
$errors = [];
foreach($data as $name => $value) {
if($validator = array_get($validators, $name)) {
if (!is_callable($validator)) {
$errors[$name.'_validator'] = 'Validator is not callable.';
}
if (!$validator($value)) {
if (!$message = array_get($messages, $name)) {
$message = sprintf('Invalid Input: %s', $name);
}
$errors[$name] = $message;
}
}
}
return $errors;
}
}