src/Filter/Field.php line 85

Open in your IDE?
  1. <?php
  2. namespace App\Filter;
  3. use Symfony\Component\HttpFoundation\Request;
  4. /**
  5.  * Class Field
  6.  *
  7.  * @package App\Filter
  8.  */
  9. abstract class Field implements \JsonSerializable {
  10.     private $request;
  11.     public $values;
  12.     /**
  13.      * Uri param key name
  14.      * @return mixed
  15.      */
  16.     abstract public function uriParamKey();
  17.     /**
  18.      * Entity name
  19.      * @return mixed
  20.      */
  21.     abstract public function entityName();
  22.     /**
  23.      * Condition to compare
  24.      * @return mixed
  25.      */
  26.     abstract public function condition();
  27.     /**
  28.      * Param Value
  29.      * @return mixed
  30.      */
  31.     abstract public function value();
  32.     public function __construct(Request $request){
  33.         $this->request = $request;
  34.         $this->values = $this->extractValues();
  35.     }
  36.     /**
  37.      * Param value
  38.      * @return mixed|null
  39.      */
  40.     public function singleParamValue(){
  41.         return $this->values ?? null;
  42.     }
  43.     /**
  44.      * Entity property name
  45.      * @return mixed
  46.      */
  47.     public function entityProperty(){
  48.         return $this->uriParamKey();
  49.     }
  50.     /**
  51.      * Extract values from request
  52.      * @return mixed
  53.      */
  54.     private function extractValues(){
  55.         return $this->request->get($this->uriParamKey());
  56.     }
  57.     /**
  58.      * Enforce array for easy check
  59.      *
  60.      * @return array|mixed
  61.      */
  62.     private function selectedValues(){
  63.         return is_array($this->values) ? $this->values : [$this->values];
  64.     }
  65.     /**
  66.      * Specify data which should be serialized to JSON
  67.      *
  68.      * @link https://php.net/manual/en/jsonserializable.jsonserialize.php
  69.      * @return mixed data which can be serialized by <b>json_encode</b>,
  70.      * which is a value of any type other than a resource.
  71.      * @since 5.4.0
  72.      */
  73.     public function jsonSerialize() {
  74.         return [
  75.             'name' => $this->uriParamKey(),
  76.             'value' => $this->value(),
  77.         ];
  78.     }
  79. }