name; } /** * @return string */ public function getLabel(): string { return $this->label; } /** * @return array */ public function getDefinitions(): array { return [ 'name' => $this->name, 'label' => $this->label, 'description' => $this->description, 'operators' => $this->getOperatorDefinitions(), ]; } /** * {@inheritdoc} * * Example implementation can be found in the CurrentDate parameter implementation. * * @return array */ abstract protected function getOperatorDefinitions(): array; /** * @param string $parameterName * @param string $operator * @param mixed $value * @return string */ public function getExpression(string $parameterName, string $operator, $value): string { if (is_null($value)) { throw new \UnexpectedValueException( sprintf('NULL value received for condition %s and operator $s', $this->name, $operator) ); } $operators = $this->getOperatorDefinitions(); foreach ($operators as $definition) { if ($operator == $definition['name'] && isset($definition['value_transform'])) { $value = call_user_func($definition['value_transform'], $value); break; } } switch ($operator) { case $this::VALUE_IN: if (!is_array($value)) { throw new \UnexpectedValueException('The value for the %s operator has to be an array.', $operator); } $formattedValue = implode("','", $value); return "$parameterName in ['$formattedValue']"; case $this::VALUE_NOT_IN: if (!is_array($value)) { throw new \UnexpectedValueException('The value for the %s operator has to be an array.', $operator); } $formattedValue = implode("','", $value); return "$parameterName not in ['$formattedValue']"; case $this::INTERVAL: if (!isset($value['from']) || !isset($value['to']) || $value['to'] < $value['from']) { throw new \UnexpectedValueException( 'The value for the %s operator is not a valid interval.', $operator ); } $start = $value['from']; $end = $value['to']; return "$parameterName >= $start and $parameterName <= $end"; case $this::EQUALS: return "$parameterName == '$value'"; case $this::GREATER: return "$parameterName > $value"; case $this::SMALLER: return "$parameterName < $value"; case $this::EQUALS_PARAM: return "$parameterName == $value"; } throw new \DomainException(sprintf('Unrecognized operator %s', $operator)); } /** * @param mixed $value * @return bool */ public function validateParameterValue($value): bool { return is_string($value); } /** * @param string $operator * @param $value * @return string */ public function getDisplayValue(string $operator, $value): string { $operatorLabel = ''; $operators = $this->getOperatorDefinitions(); foreach ($operators as $definition) { if ($operator == $definition['name']) { $operatorLabel = $definition['label']; if (isset($definition['value_view_transform'])) { $value = call_user_func($definition['value_view_transform'], $value); } break; } } switch ($operator) { case $this::VALUE_IN: case $this::VALUE_NOT_IN: $formattedValue = implode(',', $value); break; case $this::INTERVAL: $start = $value['from']; $end = $value['to']; $formattedValue = "$start and $end"; break; case $this::EQUALS: case $this::GREATER: case $this::SMALLER: case $this::EQUALS_PARAM: $formattedValue = $value; break; default: $formattedValue = ''; break; } return $this->label . ': ' . $operatorLabel . ' ' . $formattedValue; } }