cocur/vale
cocur/vale is a lightweight PHP value validation library. Define reusable rules to validate strings, numbers, arrays, and objects, and get clear, consistent results without pulling in a full framework—handy for DTOs, APIs, and input sanitization.
Start by installing via Composer (composer require cocur/vale) and importing the Vale class—this package is intentionally minimal, offering just a single static interface. Your first use case is likely safely reading deeply nested data: Vale::get($array, 'user.profile.address.city') returns null if any level is missing, avoiding undefined index errors. Check existence with Vale::exists($data, 'user.settings.theme') before using values—perfect for handling inconsistent API responses or config arrays.
Use Vale::set() to update nested values in-place on arrays or objects (supports dot notation), e.g., Vale::set($config, 'database.connections.mysql.host', '127.0.0.1'). Chain operations like Vale::get() and Vale::exists() in validation logic—e.g., verifying optional nested form fields before saving. For transformations, combine with array_map or closures: Vale::set($data, 'items', array_map('strtolower', Vale::get($data, 'items') ?? [])). Works seamlessly with Laravel’s collect()—extract nested values using Vale for complex structures before pushing data into Eloquent models.
The package hasn’t been updated since 2015—ensure compatibility with PHP 8+ by testing edge cases (e.g., stringable objects or strict scalar comparisons). Vale does not handle arrays of objects automatically: Vale::get($data, 'users.0.name') requires users[0] to be an array; mix with ArrayAccess wrappers for mixed object/array trees. Debug failures by enabling E_ALL & ~E_DEPRECATED—older PHP warnings may surface around create_function() (used internally), though not active in PHP 7.2+. Consider wrapping Vale in a custom helper (e.g., vale_get()) to add logging or fallback logic. If migrating to modern alternatives, evaluate symfony/property-access for richer object graph support—but Vale remains excellent for lightweight, flat dot-notation on arrays.
How can I help you explore Laravel packages today?