From 9e113faf786ce4b5973b1723960621ef41c68458 Mon Sep 17 00:00:00 2001 From: Pekka Laiho Date: Mon, 1 Jun 2020 20:12:26 +0700 Subject: [PATCH] compare equality for collections, concat strings with str --- src/Lib/Compare.php | 20 ++++++++++++++------ src/Lib/Math.php | 2 ++ src/Lib/Types.php | 10 +++++----- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/Lib/Compare.php b/src/Lib/Compare.php index 0fbeeef..594359f 100644 --- a/src/Lib/Compare.php +++ b/src/Lib/Compare.php @@ -1,6 +1,7 @@ set('=', new CoreFunc('=', 'Return true if arguments are equal.', 2, 2, - fn ($a, $b) => $a == $b + fn ($a, $b) => $this->getValue($a) == $this->getValue($b) )); $env->set('==', new CoreFunc('==', 'Return true if arguments are equal using strict comparison.', 2, 2, - fn ($a, $b) => $a === $b + fn ($a, $b) => $this->getValue($a) === $this->getValue($b) )); $env->set('!=', new CoreFunc('!=', 'Return true if arguments are not equal.', 2, 2, - fn ($a, $b) => $a != $b + fn ($a, $b) => $this->getValue($a) != $this->getValue($b) )); $env->set('!==', new CoreFunc('!==', 'Return true if arguments are not equal using strict comparison.', 2, 2, - fn ($a, $b) => $a !== $b + fn ($a, $b) => $this->getValue($a) !== $this->getValue($b) )); $env->set('<', new CoreFunc('<', 'Return true if first argument is less than second argument.', 2, 2, @@ -42,4 +41,13 @@ class Compare implements ILib fn ($a, $b) => $a >= $b )); } + + private function getValue($a) + { + if ($a instanceof Collection) { + return $a->getData(); + } + + return $a; + } } diff --git a/src/Lib/Math.php b/src/Lib/Math.php index e66dcf5..a8ddba0 100644 --- a/src/Lib/Math.php +++ b/src/Lib/Math.php @@ -45,5 +45,7 @@ class Math implements ILib return array_reduce(array_slice($args, 1), fn ($a, $b) => $a % $b, $args[0]); } )); + + // TODO: add pow, sqrt, floor, ceil, abs } } diff --git a/src/Lib/Types.php b/src/Lib/Types.php index d0d1f13..1b35709 100644 --- a/src/Lib/Types.php +++ b/src/Lib/Types.php @@ -16,20 +16,20 @@ class Types implements ILib { // Conversions - $env->set('to-bool', new CoreFunc('to-bool', 'Convert argument to boolean.', 1, 1, + $env->set('bool', new CoreFunc('bool', 'Convert argument to boolean.', 1, 1, fn ($a) => boolval($a) )); - $env->set('to-float', new CoreFunc('to-float', 'Convert argument to float.', 1, 1, + $env->set('float', new CoreFunc('float', 'Convert argument to float.', 1, 1, fn ($a) => floatval($a) )); - $env->set('to-int', new CoreFunc('to-int', 'Convert argument to integer.', 1, 1, + $env->set('int', new CoreFunc('int', 'Convert argument to integer.', 1, 1, fn ($a) => intval($a) )); - $env->set('to-str', new CoreFunc('fn?', 'Convert argument to string.', 1, 1, - fn ($a) => strval($a) + $env->set('str', new CoreFunc('str', 'Convert arguments to string and concatenate them together.', 1, -1, + fn (...$args) => implode('', array_map('strval', $args)) )); // Test types