SearchFilter.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace App\Filter;
  3. use ApiPlatform\Core\Api\FilterInterface;
  4. /**
  5. * Class SearchFilter
  6. * @package App\Filter
  7. */
  8. class SearchFilter implements FilterInterface
  9. {
  10. /**
  11. * @var string Exact matching
  12. */
  13. const STRATEGY_EXACT = 'exact';
  14. /**
  15. * @var string The value must be contained in the field
  16. */
  17. const STRATEGY_PARTIAL = 'partial';
  18. /**
  19. * @var string Finds fields that are starting with the value
  20. */
  21. const STRATEGY_START = 'start';
  22. /**
  23. * @var string Finds fields that are ending with the value
  24. */
  25. const STRATEGY_END = 'end';
  26. /**
  27. * @var string Finds fields that are starting with the word
  28. */
  29. const STRATEGY_WORD_START = 'word_start';
  30. protected $properties;
  31. /**
  32. * SearchFilter constructor.
  33. * @param array|null $properties
  34. */
  35. public function __construct(array $properties = null)
  36. {
  37. $this->properties = $properties;
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function getDescription(string $resourceClass): array
  43. {
  44. $description = [];
  45. $properties = $this->properties;
  46. foreach ($properties as $property => $strategy) {
  47. $filterParameterNames = [
  48. $property,
  49. $property.'[]',
  50. ];
  51. foreach ($filterParameterNames as $filterParameterName) {
  52. $description[$filterParameterName] = [
  53. 'property' => $property,
  54. 'type' => 'string',
  55. 'required' => false,
  56. 'strategy' => self::STRATEGY_EXACT,
  57. 'is_collection' => '[]' === substr($filterParameterName, -2),
  58. ];
  59. }
  60. }
  61. return $description;
  62. }
  63. }
  64. ?>