-
Notifications
You must be signed in to change notification settings - Fork 342
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #363 from perftools/default-page-options
- Loading branch information
Showing
12 changed files
with
208 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
namespace XHGui\Options; | ||
|
||
use ArrayAccess; | ||
use ArrayIterator; | ||
use IteratorAggregate; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
|
||
/** | ||
* @see https://symfony.com/doc/3.3/components/options_resolver.html | ||
*/ | ||
abstract class OptionsConfigurator implements ArrayAccess, IteratorAggregate | ||
{ | ||
/** @var array */ | ||
protected $options; | ||
|
||
public function __construct(array $options = []) | ||
{ | ||
$resolver = new OptionsResolver(); | ||
$this->configureOptions($resolver); | ||
|
||
$this->options = $resolver->resolve($options); | ||
} | ||
|
||
abstract protected function configureOptions(OptionsResolver $resolver); | ||
|
||
public function offsetExists($offset): bool | ||
{ | ||
return array_key_exists($offset, $this->options); | ||
} | ||
|
||
public function offsetGet($offset) | ||
{ | ||
return $this->options[$offset]; | ||
} | ||
|
||
public function offsetSet($offset, $value) | ||
{ | ||
if (null === $offset) { | ||
$this->options[] = $value; | ||
} else { | ||
$this->options[$offset] = $value; | ||
} | ||
} | ||
|
||
public function offsetUnset($offset) | ||
{ | ||
unset($this->options[$offset]); | ||
} | ||
|
||
public function getIterator(): ArrayIterator | ||
{ | ||
return new ArrayIterator($this->options); | ||
} | ||
|
||
public function toArray(): array | ||
{ | ||
return $this->options; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
namespace XHGui\Options; | ||
|
||
use Symfony\Component\OptionsResolver\Options; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
use XHGui\Searcher\SearcherInterface; | ||
|
||
class SearchOptions extends OptionsConfigurator | ||
{ | ||
/** | ||
* Options for SearchInterface::getAll | ||
* | ||
* - sort: an array of search criteria (TODO meta.SERVER.REQUEST_TIME => -1 ????) | ||
* - direction: an string, either 'desc' or 'asc' | ||
* - page: an integer, the page to display (e.g. 3) | ||
* - perPage: an integer, how many profiles to display per page (e.g. 25) | ||
* - conditions: an array of criteria to match | ||
* - projection: an array or bool | ||
*/ | ||
protected function configureOptions(OptionsResolver $resolver) | ||
{ | ||
// NOTE: the null values is trickery to set default values via null value | ||
$defaults = [ | ||
'sort' => null, | ||
'direction' => SearcherInterface::DEFAULT_DIRECTION, | ||
'page' => SearcherInterface::DEFAULT_PAGE, | ||
'perPage' => SearcherInterface::DEFAULT_PER_PAGE, | ||
'conditions' => [], | ||
'projection' => false, | ||
]; | ||
$resolver->setDefaults($defaults); | ||
$resolver->setRequired(['sort', 'direction', 'page', 'perPage']); | ||
|
||
$resolver->setAllowedTypes('sort', ['null', 'string']); | ||
$resolver->setAllowedTypes('direction', ['null', 'string']); | ||
$resolver->setAllowedTypes('page', 'int'); | ||
$resolver->setAllowedTypes('perPage', ['null', 'int']); | ||
$resolver->setAllowedTypes('conditions', 'array'); | ||
$resolver->setAllowedTypes('projection', ['bool', 'array']); | ||
|
||
$resolver->setAllowedValues('direction', [null, 'asc', 'desc']); | ||
$resolver->setAllowedValues('sort', [null, 'time', 'wt', 'cpu', 'mu', 'pmu']); | ||
|
||
$resolver->setNormalizer('direction', function (Options $options, $value) use ($defaults) { | ||
if (!$value) { | ||
return $defaults['direction']; | ||
} | ||
|
||
return $value; | ||
}); | ||
$resolver->setNormalizer('page', function (Options $options, $value) use ($defaults) { | ||
if (!$value) { | ||
return $defaults['page']; | ||
} | ||
|
||
return $value; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.