Helpers
General helpers for development.
Array
data_get
The data_get
helper retrieves a value from an array or object using "dot"
notation. If the specified key does not exist, it will return null
or a
default value if provided.
use function Mantle\Support\Helpers\data_get;
$data = [
'user' => [
'name' => 'John Doe',
'email' => 'example@example.com'
]
];
$email = data_get( $data, 'user.email' ); // returns 'example@example.com
data_set
The data_set
helper sets a value in an array or object using "dot" notation.
If the specified key does not exist, it will be created.
use function Mantle\Support\Helpers\data_set;
$data = [
'user' => [
'name' => 'John Doe',
],
];
data_set( $data, 'user.email', 'example@example.com' );
data_fill
The data_fill
helper is used to set a value in an array or object using "dot"
notation. If the specified key does not exist, it not be created.
use function Mantle\Support\Helpers\data_fill;
$data = [
'user' => [
'name' => 'John Doe',
],
];
data_fill( $data, 'user.email', 'example@example.com' );
head
The head
helper retrieves the first element of an array. If the array is
empty, it will return null
.
use function Mantle\Support\Helpers\head;
$array = [ 'apple', 'banana', 'cherry' ];
$first = head( $array ); // returns 'apple'
last
The last
helper retrieves the last element of an array. If the array is
empty, it will return null
.
use function Mantle\Support\Helpers\last;
$array = [ 'apple', 'banana', 'cherry' ];
$last = last( $array ); // returns 'cherry'
value
The value
helper retrieves the value of a variable or calls a callback if
the variable is a closure. This is useful for lazy evaluation.
use function Mantle\Support\Helpers\value;
$value = value( 'Hello, World!' ); // returns 'Hello, World!'
$value = value( fn () => { return 'Hello, World!'; } ); // returns 'Hello, World!'
Core Objects
get_comment_object
Nullable wrapper for get_comment()
.
use function Mantle\Support\Helpers\get_comment_object;
$comment = get_comment_object( 1 ); // returns a comment object or null if not found
get_post_object
Nullable wrapper for get_post()
.
use function Mantle\Support\Helpers\get_post_object;
$post = get_post_object( 1 ); // returns a post object or null if not found
get_site_object
Nullable wrapper for get_site()
.
use function Mantle\Support\Helpers\get_site_object;
$site = get_site_object( 1 ); // returns a site object or null if not found
get_term_object
Nullable wrapper for get_term()
.
use function Mantle\Support\Helpers\get_term_object;
$term = get_term_object( 1 ); // returns a term object or null if not found
get_term_object_by
Nullable wrapper for get_term_by()
.
use function Mantle\Support\Helpers\get_term_object_by;
$term = get_term_object_by( 'slug', 'example-slug', 'category' ); // returns a term object or null if not found
get_user_object
Nullable wrapper for get_user()
.
use function Mantle\Support\Helpers\get_user_object;
$user = get_user_object( 1 ); // returns a user object or null if not found
get_user_object_by
Nullable wrapper for get_user_by()
.
use function Mantle\Support\Helpers\get_user_object_by;
$user = get_user_object_by( 'login', 'exampleuser' ); // returns a user object or null if not found
Environment
is_hosted_env
The is_hosted_env
helper checks if the application is running in a hosted
environment. Checks if the site is a production
environment.
use function Mantle\Support\Helpers\is_hosted_env;
if ( is_hosted_env() ) {
// The application is running in a hosted environment
}
is_local_env
The is_local_env
helper checks if the application is running in a local
environment. Checks if the site is a local
environment.
use function Mantle\Support\Helpers\is_local_env;
if ( is_local_env() ) {
// The application is running in a local environment
}
is_wp_cli
The is_wp_cli
helper checks if the application is running in the WP-CLI
context. This is useful for determining if the code is being executed
through the command line interface.
use function Mantle\Support\Helpers\is_wp_cli;
if ( is_wp_cli() ) {
// The application is running in the WP-CLI context
}
is_unit_testing
The is_unit_testing
helper checks if the application is running in a unit
testing environment via the testing framework.
use function Mantle\Support\Helpers\is_unit_testing;
if ( is_unit_testing() ) {
// The application is running in a unit testing environment
dump( 'Unit testing is enabled!' );
}
General
backtickit
The backtickit
helper is a simple utility to wrap a string in backticks (```).
use function Mantle\Support\Helpers\backtickit;
$wrapped = backtickit( 'example' ); // returns '`example`'
blank
The blank
helper checks if a value is "blank". A value is considered blank
if it is null
, an empty string, or an empty array.
use function Mantle\Support\Helpers\blank;
blank( null ); // returns true
blank( '' ); // returns true
blank( [] ); // returns true
blank( 'example' ); // returns false
class_basename
The class_basename
helper retrieves the class name of a given object or
class name without the namespace.
use function Mantle\Support\Helpers\class_basename;
class_basename( \Mantle\Support\Helpers\ExampleClass::class ); // returns 'ExampleClass'
class_uses_recursive
The class_uses_recursive
helper returns all traits used by a class, its parent
classes and trait of their traits.
use function Mantle\Support\Helpers\class_uses_recursive;
class_uses_recursive( \Mantle\Support\Helpers\ExampleClass::class ); // returns an array of trait names
classname
See Classname;
collect
Returns a Collection instance containing the given value.
use function Mantle\Support\Helpers\collect;
$collection = collect( [ 1, 2, 3 ] ); // returns a Collection instance
defer
The defer()
helper function can be used to defer the execution of a function
until the end of the request lifecycle. This can be useful for deferring
functions that should be executed after the response has been sent to the user.
use function Mantle\Support\Helpers\defer;
defer( function() {
// Your deferred function code here.
} );
filled
The filled
helper checks if a value is "filled". A value is considered
filled if it is not null
, an empty string, or an empty array.
use function Mantle\Support\Helpers\filled;
filled( null ); // returns false
filled( '' ); // returns false
filled( [] ); // returns false
filled( 'example' ); // returns true
get_callable_fqn
The get_callable_fqn
helper retrieves the fully qualified name of a callable
or a closure. This is useful for debugging or logging purposes.
html_string
Returns a HTML instance containing the given value.
use function Mantle\Support\Helpers\html_string;
$html = html_string( '<div id="test">Hello World</div>' );
object_get
The object_get
helper retrieves a value from an object using "dot" notation.
If the specified key does not exist, it will return null
or a default value if
provided.
use function Mantle\Support\Helpers\object_get;
$object = (object) [
'user' => (object) [
'name' => 'John Doe',
]
];
$name = object_get( $object, 'user.name' ); // returns 'John Doe'
retry
The retry
helper attempts to execute a given callback a specified number of
times until it succeeds or the maximum number of attempts is reached. If the
callback fails, it will wait for a specified delay before retrying.
use function Mantle\Support\Helpers\retry;
retry(
times: 5
callback: function() {
// Your code that may fail
},
sleep: 100 // milliseconds
stringable
Returns an instance of Mantle\Support\Str
which contains all the Laravel
string helper methods you might be familiar with. You can reference those
here.
use function Mantle\Support\Helpers\stringable;
stringable( 'example string' )->title(); // Example String
tap
The tap
helper allows you to pass a value to a callback and then return the
original value. This is useful for performing side effects on a value without
modifying it.
use function Mantle\Support\Helpers\tap;
$value = tap(
new Example(),
function ( Example $example ) {
$example->doSomething();
},
);
throw_if
The throw_if
helper throws an exception if the given condition passes. This
is useful for validating conditions before executing code.
use function Mantle\Support\Helpers\throw_if;
throw_if( $value_to_check, \Exception::class, 'Invalid value!' );
throw_unless
The throw_unless
helper throws an exception if the given condition fails.
This is useful for validating conditions before executing code.
use function Mantle\Support\Helpers\throw_unless;
throw_unless( $value_to_check, \Exception::class, 'Invalid value!' );
trait_uses_recursive
The trait_uses_recursive
helper returns all traits used by a trait and its traits.
use function Mantle\Support\Helpers\trait_uses_recursive;
trait_uses_recursive( \Mantle\Support\Helpers\ExampleTrait::class ); // returns an array of trait names
with
The with
helper returns the given value, optionally passed through the given callback.
use function Mantle\Support\Helpers\with;
$value = with( 'example', function( $value ) {
return strtoupper( $value );
} ); // returns 'EXAMPLE'
Testing
capture
The capture()
helper is a simple wrapper around ob_start()
and ob_get_clean()
. It captures the output of a callable and returns it as a string. This can be useful when you want to test the output of a function or method that echoes content. Example:
use function Mantle\Support\Helpers\capture;
$output = capture( fn () => the_title() );
block_factory
The block_factory()
helper creates a new instance of a block factory. This can be useful for testing or creating blocks dynamically.
For more information see the documentation.
use function Mantle\Testing\block_factory;
$block = block_factory()->block(
name: 'example/block-name',
attributes: [ 'example' => 'value' ],
);
$heading = block_factory()->heading( 'Example Heading' );
$paragraph = block_factory()->paragraph( 'Example Paragraph' );