-
Notifications
You must be signed in to change notification settings - Fork 185
/
Copy pathOptions.php
75 lines (63 loc) Β· 1.76 KB
/
Options.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
<?php
namespace LanguageServer;
class Options
{
/**
* Filetypes the indexer should process
*
* @var array
*/
private $fileTypes = [".php"];
/**
* @param \Traversable|\stdClass|array|null $options
*/
public function __construct($options = null)
{
// Do nothing when the $options parameter is not an object
if (!is_object($options) && !is_array($options) && (!$options instanceof \Traversable)) {
return;
}
foreach ($options as $option => $value) {
$method = 'set' . ucfirst($option);
call_user_func([$this, $method], $value);
}
}
/**
* Validate and set options for file types
*
* @param array $fileTypes List of file types
*/
public function setFileTypes(array $fileTypes)
{
$fileTypes = filter_var_array($fileTypes, FILTER_SANITIZE_STRING);
$fileTypes = filter_var($fileTypes, FILTER_CALLBACK, ['options' => [$this, 'filterFileTypes']]);
$fileTypes = array_filter($fileTypes);
$this->fileTypes = !empty($fileTypes) ? $fileTypes : $this->fileTypes;
}
/**
* Get list of registered file types
*
* @return array
*/
public function getFileTypes(): array
{
return $this->fileTypes;
}
/**
* Filter valid file type
*
* @param string $fileType The file type to filter
* @return string|bool If valid it returns the file type, otherwise false
*/
private function filterFileTypes(string $fileType)
{
$fileType = trim($fileType);
if (empty($fileType)) {
return $fileType;
}
if (substr($fileType, 0, 1) !== '.') {
return false;
}
return $fileType;
}
}