summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorIgor Scheller <igor.scheller@igorshp.de>2019-10-07 21:59:40 +0200
committerIgor Scheller <igor.scheller@igorshp.de>2019-10-07 21:59:40 +0200
commit8d090438b659b641dd0f6cbc99193f3b48b2fc4b (patch)
tree030e2b069d4583e6ad6e498612c959c15850c8e6 /src
parentfaf74150e9481ad9338eb6cc2428d02b24e9fc43 (diff)
Validation rules: min/max/between: Use string length to compare strings
Diffstat (limited to 'src')
-rw-r--r--src/Http/Validation/Rules/Between.php10
-rw-r--r--src/Http/Validation/Rules/Max.php10
-rw-r--r--src/Http/Validation/Rules/Min.php10
-rw-r--r--src/Http/Validation/Rules/StringInputLength.php44
4 files changed, 74 insertions, 0 deletions
diff --git a/src/Http/Validation/Rules/Between.php b/src/Http/Validation/Rules/Between.php
new file mode 100644
index 00000000..106a93ac
--- /dev/null
+++ b/src/Http/Validation/Rules/Between.php
@@ -0,0 +1,10 @@
+<?php
+
+namespace Engelsystem\Http\Validation\Rules;
+
+use Respect\Validation\Rules\Between as RespectBetween;
+
+class Between extends RespectBetween
+{
+ use StringInputLength;
+}
diff --git a/src/Http/Validation/Rules/Max.php b/src/Http/Validation/Rules/Max.php
new file mode 100644
index 00000000..b1b2cfa3
--- /dev/null
+++ b/src/Http/Validation/Rules/Max.php
@@ -0,0 +1,10 @@
+<?php
+
+namespace Engelsystem\Http\Validation\Rules;
+
+use Respect\Validation\Rules\Max as RespectMax;
+
+class Max extends RespectMax
+{
+ use StringInputLength;
+}
diff --git a/src/Http/Validation/Rules/Min.php b/src/Http/Validation/Rules/Min.php
new file mode 100644
index 00000000..ab8d4e1a
--- /dev/null
+++ b/src/Http/Validation/Rules/Min.php
@@ -0,0 +1,10 @@
+<?php
+
+namespace Engelsystem\Http\Validation\Rules;
+
+use Respect\Validation\Rules\Min as RespectMin;
+
+class Min extends RespectMin
+{
+ use StringInputLength;
+}
diff --git a/src/Http/Validation/Rules/StringInputLength.php b/src/Http/Validation/Rules/StringInputLength.php
new file mode 100644
index 00000000..7b5c248b
--- /dev/null
+++ b/src/Http/Validation/Rules/StringInputLength.php
@@ -0,0 +1,44 @@
+<?php
+
+namespace Engelsystem\Http\Validation\Rules;
+
+use DateTime;
+use Illuminate\Support\Str;
+use Throwable;
+
+trait StringInputLength
+{
+ /**
+ * Use the input length of a string
+ *
+ * @param mixed $input
+ * @return bool
+ */
+ public function validate($input): bool
+ {
+ if (
+ is_string($input)
+ && !is_numeric($input)
+ && !$this->isDateTime($input)
+ ) {
+ $input = Str::length($input);
+ }
+
+ return parent::validate($input);
+ }
+
+ /**
+ * @param mixed $input
+ * @return bool
+ */
+ protected function isDateTime($input): bool
+ {
+ try {
+ new DateTime($input);
+ } catch (Throwable $e) {
+ return false;
+ }
+
+ return true;
+ }
+}