Recommend this page to a friend! |
Download |
Info | Example | Files | Install with Composer | Download | Reputation | Support forum | Blog | Links |
Ratings | Unique User Downloads | Download Rankings | ||||
Not enough user ratings | Total: 76 | All time: 10,170 This week: 560 |
Version | License | PHP version | Categories | |||
messagecontainer 1.0.0 | GNU Lesser Genera... | 5 | PHP 5, Data types |
<?php |
It is a Message Container for PHP, similar in functionality MessageBag for Laravel. However this is aimed for speed and usability and it doesn't have any dependency. This class is simple: 2 classes, no dependency and nothing more. You can use in any PHP project.
This library stores messages (strings) in different lockers and each locker could contain different messages with different levels (error, warning, info and success). The goal of this library:
It is an example from where we could use it, the validation of a form (this library does not validate or show values it only stores the information)
In this example, we have :
If we use plain-PHP, we could show some messages of the password:
echo $container['password']['error'];
But what if the id password does not contain any message, or if there is no error? of if there is more than error?
So we could re-define something like that: (but it will still fail if there is more than one error)
if (isset($container['password'])) {
if(isset($container['password']['error'])) {
echo $container['password']['error'];
}
}
And with our library
// We could show the first error (or empty if none):
echo $container->getLocker('password')->firstError();
// it shows all errors (or nothing if none):
foreach($container->getLocker('password')->allError() as $msg) {
echo $msg;
}
use eftec\MessageContainer;
$container=new MessageContainer(); // we create the full lockers
$container->addItem('locker1','It is a message','warning'); // we store a message inside "id1"
$container->addItem('locker1','It is a message','error'); // we store another message inside "id1"
// And later, you can process it
$lastErrorsOrWarnings=$container->get('locker1')->allErrorOrWarning();
// it will not crash even if the locker2 does not exists.
$lastErrorsOrWarnings2=$container->get('locker2')->allErrorOrWarning();
Lets say the next example of container that shows every part of the Container.
We have 3 levels of spaces.
Messages are leveled as follows
| id | Description | Example | | ------- | ------------------------------------------------------------ | ----------------------------------------- | | error | The message is an error, and it must be solved. It is our show stopper. | Database is down | | warning | The message is a warning that maybe it could be ignored. However, the class MessageContainer could allow to group Error and Warning as the same. | The registry was stored but with warnings | | info | The message is information. For example, to log or debug an operation. | Log is stored | | success | The message is a successful operation | Order Accepted |
Example:
$container=new MessageContainer();
$container->addItem('id1','some msg 1','error');
$container->addItem('id1','some msg 2','error');
$container->addItem('id1','some msg 1','warning');
$container->addItem('id2','some msg 1','info');
$container->addItem('id2','some msg 1','success');
$container->addItem('id33','some msg 1','error');
$container->addItem('id33','some msg 2','error');
$container->addItem('id33','some msg 1','success');
$container->addItem('id33','some msg 2','success');
$container->addItem('id33','some msg 2','success');
// obtaining information per locker
$msg=$container->getLocker('id1')->firstErrorOrWarning(); // returns if the locker id1 has an error or warning
$msg2=$container->getLocker('id2')->allInfo(); // returns all info store in locker id2 ["some msg1","some msg2"]
$msg3=$container->getLocker('id3')->allInfo(); // (note this locker is not defined so it returns an empty array.
$msg4=$container->getLocker('id33')->hasError(); // returns true if there is an error.
$msg5=$container->getLocker('id33')->countError(); // returns the number of errors (or zero if none).
// obtaining information globally (all lockers)
$msg7=$container->hasError(); // returns true if there is an error in any locker.
$msg8=$container->allErrorArray(true); // returns all errors and warnings presents in any locker.
To add a new message inside a locker, we use the method addItem()
$container->addItem(<idlocker>,<message>,<level>,<context array>);
Where
// without context:
$container->addItem('locker1','The variable price must be higher than 200','warning');
// with context:
// The variable price must be higher than 200
$container->addItem('locker2'
,'The variable {{var1}} must be higher than {{var2}}'
,'warning'
,['var1'=>'price','var2'=>200]);
// The variable price must be higher than 200 (not 500, the context is not updated this second time)
$container->addItem('locker2'
,'The variable {{var1}} must be higher than {{var2}}'
,'warning'
,['var1'=>'price','var2'=>500]);
// The variable price must be higher than 200 (we use the previous context)
$container->addItem('locker2'
,'The variable {{var1}} must be higher than {{var2}}'
,'warning');
> Note: We could add one or many messages to a locker. In the later example, the locker locker2 stores 3 messages. > > Note: The message is evaluated when we call the method addItem()
MessageContainer stores a list of lockers of messages. It's aimed at convenience, so it features many methods to access the information in different ways.
Messages are ranked as follows
| id | Description | Example | | ------- | ------------------------------------------------------------ | ----------------------------------------- | | error | The message is an error, and it must be solved. It is a show stopper. | Database is down | | warning | The message is a warning that maybe it could be ignored. | The registry was stored but with warnings | | info | The message is information | Log is stored | | success | The message is a successful operation | Order Accepted |
Sometimes, both errors are warning are considered as equals. So the system allows reading an error or warning.
Error always has the priority, then warning, info and success. If you want to read the first message, then it starts searching for errors.
You can obtain a message as an array of objects of the type MessageLocker, as an array of string, or as a single string (first message)
$container->get('idfield'); // container idfield
$container->get('idfield2'); // container idfield2
if($container->hasError()) {
// Error: we do something here.
echo "we found ".$container->errorCount()." errors in all lockers";
}
// using messageList
if($container->hasError()) {
// Error: we do something here.
echo "we found ".$container->errorcount." errors in all lockers";
}
| Name of the field | Type | Description | | ----------------- | ---- | --------------------------------------------------- | | $errorCount | int | Get the number of errors in all lockers | | $warningCount | int | Get the number of warnings in all lockers | | $errorOrWarning | int | Get the number of errors or warnings in all lockers | | $infoCount | int | Get the number of information messages. | | $successCount | int | Get the number of success messages. |
Example:
if ($container->errorcount>0) {
// some error
}
| Name | Type | Description | Example of result | | ------------------ | ------ | ------------------------------------------------------------ | ----------------------------------------- | | firstErrorText() | method | Returns the first message of error of all lockers | "Error in field" | | firstWarningText() | method | Returns the first message of warning of all lockers | "warning in field" | | firstInfoText() | method | Returns the first message of info of all lockers | "info: log" | | firstSuccessText() | method | Returns the first message of success of all lockers | "Operation successful" | | allError() | method | Returns all errors of all lockers (as an array of objects of the type MessageLocker) | MessageLocker[] | | allWarning() | method | Returns all warning of all lockers (as an array of objects of the type MessageLocker) | MessageLocker[] | | allInfo() | method | Returns all info of all lockers (as an array of objects of the type MessageLocker) | MessageLocker[] | | allSuccess() | method | Returns all success of all lockers (as an array of objects of the type MessageLocker) | MessageLocker[] | | allErrorArray() | method | Returns all errors of all lockers (as an array of texts) | ["Error in field1","Error in field2"] | | allWarningArray() | method | Returns all warning of all lockers (as an array of texts) | ["Warning in field1","Warning in field2"] | | allInfoArray() | method | Returns all info of all lockers (as an array of texts) | ["Info in field1","Info in field2"] | | allSuccessArray | method | Returns all success of all lockers (as an array of texts) | ["Info in field1","Info in field2"] |
echo $container->firstErrorText(); // returns first error if any
$array=$container->allError(); // MessageLocker[]
echo $array[0]->firstError();
$array=$container->allErrorArray(); // string[]
echo $array[0];
It is possible to obtain a CSS class based in the current level or state of a container.
$css=$this-messageList->cssClasses('container1');
| Name | Type | Description | | ---------- | ------ | ------------------------------------------------------------ | | $items | field | We get all lockers (array of the type MessageLocker). Each container could contain many messages. | | resetAll() | method | $array=$this-messageList->items; $this-messageList->items['id'];Delete all lockers and reset counters | | addItem() | method | It adds a new message to a container | | allIds() | method | Get all the id of the lockers | | get() | method | Get a container (as an object of the type MessageLocker). You can also use items[] | | hasError() | method | Returns true if there is an error. |
echo $container->resetAll(); // resets all lockers
$container->addItem('containerid','it is a message','error'); // we add an error in the container with #id containerid
$array=$container->allIds(); // ['containerid']
var_dump($validation->get('containerid')); // object MessageLocker
$array=$this-messageList->items;
var_dump($this-messageList->items['containerid']); // object MessageLocker
if($container->hasError()) { // $validation->hasError() does the same
echo "there is an error";
}
Inside MessageContainer we could have one or many lockers( MessageLocker ).
| Name | Type | Description | Example of result | | ------------------ | ------ | --------------------------------------------------------- | ----------------------------------------- | | firstErrorText() | method | Returns the first message of error of a container | "Error in field" | | firstWarningText() | method | Returns the first message of warning of a container | "warning in field" | | firstInfoText() | method | Returns the first message of info of a container | "info: log" | | firstSuccessText() | method | Returns the first message of success of a container | "Operation successful" | | allError() | method | Returns all errors of a container (as an array of texts) | ["Error in field1","Error in field2"] | | allWarning() | method | Returns all warning of a container (as an array of texts) | ["Warning in field1","Warning in field2"] | | allInfo() | method | Returns all info of a container (as an array of texts) | ["Info in field1","Info in field2"] | | allSuccess() | method | Returns all success of a container (as an array of texts) | ["Info in field1","Info in field2"] |
$container->get('idfield'); // container idfield
echo $container->firstErrorText(); // we show the first error (if any) in the container
var_dump($container->allError); // we show the all errors
Class MessageList
Array of containers
Number of errors stored globally
Number of warnings stored globally
Number of errors or warning stored globally
Number of information stored globally
Number of success stored globally
Used to convert a type of message to a css class
MessageList constructor.
It resets all the container and flush all the results.
You could add a message (including errors,warning..) and store it in a $idLocker
It obtains all the ids for all the lockers.
Alias of $this->getMessage()
It returns a MessageLocker containing an locker.<br> <b>If the locker doesn't exist then it returns an empty object (not null)</b>
It returns a css class associated with the type of errors inside a locker<br> If the locker contains more than one message, then it uses the most severe one (error,warning,etc.)<br> The method uses the field <b>$this->cssClasses</b>, so you can change the CSS classes. <pre> $this->clsssClasses=['error'=>'class-red','warning'=>'class-yellow','info'=>'class-green','success'=>'class-blue']; $css=$this->cssClass('customerId'); </pre>
It returns the first message of error or empty if none<br> If not, then it returns the first message of warning or empty if none
It returns the first message of error or empty if none
It returns the first message of warning or empty if none
It returns the first message of information or empty if none
It returns the first message of success or empty if none
It returns an array with all messages of any type of all lockers
It returns an array with all messages of error of all lockers.
It returns an array with all messages of warning of all lockers.
It returns an array with all messages of errors and warnings of all lockers.
It returns an array with all messages of info of all lockers.
It returns an array with all messages of success of all lockers.
It returns an associative array of the form <br> <pre> [ ['id'=>'', // id of the locker 'level'=>'' // level of message (error, warning, info or success) 'msg'=>'' // the message to show ] ] </pre>
It returns true if there is an error (or error and warning).
Class MessageLocker
MessageLocker constructor.
We set the context only if the current context is null.
It adds an error to the locker.
Replaces all variables defined between {{ }} by a variable inside the dictionary of values.<br> Example:<br> replaceCurlyVariable('hello={{var}}',['var'=>'world']) // hello=world<br> replaceCurlyVariable('hello={{var}}',['varx'=>'world']) // hello=<br> replaceCurlyVariable('hello={{var}}',['varx'=>'world'],true) // hello={{var}}<br>
It adds a warning to the locker.
It adds an information to the locker.
It adds a success to the locker.
It returns the number of errors or warnings contained in the locker
It returns the number of errors contained in the locker
It returns the number of warnings contained in the locker
It returns the number of infos contained in the locker
It returns the number of successes contained in the locker
It returns the first message of any kind.<br> If error then it returns the first message of error<br> If not, if warning then it returns the first message of warning<br> If not, then it show the first info message (if any)<br> If not, then it shows the first success message (if any)<br> If not, then it shows the default message.
It returns the first message of error, if any. Otherwise it returns the default value
It returns the first message of warning, if any. Otherwise it returns the default value
It returns the first message of error or warning (in this order), if any. Otherwise it returns the default value
It returns the first message of info, if any. Otherwise it returns the default value
It returns the first message of success, if any. Otherwise it returns the default value
Returns all messages or an empty array if none.
Returns all messages of errors (as an array of string), or an empty array if none.
Returns all messages of warning, or an empty array if none.
Returns all messages of errors or warnings, or an empty array if none
Returns all messages of info, or an empty array if none.
Returns all messages of success, or an empty array if none.
It returns an associative array of the form:<br> <pre> [ ['id'=>'', // id of the locker 'level'=>'' // level of message (error, warning, info or success) 'msg'=>'' // the message to show ] ] </pre>
It returns true if there is an error (or error and warning).
Files (12) |
File | Role | Description | ||
---|---|---|---|---|
docs (3 files) | ||||
examples (1 file) | ||||
lib (2 files) | ||||
tests (2 files) | ||||
composer.json | Data | Auxiliary data | ||
LICENSE | Lic. | License text | ||
phpunit.xml | Data | Auxiliary data | ||
README.md | Doc. | Documentation |
Files (12) | / | docs |
File | Role | Description |
---|---|---|
diagram.drawio | Data | Auxiliary data |
img1.png | Icon | Icon image |
validation_example.png | Icon | Icon image |
Files (12) | / | lib |
File | Role | Description |
---|---|---|
MessageContainer.php | Class | Class source |
MessageLocker.php | Class | Class source |
Files (12) | / | tests |
File | Role | Description |
---|---|---|
bootstrap.php | Aux. | Auxiliary script |
MessageContainerTest.php | Class | Class source |
The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page. |
Install with Composer |
Version Control | Unique User Downloads | Download Rankings | |||||||||||||||
100% |
|
|
Applications that use this package |
If you know an application of this package, send a message to the author to add a link here.