- How do I install ecommit/scalar-values in a Laravel project?
- Run `composer require ecommit/scalar-values` in your project root. The package has no Laravel-specific dependencies and integrates seamlessly with Composer’s autoloader. No additional configuration is required for basic usage.
- Does this package work with Laravel’s built-in validation (e.g., Form Requests)?
- Yes. Use `ScalarValues::containsOnlyScalarValues()` in `authorize()` or custom validation rules to reject non-scalar inputs before Laravel’s validator runs. Example: `if (!ScalarValues::containsOnlyScalarValues($request->all())) { abort(422); }`
- Will this break if my array contains nested arrays or objects?
- No—this package only checks root-level scalars. Nested arrays or objects will cause `containsOnlyScalarValues()` to return `false`, while `filterScalarValues()` removes non-scalar root items (e.g., `['a', ['b']]` becomes `['a']`). For deep validation, chain with `array_walk_recursive()` or Laravel’s `collect()`.
- Is this package compatible with Laravel 10/11, or only older versions?
- The package targets PHP 8.0+, so it works with Laravel 9+ (including 10/11). However, test edge cases like `Stringable` objects or `null` values, as Laravel’s type system may handle them differently than PHP’s `is_scalar()`.
- Can I use this in API middleware to sanitize incoming request data?
- Absolutely. Add middleware to filter non-scalar values before validation: `public function handle($request, Closure $next) { $request->merge(ScalarValues::filterScalarValues($request->all())); return $next($request); }`. This ensures clean data reaches Laravel’s validator.
- What happens if my array contains `false`, `true`, or `null`?
- These are treated as scalars (PHP’s `is_scalar()` includes them). However, if you need to exclude `false`/`true` (e.g., for strict numeric validation), use `is_numeric()` or custom logic post-filtering, as this package follows PHP’s native scalar definition.
- Are there performance concerns for large arrays (e.g., bulk API imports)?
- Minimal overhead for most cases, but `filterScalarValues()` iterates the array once. For high-throughput scenarios (e.g., 10K+ items), benchmark with `microtime(true)` or consider Laravel’s `collect()->filter()` alternative, which may optimize better in some cases.
- What are the alternatives to this package in Laravel?
- For simple cases, use Laravel’s `collect($array)->filter(fn($item) => is_scalar($item))->values()->all()`. For validation, combine with `Validator::make($array, ['*', 'scalar'])` or custom rules. This package shines for pre-validation sanitization or non-Laravel PHP apps.
- How do I handle custom objects or resources that *look* like scalars?
- By default, objects/resources are non-scalar and filtered out. To include them, extend the package or pre-process with `is_object($item) ? (string)$item : $item` (if you cast objects to strings). Note: This may conflict with Laravel’s type expectations (e.g., `DateTime` objects).
- Is this package actively maintained? Should I use it in production?
- The package has no recent commits or stars, so treat it as a lightweight utility with caveats. For production, add a fallback (e.g., `try-catch` around `ScalarValues::filterScalarValues()`) or fork it if critical. Test thoroughly with your data types.