-
Notifications
You must be signed in to change notification settings - Fork 189
Allow configurable file extension for indexing #308
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 2 commits
cdb5b56
5f096c4
7dc4477
f7175bc
9433694
39cfbda
3c33e7f
d2e5048
b9d0d1b
9067b44
1e319c7
58c82e6
44a942e
940eb97
5b1b6bf
707c97f
1e73d08
1f90b4e
ca225ff
c4568bf
5308e7a
a06057b
23a40f0
f4f1067
9cc2736
09fbec2
a5417cd
e317e8c
a1c3845
a1e5654
a81bed9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,29 +9,67 @@ class Options | |
* | ||
* @var array | ||
*/ | ||
public $fileTypes = [".php"]; | ||
private $fileTypes = [".php"]; | ||
|
||
/** | ||
* @param \stdClass|null $options | ||
* @param \Traversable|\stdClass|array|null $options | ||
*/ | ||
public function __construct(\stdClass $options = null) | ||
public function __construct($options = null) | ||
{ | ||
// Do nothing when the $options parameter is not an object | ||
if (!is_object($options)) { | ||
if (!is_object($options) && !is_array($options) && (!$options instanceof \Traversable)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that won't work, you need to do it like this: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll add tests for the Option class when I'm back There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jens1o actually There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah, thanks. Didn't knew that. |
||
return; | ||
} | ||
|
||
$this->fileTypes = $options->fileTypes ?? $this->normalizeFileTypes($this->fileTypes); | ||
foreach ($options as $option => $value) { | ||
$method = 'set' . ucfirst($option); | ||
|
||
call_user_func([$this, $method], $value); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check first wether the method exists? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thx will do it later when I'm back home again There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would rather not use setter methods at all and let There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @felixfbecker How would you validate/filter the option? Calling them extra or using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wouldn't. In the LS code I just don't assign invalid values and JSONMapper will throw if the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will it notify the client about the invalid value type? Since the options are coming from the client we should validate/filter them There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, the exception can be caught and propagated to the client. |
||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would rather let |
||
} | ||
|
||
/** | ||
* Validate and set options for file types | ||
* | ||
* @param array $fileTypes List of file types | ||
*/ | ||
public function setFileTypes(array $fileTypes) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These functions are not needed if you let JsonMapper take care of validation |
||
{ | ||
$fileTypes = filter_var_array($fileTypes, FILTER_SANITIZE_STRING); | ||
$fileTypes = filter_var($fileTypes, FILTER_CALLBACK, ['options' => [$this, 'filterFileTypes']]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it may be possible that you'll pass false into this method. Also, you've inserted a double space There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, did a manual test before I used it, will be tested later |
||
$fileTypes = array_filter($fileTypes); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You need to provide a callback for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With no callback it will remove all that are false, but I can add one There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https://secure.php.net/manual/en/function.array-filter.php says, that the second parameter must be provided. But you could use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Weird for me it says it is optional :/
|
||
|
||
$this->fileTypes = !empty($fileTypes) ? $fileTypes : $this->fileTypes; | ||
} | ||
|
||
/** | ||
* Get list of registered file types | ||
* | ||
* @return array | ||
*/ | ||
public function getFileTypes(): array | ||
{ | ||
return $this->fileTypes; | ||
} | ||
|
||
private function normalizeFileTypes(array $fileTypes): array | ||
/** | ||
* 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) | ||
{ | ||
return array_map(function (string $fileType) { | ||
if (substr($fileType, 0, 1) !== '.') { | ||
$fileType = '.' . $fileType; | ||
} | ||
$fileType = trim($fileType); | ||
|
||
if (empty($fileType)) { | ||
return $fileType; | ||
}, $fileTypes); | ||
} | ||
|
||
if (substr($fileType, 0, 1) !== '.') { | ||
return false; | ||
} | ||
|
||
return $fileType; | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.