-
Notifications
You must be signed in to change notification settings - Fork 31
Add Tribe__Cache::has method #2061
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -732,5 +732,55 @@ public static function usearch( $needle, array $haystack, callable $callback ) { | |
|
|
||
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * Checks if an array has a specific shape. | ||
| * | ||
| * @since TBD | ||
| * | ||
| * @param array $array The array to check. | ||
| * @param array<string|int,callable> $shape The shape to check for. Each key, either a string or an integer, | ||
| * maps to a callable that will be used to validate the value at that key. | ||
| * The callable must have the signature `fn( mixed $value ) :bool`. | ||
| * @param bool $strict Whether the array should only contain the keys specified in the shape. | ||
| * | ||
| * @return bool Whether the array has the specified shape. | ||
| */ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| public static function has_shape( $array, array $shape, bool $strict = false ): bool { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [phpcs] reported by reviewdog 🐶 |
||
| if ( ! is_array( $array ) ) { | ||
| return false; | ||
| } | ||
|
|
||
| if ( | ||
| $strict | ||
| && ( | ||
| array_intersect_key( $array, $shape ) !== $array | ||
| || | ||
| array_diff_key( $array, $shape ) !== [] | ||
| ) | ||
| ) { | ||
| return false; | ||
| } | ||
|
|
||
| if ( count( array_intersect_key( $shape, $array ) ) < count( $shape ) ) { | ||
| return false; | ||
| } | ||
|
|
||
| foreach ( $shape as $key => $check ) { | ||
| if ( ! is_callable( $check ) ) { | ||
| throw new \BadMethodCallException( 'The shape array must contain only callables as values.' ); | ||
| } | ||
|
|
||
| try { | ||
| if ( ! $check( $array[ $key ] ) ) { | ||
| return false; | ||
| } | ||
| } catch ( \Throwable $th ) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,7 @@ | |
|
|
||
| class Tribe__Utils__Array_Test extends \Codeception\Test\Unit { | ||
| public function shape_filter_data_provider() { | ||
| $test_shape = [ | ||
| $test_shape = [ | ||
| 'a' => | ||
| [ | ||
| 'deeply' => | ||
|
|
@@ -306,10 +306,10 @@ public function test_shape_filter( array $input, array $shape, array $expected ) | |
| } | ||
|
|
||
| public function usearch_data_provider() { | ||
| $value_gt_needle = static function ( $needle, $value ): bool { | ||
| $value_gt_needle = static function ( $needle, $value ): bool { | ||
| return $value > $needle; | ||
| }; | ||
| $matches_needle = static function ( $needle, $value ): bool { | ||
| $matches_needle = static function ( $needle, $value ): bool { | ||
| return $value === $needle; | ||
| }; | ||
| $callback_using_value_and_key = static function ( $needle, $value, $key ): bool { | ||
|
|
@@ -352,4 +352,90 @@ public function usearch_data_provider() { | |
| public function test_usearch( $needle, array $haystack, $expected, callable $callback ) { | ||
| $this->assertEquals( $expected, Arr::usearch( $needle, $haystack, $callback ) ); | ||
| } | ||
|
|
||
| public function has_shape_data_provider(): array { | ||
| return [ | ||
| 'not an array' => [ 'foo', [], true, false ], | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [phpcs] reported by reviewdog 🐶 |
||
| 'empty array, empty shape' => [ [], [], true, true ], | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [phpcs] reported by reviewdog 🐶 |
||
| 'empty array, non-empty shape, strict' => [ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [phpcs] reported by reviewdog 🐶 |
||
| [], | ||
| [ 'foo' => 'is_string' ], | ||
| true, | ||
| false | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [phpcs] reported by reviewdog 🐶
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [phpcs] reported by reviewdog 🐶 |
||
| ], | ||
| 'empty array, non-empty shape, non-strict' => [ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [phpcs] reported by reviewdog 🐶 |
||
| [], | ||
| [ 'foo' => 'is_string' ], | ||
| false, | ||
| false | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [phpcs] reported by reviewdog 🐶
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [phpcs] reported by reviewdog 🐶 |
||
| ], | ||
| 'non-empty array, function shape, missing key, strict' => [ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [phpcs] reported by reviewdog 🐶 |
||
| [ 'foo' => 23 ], | ||
| [ 'bar' => 'is_string' ], | ||
| true, | ||
| false | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [phpcs] reported by reviewdog 🐶
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [phpcs] reported by reviewdog 🐶 |
||
| ], | ||
| 'non-empty array, function shape, missing key, non-strict' => [ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [phpcs] reported by reviewdog 🐶 |
||
| [ 'foo' => 23 ], | ||
| [ 'bar' => 'is_string' ], | ||
| false, | ||
| false | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [phpcs] reported by reviewdog 🐶
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [phpcs] reported by reviewdog 🐶 |
||
| ], | ||
| 'non-empty array, function shape, extra key, strict' => [ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [phpcs] reported by reviewdog 🐶 |
||
| [ 'foo' => 23, 'bar' => 'baz' ], | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [phpcs] reported by reviewdog 🐶 |
||
| [ 'foo' => 'is_int' ], | ||
| true, | ||
| false | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [phpcs] reported by reviewdog 🐶
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [phpcs] reported by reviewdog 🐶 |
||
| ], | ||
| 'non-empty array, function shape, extra key, non-strict' => [ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [phpcs] reported by reviewdog 🐶 |
||
| [ 'foo' => 23, 'bar' => 'baz' ], | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [phpcs] reported by reviewdog 🐶 |
||
| [ 'foo' => 'is_int' ], | ||
| false, | ||
| true | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [phpcs] reported by reviewdog 🐶
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [phpcs] reported by reviewdog 🐶 |
||
| ], | ||
| 'non-empty array, closure shape, all key fail failure, strict' => [ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [phpcs] reported by reviewdog 🐶 |
||
| [ 'foo' => 23, 'bar' => 89 ], | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [phpcs] reported by reviewdog 🐶 |
||
| [ 'foo' => fn( $foo ) => $foo === 'hello', 'bar' => fn( $bar ) => $bar === 'world' ], | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [phpcs] reported by reviewdog 🐶 |
||
| true, | ||
| false | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [phpcs] reported by reviewdog 🐶
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [phpcs] reported by reviewdog 🐶 |
||
| ], | ||
| 'non-empty array, closure shape, all key fail failure, non-strict' => [ | ||
| [ 'foo' => 23, 'bar' => 89 ], | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [phpcs] reported by reviewdog 🐶 |
||
| [ 'foo' => fn( $foo ) => $foo === 'hello', 'bar' => fn( $bar ) => $bar === 'world' ], | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [phpcs] reported by reviewdog 🐶 |
||
| false, | ||
| false | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [phpcs] reported by reviewdog 🐶
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [phpcs] reported by reviewdog 🐶 |
||
| ], | ||
| 'non-empty array, closure shape, all key pass, strict' => [ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [phpcs] reported by reviewdog 🐶 |
||
| [ 'foo' => 'hello', 'bar' => 'world' ], | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [phpcs] reported by reviewdog 🐶 |
||
| [ 'foo' => fn( $foo ) => $foo === 'hello', 'bar' => fn( $bar ) => $bar === 'world' ], | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [phpcs] reported by reviewdog 🐶 |
||
| true, | ||
| true | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [phpcs] reported by reviewdog 🐶
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [phpcs] reported by reviewdog 🐶 |
||
| ], | ||
| 'non-empty array, closure shape, all key pass, non-strict ' => [ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [phpcs] reported by reviewdog 🐶 |
||
| [ 'foo' => 'hello', 'bar' => 'world' ], | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [phpcs] reported by reviewdog 🐶 |
||
| [ 'foo' => fn( $foo ) => $foo === 'hello', 'bar' => fn( $bar ) => $bar === 'world' ], | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [phpcs] reported by reviewdog 🐶 |
||
| false, | ||
| true | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [phpcs] reported by reviewdog 🐶
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [phpcs] reported by reviewdog 🐶 |
||
| ], | ||
| 'non-empty array, closure shape, some key pass, strict' => [ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [phpcs] reported by reviewdog 🐶 |
||
| [ 'foo' => 'hello', 'bar' => 89 ], | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [phpcs] reported by reviewdog 🐶 |
||
| [ 'foo' => fn( $foo ) => $foo === 'hello', 'bar' => fn( $bar ) => $bar === 'world' ], | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [phpcs] reported by reviewdog 🐶 |
||
| true, | ||
| false | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [phpcs] reported by reviewdog 🐶
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [phpcs] reported by reviewdog 🐶 |
||
| ], | ||
| 'non-empty array, closure shape, some key pass, non-strict' => [ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [phpcs] reported by reviewdog 🐶 |
||
| [ 'foo' => 'hello', 'bar' => 89 ], | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [phpcs] reported by reviewdog 🐶 |
||
| [ 'foo' => fn( $foo ) => $foo === 'hello', 'bar' => fn( $bar ) => $bar === 'world' ], | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [phpcs] reported by reviewdog 🐶 |
||
| false, | ||
| false | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [phpcs] reported by reviewdog 🐶
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [phpcs] reported by reviewdog 🐶 |
||
| ], | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * @dataProvider has_shape_data_provider | ||
| */ | ||
| public function test_has_shape( $input, $shape, $strict, $expected ): void { | ||
| $this->assertEquals( $expected, Arr::has_shape( $input, $shape, $strict ) ); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[phpcs] reported by reviewdog 🐶
Universal.NamingConventions.NoReservedKeywordParameterNames.defaultFoundIt is recommended not to use reserved keyword "default" as function parameter name. Found: $default