Download the PHP package kodooy/name-value-object without Composer
On this page you can find all versions of the php package kodooy/name-value-object. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download kodooy/name-value-object
More information about kodooy/name-value-object
Files in kodooy/name-value-object
Package name-value-object
Short Description A Laravel package for handling name value objects and database casting
License MIT
Informations about the package name-value-object
Laravel Name Value Object Package
A Laravel package for handling name value objects and database casting.
Requirements
- PHP 8.2 or higher
- Laravel 10.0, 11.0, or 12.0
Installation
Usage
Database Casting
Your database table should have first_name and last_name columns:
Add the cast to your Eloquent model:
Usage:
In controller:
The name is automatically cast when retrieved:
API Reference
Name Value Object
The Name value object provides the following methods:
Constructor
Creates a new Name instance. Both parameters are required and cannot be empty.
- Parameters:
$firstName: The first name (will be trimmed and capitalized)$lastName: The last name (will be trimmed and capitalized)
- Throws:
InvalidArgumentExceptionif either name is empty
Basic Access Methods
Returns the first name.
Returns the last name.
Returns the full name (first name + space + last name).
Formatting Methods
Returns the initials with optional separator.
- Examples:
initials()→ "JD",initials('.')→ "J.D.",initials('-')→ "J-D"
Returns the monogram format with periods.
- Example: "J.D."
Returns the name in formal format (Last, First).
- Example: "Doe, John"
Returns the casual format (First + Last initial).
- Example: "John D."
Returns the abbreviated format (First initial + Last name).
- Example: "J. Doe"
Returns the reversed format (Last + First).
- Example: "Doe John"
Variation Methods
Returns the name with a prefix.
- Examples:
withPrefix('Dr.')→ "Dr. John Doe",withPrefix('Mr.')→ "Mr. John Doe"
Returns the name with a suffix.
- Examples:
withSuffix('Jr.')→ "John Doe Jr.",withSuffix('III')→ "John Doe III"
Privacy Methods
Returns a masked version of the name.
- Parameters:
$visibleChars: Number of visible characters from the start (default: 1)$maskChar: Character to use for masking (default: '*')
- Example:
masked()→ "J D",masked(2, '#')→ "Jo## Do#"
Returns a SHA256 hash of the full name for privacy.
- Example: "a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3"
Comparison Methods
Checks if this name is equal to another Name instance.
- Performs case-sensitive comparison of both first and last names
Data Conversion Methods
Returns an array representation containing:
first_name: The first namelast_name: The last namefull_name: The full nameinitials: The initials
Returns the full name. Allows the object to be used as a string.
- Example:
echo $name; // "John Doe"
Static Factory Methods
Creates a Name instance from an array.
- Accepted keys:
first_name/firstnameandlast_name/lastname - Throws:
InvalidArgumentExceptionif required keys are missing
Creates a Name instance from a full name string.
- Parameter:
$fullName- Must contain exactly two words separated by space - Throws:
InvalidArgumentExceptionif format is invalid - Example:
fromFullName('John Doe')→ Name('John', 'Doe')
Creates a Name instance from an email address.
- Parameter:
$email- Must be in format "[email protected]" - Throws:
InvalidArgumentExceptionif email format is invalid - Example:
fromEmail('[email protected]')→ Name('John', 'Doe')
Generates a random Name instance from predefined lists.
- Returns a Name with randomly selected first and last names
Usage Examples
Error Handling
Invalid Data Handling
The Name value object validates input and throws exceptions for invalid data:
Controller Validation
Proper validation in your controller prevents exceptions:
Cast Error Handling
When using the cast with invalid data types:
Testing
License
The MIT License (MIT). Please see License File for more information.
All versions of name-value-object with dependencies
illuminate/contracts Version ^10.0|^11.0|^12.0
illuminate/database Version ^10.0|^11.0|^12.0