dragon-code/support
Dragon Code Support provides a lightweight collection of PHP/Laravel helpers, facades, and utility tools for everyday projects. Designed to be easily extended with new methods or classes, with a clear contribution and testing structure.
composer require dragon-code/support
config/app.php under aliases:
'aliases' => [
// ...
'Arr' => DragonCode\Support\Facades\Arr::class,
'Str' => DragonCode\Support\Facades\Str::class,
'File' => DragonCode\Support\Facades\File::class,
'Digit' => DragonCode\Support\Facades\Digit::class,
// ... other facades as needed
],
Arr for array manipulation or Str for string operations in your Laravel controllers/services:
use Arr;
use Str;
$flattened = Arr::flattenKeys(['a' => ['b' => 1]]);
$slug = Str::slug('Hello World');
Arr, Str, File, Digit, Instance, etc.php artisan vendor:publish --tag=support-config if configuration publishing is needed (though unlikely for this package).$array = ['a' => ['b' => 1, 'c' => 2]];
$flattened = Arr::flattenKeys($array);
// Result: ['a.b' => 1, 'a.c' => 2]
$collection = Arr::of(['a', 'b', 'c'])->addUnique('d')->toArray();
// Result: ['a', 'b', 'c', 'd']
$slug = Str::slug('Hello World', '-');
// Result: 'hello-world'
$matches = Str::matchAll('/\d+/', 'Order 123');
// Result: ['123']
File::directory('path/to/dir')->copy('path/to/new/dir');
$content = File::load('path/to/file.txt');
if (Instance::of($var, \DateTime::class)) {
// Handle DateTime
}
$shortNumber = Digit::toShort(1000000, 'M');
// Result: '1M'
Replace Native PHP/Collections:
Use Arr or Str facades instead of raw PHP functions (e.g., array_merge, str_replace) for consistency and potential future extensions.
// Instead of:
$merged = array_merge($array1, $array2);
// Use:
$merged = Arr::merge($array1, $array2);
Leverage Invokable Helpers:
Chain methods on Arr::of() or Str::of() for fluent syntax:
$result = Arr::of($data)
->filter(fn($value) => $value > 0)
->map(fn($value) => $value * 2)
->toArray();
Extend for Custom Logic:
Add new methods to existing classes (e.g., Arr, Str) by:
Testing:
Mock facades in tests using Laravel’s MockFacade or replace them with direct class calls for isolation:
$this->partialMock(Arr::class, ['flattenKeys']);
Configuration: Publish config (if available) with:
php artisan vendor:publish --tag=support-config
(Note: The package may not include config files; check the repo.)
Facade Initialization:
config/app.php before use. If you encounter Class not found errors, verify the alias is correctly added.Class 'Arr' not found → Ensure 'Arr' => DragonCode\Support\Facades\Arr::class is in aliases.Method Signature Mismatches:
Arr::flattenKeys) may behave differently with mixed input types. Refer to the release notes for fixes (e.g., #295).Arr::flattenKeys(['a' => [1, 'b' => 2]]); // May throw errors; test edge cases.
PHP Version Compatibility:
PHP 8.0 not supported → Upgrade PHP or use an older package version.Filesystem Permissions:
File::load() or Directory::copy() may fail silently or throw exceptions if permissions are insufficient. Validate paths and permissions:
if (!is_readable('path/to/file')) {
throw new \RuntimeException('File not readable');
}
Invokable Helper Quirks:
Arr::of()->toInstance() may return unexpected types. Test return values:
$instance = Arr::of(['key' => 'value'])->toInstance();
// Ensure $instance is the expected class (e.g., stdClass).
Log Facade Calls: Temporarily replace facades with direct class calls to debug:
// In tests or debug code:
$result = \DragonCode\Support\Arr::flattenKeys($data);
Check Release Notes:
Enable Strict Typing:
Use PHP’s declare(strict_types=1) to catch type-related issues early.
Test Edge Cases:
null values, or mixed types (e.g., ['a' => [1, 'b']]) may break methods. Write tests for these scenarios.Add Custom Methods:
DragonCode\Support\Arr) in your project:
namespace App\Support;
use DragonCode\Support\Arr as BaseArr;
class Arr extends BaseArr {
public static function customMethod(array $array) {
return array_filter($array, fn($value) => $value > 100);
}
}
config/app.php to point to your extended class.Contribute to the Package:
DragonCode\Support\Str).DragonCode\Support\Facades\Str).Tests/Unit/Str/<MethodName>Test.Override Default Behavior:
AppServiceProvider:
$this->app->bind(
\DragonCode\Support\Arr::class,
fn($app) => new \App\Support\CustomArr()
);
Avoid Overhead in Loops:
// Slow (facade):
foreach ($data as $item) {
$processed = Arr::flattenKeys($item);
}
// Faster (direct):
foreach ($data as $item) {
$processed = \DragonCode\Support\Arr::flattenKeys($item);
}
**Cache Complex Operations
How can I help you explore Laravel packages today?