-
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 all 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; | ||
| } | ||
| } | ||
| } | ||
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