<?php
namespace App\Filter;
use Symfony\Component\HttpFoundation\Request;
/**
* Class Field
*
* @package App\Filter
*/
abstract class Field implements \JsonSerializable {
private $request;
public $values;
/**
* Uri param key name
* @return mixed
*/
abstract public function uriParamKey();
/**
* Entity name
* @return mixed
*/
abstract public function entityName();
/**
* Condition to compare
* @return mixed
*/
abstract public function condition();
/**
* Param Value
* @return mixed
*/
abstract public function value();
public function __construct(Request $request){
$this->request = $request;
$this->values = $this->extractValues();
}
/**
* Param value
* @return mixed|null
*/
public function singleParamValue(){
return $this->values ?? null;
}
/**
* Entity property name
* @return mixed
*/
public function entityProperty(){
return $this->uriParamKey();
}
/**
* Extract values from request
* @return mixed
*/
private function extractValues(){
return $this->request->get($this->uriParamKey());
}
/**
* Enforce array for easy check
*
* @return array|mixed
*/
private function selectedValues(){
return is_array($this->values) ? $this->values : [$this->values];
}
/**
* Specify data which should be serialized to JSON
*
* @link https://php.net/manual/en/jsonserializable.jsonserialize.php
* @return mixed data which can be serialized by <b>json_encode</b>,
* which is a value of any type other than a resource.
* @since 5.4.0
*/
public function jsonSerialize() {
return [
'name' => $this->uriParamKey(),
'value' => $this->value(),
];
}
}