Skip to content

Commit

Permalink
Add PHP polyfills
Browse files Browse the repository at this point in the history
  • Loading branch information
aristath committed May 10, 2024
1 parent 915df6f commit 4b0dab5
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
1 change: 1 addition & 0 deletions load.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

define( 'SQLITE_MAIN_FILE', __FILE__ );

require_once __DIR__ . '/php-polyfills.php';
require_once __DIR__ . '/admin-page.php';
require_once __DIR__ . '/activate.php';
require_once __DIR__ . '/deactivate.php';
Expand Down
54 changes: 54 additions & 0 deletions php-polyfills.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
* Polyfills for php 7 & 8 functions
*
* @package wp-sqlite-integration
*/

if ( ! function_exists( 'str_starts_with' ) ) {
/**
* Check if a string starts with a specific substring.
*
* @param string $haystack The string to search in.
* @param string $needle The string to search for.
*
* @see https://www.php.net/manual/en/function.str-starts-with
*
* @return bool
*/
function str_starts_with( string $haystack, string $needle ) {
return empty( $needle ) || 0 === strpos( $haystack, $needle );
}
}

if ( ! function_exists( 'str_contains' ) ) {
/**
* Check if a string contains a specific substring.
*
* @param string $haystack The string to search in.
* @param string $needle The string to search for.
*
* @see https://www.php.net/manual/en/function.str-contains
*
* @return bool
*/
function str_contains( string $haystack, string $needle ) {
return empty( $needle ) || false !== strpos( $haystack, $needle );
}
}

if ( ! function_exists( 'str_ends_with' ) ) {
/**
* Check if a string ends with a specific substring.
*
* @param string $haystack The string to search in.
* @param string $needle The string to search for.
*
* @see https://www.php.net/manual/en/function.str-ends-with
*
* @return bool
*/
function str_ends_with( string $haystack, string $needle ) {
return empty( $needle ) || substr( $haystack, -strlen( $needle ) === $needle );
}
}
2 changes: 1 addition & 1 deletion tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function apply_filters( $tag, $value, ...$args ) {
}

/**
* Polyfills for php 8 functions
* Polyfills for php 7 & 8 functions
*/

if ( ! function_exists( 'str_starts_with' ) ) {
Expand Down

0 comments on commit 4b0dab5

Please sign in to comment.