Class Errors
An error collector.
- ICanBoogie\Errors implements ArrayAccess, Countable, Iterator
Methods summary
public
offsetExists( $attribute
)
: boolean
Checks if an error is defined for an attribute.
Checks if an error is defined for an attribute.
Example:
$e = new Errors(); $e['username'] = 'Funny username'; var_dump(isset($e['username']); #=> true var_dump(isset($e['password']); #=> false
Returns
true if an error is defined for the specified attribute, false otherwise.
Implementation of
ArrayAccess::offsetExists()
public
offsetGet( string|null $attribute
)
: string|array|null
Returns error messages.
Returns error messages.
Example:
$e = new Errors(); var_dump($e['password']); #=> null $e['password'] = 'Invalid password'; var_dump($e['password']); #=> 'Invalid password' $e['password'] = 'Ugly password'; var_dump($e['password']); #=> array('Invalid password', 'Ugly password')
Parameters
$attribute
- The attribute that caused the error, or null if the error is global.
Returns
Return the global error messages or the error messages attached to an attribute. If there is only one message a string is returned, otherwise an array with all the messages is returned. null is returned if there is no message defined.
Implementation of
ArrayAccess::offsetGet()
public
offsetSet( string|null $attribute
, string $message
)
Adds an error message.
Adds an error message.
Example:
$e = new Errors(); $e['password'] = 'Invalid password'; $e[] = 'Requires authentication';
Parameters
$attribute
If null, the message is considered as a general error message instead of an attribute message.
$message
- The error message.
Implementation of
ArrayAccess::offsetSet()
public
offsetUnset( string|null $attribute
)
Removes error messages.
Removes error messages.
Parameters
$attribute
If null, general message are removed, otherwise the message attached to the attribute are removed.
Implementation of
ArrayAccess::offsetUnset()
public
count( void )
Returns the number of errors defined.
Returns the number of errors defined.
Example:
$e = new Errors(); $e['username'] = 'Funny user name'; $e['password'] = 'Weak password'; $e['password'] = 'should have at least one digit'; count($e); #=> 3
Implementation of
Countable::count()
public
each( mixed $callback
)
Iterates through errors using the specified callback.
Iterates through errors using the specified callback.
Example:
$e = new Errors(); $e['username'] = 'Funny user name'; $e['password'] = 'Weak password'; $e->each(function($attribute, $message) { echo "$attribute => $message<br />"; });
Parameters
$callback
public
format( string $pattern
, array $args
= array(), array $options
= array() )
: mixed
Formats the given string by replacing placeholders with the values provided.
Formats the given string by replacing placeholders with the values provided.
Parameters
$pattern
- The format pattern.
$args
- An array of replacements for the placeholders.
$options
- Options for the formatter.
Returns
A string or a stringyfiable object.
See
\ICanBoogie\FormattedString
\ICanBoogie\format
public
add( string $id
, string $pattern
, array $args
= array(), array $options
= array() )
: string
Formats and add an error message.
Formats and add an error message.
Parameters
$id
$pattern
- The format pattern.
$args
- An array of replacements for the placeholders.
$options
- Options for the formatter.
Returns
The formatted message.