2017-02-18 00:18:16 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace LanguageServer;
|
|
|
|
|
|
|
|
class Options
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Filetypes the indexer should process
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-02-18 09:38:55 +00:00
|
|
|
private $fileTypes = [".php"];
|
2017-02-18 00:18:16 +00:00
|
|
|
|
|
|
|
/**
|
2017-02-18 09:38:55 +00:00
|
|
|
* @param \Traversable|\stdClass|array|null $options
|
2017-02-18 00:18:16 +00:00
|
|
|
*/
|
2017-02-18 09:38:55 +00:00
|
|
|
public function __construct($options = null)
|
2017-02-18 00:18:16 +00:00
|
|
|
{
|
|
|
|
// Do nothing when the $options parameter is not an object
|
2017-02-18 09:38:55 +00:00
|
|
|
if (!is_object($options) && !is_array($options) && (!$options instanceof \Traversable)) {
|
2017-02-18 00:18:16 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-18 09:38:55 +00:00
|
|
|
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;
|
2017-02-18 00:18:16 +00:00
|
|
|
}
|
|
|
|
|
2017-02-18 09:38:55 +00:00
|
|
|
/**
|
|
|
|
* 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)
|
2017-02-18 00:18:16 +00:00
|
|
|
{
|
2017-02-18 09:38:55 +00:00
|
|
|
$fileType = trim($fileType);
|
2017-02-18 00:18:16 +00:00
|
|
|
|
2017-02-18 09:38:55 +00:00
|
|
|
if (empty($fileType)) {
|
2017-02-18 00:18:16 +00:00
|
|
|
return $fileType;
|
2017-02-18 09:38:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (substr($fileType, 0, 1) !== '.') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $fileType;
|
2017-02-18 00:18:16 +00:00
|
|
|
}
|
|
|
|
}
|