cakephp/utility
Lightweight CakePHP Utility library providing handy helpers for arrays, text, numbers, hashing, security, and type conversion. A standalone set of utility classes you can use inside or outside CakePHP to simplify common PHP tasks with minimal dependencies.
Installation Add the package via Composer in your Laravel project:
composer require cakephp/utility
No additional configuration is required—it’s a standalone utility library.
First Use Case: Text Manipulation
Use the Text helper for common string operations like truncation, pluralization, or slug generation:
use Cake\Utility\Text;
$truncated = Text::truncate('This is a very long string', 10); // "This is a..."
$slug = Text::slug('Hello World!'); // "hello-world"
Where to Look First
src directory for all available classes.Text Utilities
use Cake\Utility\Text;
use Cake\Utility\Inflector;
$slug = Text::slug('User Profile'); // "user-profile"
$plural = Inflector::pluralize('user'); // "users"
Text::truncate('Lorem ipsum...', 20); // "Lorem ipsum..."
Hash Manipulation
use Cake\Utility\Hash;
$data = ['user' => ['name' => 'John']];
Hash::get($data, 'user.name'); // "John"
Hash::extract($data, '{n}.name'); // ['John']
$merged = Hash::merge(['a' => 1], ['a' => 2, 'b' => 3]); // ['a' => 2, 'b' => 3]
Security
use Cake\Utility\Security;
$clean = Security::escapeHtml('<script>alert(1)</script>'); // <script>...
$stripped = Security::stripTags('<b>Hello</b>'); // "Hello"
XML/JSON Handling
use Cake\Utility\Xml;
$xml = '<root><item>test</item></root>';
$array = Xml::toArray($xml); // ['root' => ['item' => 'test']]
// app/Providers/AppServiceProvider.php
public function register()
{
$this->app->singleton('text', function () {
return new \Cake\Utility\Text();
});
}
Use via dependency injection:
public function __construct(private Text $text) {}
TextFacade).Hash for complex data manipulation).Namespace Conflicts
Cake\Utility\* namespaces. Avoid collisions with Laravel’s Illuminate\Support\Str (e.g., don’t mix Str::slug() and Text::slug()).Array vs. Object Handling
Hash works with arrays by default. For objects, use Hash::extract($obj->toArray()).Hash::get((array) $object, 'property');
XML Parsing Quirks
Xml::toArray() may not handle malformed XML gracefully. Validate input first:
if (!Xml::isWellFormed($xmlString)) {
throw new \InvalidArgumentException('Invalid XML');
}
Security Overhead
Security::escapeHtml() is strict. For HTML emails or rich text, consider DOMPurifier instead.Hash::check() to validate paths before extraction:
if (Hash::check($data, 'user.name')) {
$value = Hash::get($data, 'user.name');
}
Text::truncate() respects UTF-8 characters (test with non-ASCII strings).Hash operations on large arrays can be slow. Cache results if reused:
$cached = Cache::remember("hash_{$key}", 60, fn() => Hash::get($data, $key));
Custom Inflector Rules
Override Inflector behavior by extending the class:
class CustomInflector extends \Cake\Utility\Inflector {
public static function pluralize($string) {
return parent::pluralize($string) . '_custom';
}
}
Use via CustomInflector::pluralize('user').
Security Policies
Extend Security for project-specific rules (e.g., custom allowed tags):
Security::allowTags(['<b>', '<i>', '<custom-tag>']);
XML Schema Validation
Combine with Cake\Utility\Xml and DOMDocument for schema validation:
$dom = new \DOMDocument();
$dom->loadXML($xml);
$schema = $dom->schemaValidate('schema.xsd');
Laravel Blade Directives Create Blade helpers for utilities:
// app/Providers/BladeServiceProvider.php
Blade::directive('slug', function ($expression) {
return "<?php echo \\Cake\\Utility\\Text::slug({$expression}); ?>";
});
Usage:
@slug('Hello World') → "hello-world"
How can I help you explore Laravel packages today?