From bdff2f482e8121c79e305ab554207f8134207af0 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Thu, 4 Aug 2022 12:27:18 +0200 Subject: [PATCH 1/5] ColumnDefinition: Add property `$type` --- src/ColumnDefinition.php | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/ColumnDefinition.php b/src/ColumnDefinition.php index ddb8062..d2be0d6 100644 --- a/src/ColumnDefinition.php +++ b/src/ColumnDefinition.php @@ -10,6 +10,9 @@ class ColumnDefinition /** @var string The name of the column */ protected $name; + /** @var ?string The data type of the column */ + protected $type; + /** @var ?string The label of the column */ protected $label; @@ -33,6 +36,30 @@ public function getName(): string return $this->name; } + /** + * Get the data type of the column + * + * @return ?string + */ + public function getType(): ?string + { + return $this->type; + } + + /** + * Set the data type of the column + * + * @param ?string $type + * + * @return $this + */ + public function setType(?string $type): self + { + $this->type = $type; + + return $this; + } + /** * Get the column label * @@ -71,6 +98,11 @@ public static function fromArray(array $options): self } $self = new static($options['name']); + + if (isset($options['type'])) { + $self->setType($options['type']); + } + if (isset($options['label'])) { $self->setLabel($options['label']); } From eb76fc40b2a08afb627c3f88380232cfe29c24a8 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Thu, 4 Aug 2022 12:28:20 +0200 Subject: [PATCH 2/5] ColumnDefinition: Add property `$allowedValues` --- src/ColumnDefinition.php | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/ColumnDefinition.php b/src/ColumnDefinition.php index d2be0d6..0479b27 100644 --- a/src/ColumnDefinition.php +++ b/src/ColumnDefinition.php @@ -16,6 +16,9 @@ class ColumnDefinition /** @var ?string The label of the column */ protected $label; + /** @var ?array The values allowed for this column */ + protected $allowedValues; + /** * Create a new column definition * @@ -84,6 +87,30 @@ public function setLabel(?string $label): self return $this; } + /** + * Get the allowed values for this column + * + * @return ?array + */ + public function getAllowedValues(): ?array + { + return $this->allowedValues; + } + + /** + * Set the allowed values for this column + * + * @param ?array $values + * + * @return $this + */ + public function setAllowedValues(?array $values): self + { + $this->allowedValues = $values; + + return $this; + } + /** * Create a new column definition based on the given options * @@ -107,6 +134,10 @@ public static function fromArray(array $options): self $self->setLabel($options['label']); } + if (isset($options['allowed_values'])) { + $self->setAllowedValues($options['allowed_values']); + } + return $self; } } From 407d33c8ee59402d635811c7f5f0d9b53afaef2c Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Thu, 4 Aug 2022 12:29:13 +0200 Subject: [PATCH 3/5] ColumnDefinition: Add method `isValidValue()` A column definition should be able to evaluate its own rules. They're probably too complex in the future so it makes sense to start with it already now. --- src/ColumnDefinition.php | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/src/ColumnDefinition.php b/src/ColumnDefinition.php index 0479b27..7cf30b7 100644 --- a/src/ColumnDefinition.php +++ b/src/ColumnDefinition.php @@ -3,7 +3,9 @@ namespace ipl\Orm; use InvalidArgumentException; -use LogicException; +use ipl\Stdlib\Filter\Condition; +use ipl\Stdlib\Filter\Like; +use ipl\Stdlib\Filter\Unlike; class ColumnDefinition { @@ -111,6 +113,35 @@ public function setAllowedValues(?array $values): self return $this; } + /** + * Get whether the given filter's value is valid + * + * @param Condition $filter + * + * @return bool + */ + public function isValidValue(Condition $filter): bool + { + if ($filter instanceof Like || $filter instanceof Unlike) { + return true; + } + + switch ($this->type) { + case 'number': + if (! is_numeric($filter->getValue())) { + return false; + } + + break; + } + + if (empty($this->allowedValues)) { + return true; + } + + return isset($this->allowedValues[$filter->getValue()]); + } + /** * Create a new column definition based on the given options * From f70fc993488656bb27c33d062488339eb2367fe9 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Thu, 4 Aug 2022 12:30:35 +0200 Subject: [PATCH 4/5] ColumnDefinition: Add method `getValueLabel()` --- src/ColumnDefinition.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/ColumnDefinition.php b/src/ColumnDefinition.php index 7cf30b7..331832c 100644 --- a/src/ColumnDefinition.php +++ b/src/ColumnDefinition.php @@ -142,6 +142,20 @@ public function isValidValue(Condition $filter): bool return isset($this->allowedValues[$filter->getValue()]); } + /** + * Get the given value's label + * + * Returns the value itself if there's no label available + * + * @param string $value + * + * @return string + */ + public function getValueLabel(string $value): string + { + return $this->allowedValues[$value] ?? $value; + } + /** * Create a new column definition based on the given options * From 5e9b5414b5d5be731b117791fade4800982b3ccf Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Thu, 4 Aug 2022 12:30:53 +0200 Subject: [PATCH 5/5] [TODO] Support for min max properties --- src/ColumnDefinition.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/ColumnDefinition.php b/src/ColumnDefinition.php index 331832c..e48e4ad 100644 --- a/src/ColumnDefinition.php +++ b/src/ColumnDefinition.php @@ -179,6 +179,9 @@ public static function fromArray(array $options): self $self->setLabel($options['label']); } + // min (number start, datetime range start) + // max (number end, string length, datetime range end) + if (isset($options['allowed_values'])) { $self->setAllowedValues($options['allowed_values']); }