php-standard-library/type
Runtime type validation for PHP using “Parse, Don’t Validate”: coerce and assert unstructured input into well-typed data. Useful for APIs, configs, and user input with clear parsing rules, assertions, and predictable failures.
Validator, FormRequest, and custom validation rules, offering a consistent, reusable layer for runtime type checks beyond Laravel’s built-in is_* functions.instanceof/gettype() chains with optimized utility methods in performance-critical paths (e.g., batch processing).Arr::, Str::).Validator::extend() or custom FormRequest rules.is_* checks in services handling dynamic data (e.g., API payloads, config parsing).$casts, $fillable, and accessors already handle type safety.is_fillable()).isNonEmptyArray()) as a fallback.Arr::, Validator) cover 80% of use cases, the package may not justify adoption.is_string() vs. Type::isString()). Mitigate by:
Type::isNonEmptyArray() over is_array()).Type:: for dynamic input, Laravel’s Arr:: for internal data").is_* checks) or add complexity (e.g., another validation layer)?is_array()) or supplement them (e.g., for nullability checks)?Validator::extend())?null handling) where this package outperforms Laravel’s defaults?is_* checks)?symfony/polyfill, webmozart/assert, spatie/array-to-object) that better fit Laravel’s ecosystem?Validator with type-specific rules (e.g., TypeRule::isNonEmptyArray()).Type::assertString($name)).Type::isCallable($handler)).$casts, $fillable) suffices.Pilot Phase (Week 1):
composer require php-standard-library/type
is_* logic with package methods (e.g., Type::isNonEmptyArray()).null, nested arrays) to ensure correctness.Validation Layer Integration (Week 2):
// app/Rules/TypeRule.php
use PhpStandardLibrary\Type\Type;
use Illuminate\Contracts\Validation\Rule;
class TypeRule implements Rule {
public function passes($attribute, $value) {
return Type::isNonEmptyArray($value);
}
}
Validator::extend('non_empty_array', function ($attribute, $value) {
return Type::isNonEmptyArray($value);
});
public function rules() {
return [
'data' => ['required', 'array', new TypeRule],
];
}
Service Layer Adoption (Week 3):
// Before
if (!is_callable($callback) || !is_array($config)) {
throw new InvalidArgumentException();
}
// After
Type::assertCallable($callback);
Type::assertNonEmptyArray($config);
Type:: vs. Laravel’s built-ins.Static Analysis Complement (Ongoing):
// Runtime guard (package)
Type::assertString($name);
// Static check (PHPStan)
// $name is string (from PHPDoc or type hint)
phpunit/phpunit (How can I help you explore Laravel packages today?