From 2539d85cb3c4cd2280b1803a85f312962f78f255 Mon Sep 17 00:00:00 2001 From: Dave Ryan Date: Mon, 13 Dec 2021 11:48:13 -0700 Subject: [PATCH 1/4] Add PromptCompare condition --- includes/Conditions/PromptCompare.php | 120 ++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 includes/Conditions/PromptCompare.php diff --git a/includes/Conditions/PromptCompare.php b/includes/Conditions/PromptCompare.php new file mode 100644 index 0000000..356a97f --- /dev/null +++ b/includes/Conditions/PromptCompare.php @@ -0,0 +1,120 @@ +has( 'key' ) ) { + $this->error( "Condition 'key' is missing for type: '{$this->get('condition')}'" ); + } + if ( ! $this->has( 'value' ) ) { + $this->error( "Condition 'value' is missing for type: '{$this->get('condition')}'" ); + } + if ( ! $this->store->has( $this->get( 'key' ) ) ) { + $this->error( "Store did not contain: '{$this->get('key')}'" ); + } + + return $this; + } + + /** + * Evaluate whether or not the key exists. + * + * @return bool + */ + public function evaluate() { + return $this->compare(); + } + + /** + * Map comparison operators to logic. + * + * @return boolean + */ + private function compare() { + $stored = $this->store->get( $this->get( 'key' ) ); + $value = $this->get( 'value' ); + $type = $this->has( 'compare' ) ? strtolower( $this->get( 'compare' ) ) : $this->default_comparison_operator( $stored ); + switch ( $type ) { + case 'contains': + case 'includes': + case 'in': + return $this->multitype_contains( $stored, $value ); + case 'notcontains': + case 'notincludes': + case 'notin': + case 'nin': + return ! $this->multitype_contains( $stored, $value ); + case '<': + case 'lt': + return $stored < $value; + case '<=': + case 'lte': + return $stored <= $value; + case '>': + case 'gt': + return $stored > $value; + case '>=': + case 'gte': + return $stored >= $value; + case '!=': + case '!==': + case 'ne': + case 'notequals': + return $stored !== $value; + case '===': + case '==': + case '=': + case 'eq': + case 'equals': + default: + + return $stored === $value; + } + } + + /** + * Detect needle within arrays, objects or strings. + * + * @param array|object|string|mixed $haystack Variable to search. + * @param string|mixed $needle Variable to find in haystack. + * @return boolean + */ + private function multitype_contains( $haystack, $needle ) { + if ( is_array( $haystack ) ) { + return in_array( $needle, $haystack ); + } elseif ( is_object( $haystack ) ) { + return property_exists( $haystack, $needle ); + } elseif ( is_string( $haystack ) ) { + return stripos( $haystack, strtolower( $needle ) ); + } + + return false; + } + + /** + * Set the default comparison operator condition when a config doesn't provide it. + * + * Arrays and objects check contains. + * Strings and otherwise check equals. + * + * @param mixed $stored + * @return string + */ + private function default_comparison_operator( $stored ) { + if ( is_array( $stored ) || is_object( $stored ) ) { + return 'in'; + } + + return 'eq'; + } +} From f1b850e32ccff02427bec67f4acc36ef58aa6ede Mon Sep 17 00:00:00 2001 From: Dave Ryan Date: Mon, 13 Dec 2021 11:55:20 -0700 Subject: [PATCH 2/4] lint --- includes/Conditions/PromptCompare.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/includes/Conditions/PromptCompare.php b/includes/Conditions/PromptCompare.php index 356a97f..2fc22dd 100644 --- a/includes/Conditions/PromptCompare.php +++ b/includes/Conditions/PromptCompare.php @@ -77,7 +77,6 @@ private function compare() { case 'eq': case 'equals': default: - return $stored === $value; } } @@ -91,7 +90,7 @@ private function compare() { */ private function multitype_contains( $haystack, $needle ) { if ( is_array( $haystack ) ) { - return in_array( $needle, $haystack ); + return in_array( $needle, $haystack, true ); } elseif ( is_object( $haystack ) ) { return property_exists( $haystack, $needle ); } elseif ( is_string( $haystack ) ) { @@ -103,11 +102,11 @@ private function multitype_contains( $haystack, $needle ) { /** * Set the default comparison operator condition when a config doesn't provide it. - * + * * Arrays and objects check contains. * Strings and otherwise check equals. * - * @param mixed $stored + * @param mixed $stored Stored value in $this->store to type-check. * @return string */ private function default_comparison_operator( $stored ) { From fd3fb415d828d45fe20e3100340086850e932f15 Mon Sep 17 00:00:00 2001 From: Dave Ryan Date: Wed, 15 Dec 2021 09:28:03 -0700 Subject: [PATCH 3/4] Rename to Prompt --- includes/Conditions/{PromptCompare.php => Prompt.php} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename includes/Conditions/{PromptCompare.php => Prompt.php} (98%) diff --git a/includes/Conditions/PromptCompare.php b/includes/Conditions/Prompt.php similarity index 98% rename from includes/Conditions/PromptCompare.php rename to includes/Conditions/Prompt.php index 2fc22dd..57e86bc 100644 --- a/includes/Conditions/PromptCompare.php +++ b/includes/Conditions/Prompt.php @@ -5,7 +5,7 @@ /** * Class Prompt */ -class PromptCompare extends AbstractCondition { +class Prompt extends AbstractCondition { /** * Validate condition properties. From dd88c62ce8d8d59afde561c391d51cfb4b792fc1 Mon Sep 17 00:00:00 2001 From: Micah Wood Date: Wed, 15 Dec 2021 11:52:05 -0500 Subject: [PATCH 4/4] Rename from Prompt to Compare --- includes/Conditions/{Prompt.php => Compare.php} | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename includes/Conditions/{Prompt.php => Compare.php} (97%) diff --git a/includes/Conditions/Prompt.php b/includes/Conditions/Compare.php similarity index 97% rename from includes/Conditions/Prompt.php rename to includes/Conditions/Compare.php index 57e86bc..619d705 100644 --- a/includes/Conditions/Prompt.php +++ b/includes/Conditions/Compare.php @@ -3,9 +3,9 @@ namespace WP_Forge\WP_Scaffolding_Tool\Conditions; /** - * Class Prompt + * Class Compare */ -class Prompt extends AbstractCondition { +class Compare extends AbstractCondition { /** * Validate condition properties.