1. Go to this page and download the library: Download tobento/service-message library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use Tobento\Service\Message\Message;
$message = new Message('error', 'Any error message');
<?= $message
use Tobento\Service\Message\Messages;
use Tobento\Service\Message\MessagesInterface;
use Tobento\Service\Message\MessageFactoryInterface;
use Tobento\Service\Message\ModifiersInterface;
use Psr\Log\LoggerInterface;
$messages = new Messages(
messageFactory: null, // null|MessageFactoryInterface
modifiers: null, // null|ModifiersInterface
logger: null, // null|LoggerInterface
);
var_dump($messages instanceof MessagesInterface);
// bool(true)
use Tobento\Service\Message\Messages;
use Tobento\Service\Message\MessageFactory;
use Tobento\Service\Message\MessageFactoryInterface;
$messages = new Messages();
$newMessages = $messages->withMessageFactory(
messageFactory: new MessageFactory()
);
var_dump($newMessages->messageFactory() instanceof MessageFactoryInterface);
// bool(true)
use Tobento\Service\Message\Messages;
use Tobento\Service\Message\Modifiers;
use Tobento\Service\Message\ModifiersInterface;
$messages = new Messages();
$newMessages = $messages->withModifiers(
modifiers: new Modifiers()
);
var_dump($newMessages->modifiers() instanceof ModifiersInterface);
// bool(true)
use Tobento\Service\Message\Messages;
use Psr\Log\LoggerInterface;
$messages = new Messages();
$newMessages = $messages->withLogger(
logger: null // null|LoggerInterface
);
var_dump($newMessages->logger() instanceof LoggerInterface);
// bool(false) as null
use Tobento\Service\Message\MessagesFactory;
use Tobento\Service\Message\MessagesFactoryInterface;
use Tobento\Service\Message\MessageFactoryInterface;
use Tobento\Service\Message\MessagesInterface;
use Tobento\Service\Message\ModifiersInterface;
use Psr\Log\LoggerInterface;
$messagesFactory = new MessagesFactory(
messageFactory: null, // null|MessageFactoryInterface
modifiers: null, // null|ModifiersInterface
logger: null, // null|LoggerInterface
);
var_dump($messagesFactory instanceof MessagesFactoryInterface);
// bool(true)
$messages = $messagesFactory->createMessages();
var_dump($messages instanceof MessagesInterface);
// bool(true)
use Tobento\Service\Message\Messages;
use Tobento\Service\Message\Message;
$messages = new Messages();
$messages->addMessage(
message: new Message('error', 'Error message'),
log: false,
);
use Tobento\Service\Message\Messages;
use Tobento\Service\Message\MessagesInterface;
$someMessages = new Messages();
$someMessages->add('error', 'Error message');
$messages = new Messages();
$messages->add('success', 'Success message');
$messages->push(
messages: $someMessages // array|MessagesInterface
);
$messages->push(
messages: [
['level' => 'info', 'message' => 'Some info'],
]
);
use Tobento\Service\Message\Messages;
use Tobento\Service\Message\MessageInterface;
$someMessages = new Messages();
$someMessages->add('error', 'Error message');
$messages = new Messages();
$messages->add('success', 'Success message');
$newMessages = $messages->withMessage(
...$someMessages // MessageInterface
);
use Tobento\Service\Message\Messages;
use Tobento\Service\Message\MessageInterface;
$messages = new Messages();
$messages->add('success', 'Success message');
$messages->add('error', 'Error message');
$messages = $messages->filter(
fn(MessageInterface $m): bool => $m->level() === 'error'
);
use Tobento\Service\Message\Messages;
$messages = new Messages();
$messages->add(level: 'success', message: 'Success message', key: 'foo');
$messages->add('error', 'Error message');
$messages = $messages->key('foo');
use Tobento\Service\Message\Messages;
$messages = new Messages();
$messages->add('success', 'Success message');
$messages->add('error', 'Error message');
$messages->add('info', 'Info message');
$messages = $messages->only(levels: ['info', 'success']);
use Tobento\Service\Message\Messages;
$messages = new Messages();
$messages->add('success', 'Success message');
$messages->add('error', 'Error message');
$messages->add('info', 'Info message');
$messages = $messages->except(levels: ['info', 'success']);
use Tobento\Service\Message\Messages;
use Tobento\Service\Message\MessageInterface;
$messages = new Messages();
$messages->add('success', 'Success message');
$messages->add('error', 'Error message');
foreach($messages->all() as $message) {
var_dump($message instanceof MessageInterface);
//bool(true)
}
// or just
foreach($messages as $message) {
var_dump($message instanceof MessageInterface);
//bool(true)
}
use Tobento\Service\Message\Messages;
use Tobento\Service\Message\MessageInterface;
$messages = new Messages();
$messages->add('success', 'Success message');
$messages->add('error', 'Error message');
var_dump($messages->first() instanceof MessageInterface);
// bool(true)
use Tobento\Service\Message\Messages;
use Tobento\Service\Message\MessageInterface;
$messages = new Messages();
$messages->add('success', 'Success message');
$messages->add('error', 'Error message');
var_dump($messages->last() instanceof MessageInterface);
// bool(true)
use Tobento\Service\Message\HasMessages;
use Tobento\Service\Message\MessagesAware;
use Tobento\Service\Message\MessagesInterface;
class Foo implements MessagesAware
{
use HasMessages;
}
$foo = new Foo();
var_dump($foo->messages() instanceof MessagesInterface);
// bool(true)
use Tobento\Service\Message\Modifiers;
use Tobento\Service\Message\ModifiersInterface;
use Tobento\Service\Message\Modifier;
$modifiers = new Modifiers(
new Modifier\ParameterReplacer(),
);
var_dump($modifiers instanceof ModifiersInterface);
// bool(true)
use Tobento\Service\Message\Modifiers;
use Tobento\Service\Message\Modifier;
$modifiers = new Modifiers();
$modifiers->add(new Modifier\ParameterReplacer());
use Tobento\Service\Message\Modifiers;
use Tobento\Service\Message\Modifier;
$modifiers = new Modifiers();
$modifiers->add(new Modifier\ParameterReplacer());
$modifiers->prepend(new Modifier\Pluralization());
use Tobento\Service\Message\Modifiers;
use Tobento\Service\Message\Modifier;
use Tobento\Service\Message\Message;
$modifiers = new Modifiers(
new Modifier\ParameterReplacer(),
);
$message = new Message(
level: 'success',
message: 'Hello :name, welcome back',
parameters: [':name' => 'John'],
);
$newMessage = $modifiers->modify($message);
var_dump($newMessage->message());
// string(24) "Hello John, welcome back"
var_dump($newMessage === $message);
// bool(false)
use Tobento\Service\Message\Modifiers;
use Tobento\Service\Message\ModifierInterface;
use Tobento\Service\Message\Modifier;
$modifiers = new Modifiers(
new Modifier\ParameterReplacer(),
);
foreach($modifiers->all() as $modifier) {
var_dump($modifier instanceof ModifierInterface);
// bool(true)
}
use Tobento\Service\Message\Modifier\Pluralization;
use Tobento\Service\Message\ModifierInterface;
use Tobento\Service\Message\Message;
$modifier = new Pluralization(key: 'count');
var_dump($modifier instanceof ModifierInterface);
// bool(true)
$message = new Message(
level: 'success',
message: 'One item created|Many items created',
parameters: ['count' => 5],
);
$newMessage = $modifier->modify($message);
var_dump($newMessage->message());
// string(18) "Many items created"
var_dump($newMessage === $message);
// bool(false)
use Tobento\Service\Message\Modifier\ParameterReplacer;
use Tobento\Service\Message\ModifierInterface;
use Tobento\Service\Message\Message;
$modifier = new ParameterReplacer();
var_dump($modifier instanceof ModifierInterface);
// bool(true)
$message = new Message(
level: 'success',
message: 'Hello :name, welcome back',
parameters: [':name' => 'John'],
);
$newMessage = $modifier->modify($message);
var_dump($newMessage->message());
// string(24) "Hello John, welcome back"
var_dump($newMessage === $message);
// bool(false)
use Tobento\Service\Message\Modifier\LimitLength;
use Tobento\Service\Message\ModifierInterface;
use Tobento\Service\Message\Message;
$modifier = new LimitLength(
length: 15,
parameterKey: 'limit_length',
);
var_dump($modifier instanceof ModifierInterface);
// bool(true)
$message = new Message(
level: 'success',
message: 'Some very long message',
parameters: [
// used instead of default if set.
'limit_length' => 10,
],
);
$newMessage = $modifier->modify($message);
var_dump($newMessage->message());
// string(10) "Some ve..."
var_dump($newMessage === $message);
// bool(false)
use Tobento\Service\Translation;
use Tobento\Service\Message\Modifier\Translator;
use Tobento\Service\Message\ModifierInterface;
use Tobento\Service\Message\Message;
$translator = new Translation\Translator(
new Translation\Resources(
new Translation\Resource('*', 'en', [
'Some error occured' => 'Some error occured',
]),
new Translation\Resource('*', 'de', [
'Some error occured' => 'Ein Fehler is passiert',
]),
),
new Translation\Modifiers(
new Translation\Modifier\Pluralization(),
new Translation\Modifier\ParameterReplacer(),
),
new Translation\MissingTranslationHandler(),
'de',
);
$modifier = new Translator(
translator: $translator,
src: '*',
);
var_dump($modifier instanceof ModifierInterface);
// bool(true)
$message = new Message(
level: 'error',
message: 'Some error occured',
);
$newMessage = $modifier->modify($message);
var_dump($newMessage->message());
// string(22) "Ein Fehler is passiert"
var_dump($newMessage === $message);
// bool(false)
use Tobento\Service\Translation;
use Tobento\Service\Message\Modifier\ParameterTranslator;
use Tobento\Service\Message\ModifierInterface;
use Tobento\Service\Message\Message;
$translator = new Translation\Translator(
new Translation\Resources(
new Translation\Resource('*', 'en', [
'title' => 'title',
]),
new Translation\Resource('*', 'de', [
'title' => 'Titel',
]),
),
new Translation\Modifiers(
new Translation\Modifier\Pluralization(),
new Translation\Modifier\ParameterReplacer(),
),
new Translation\MissingTranslationHandler(),
'de',
);
$modifier = new ParameterTranslator(
parameters: [':attribute'],
translator: $translator,
src: '*',
);
var_dump($modifier instanceof ModifierInterface);
// bool(true)
$message = new Message(
level: 'error',
message: 'The :attribute is invalid.',
parameters: [
':attribute' => 'title',
],
);
$newMessage = $modifier->modify($message);
var_dump($newMessage->parameters()[':attribute']);
// string(5) "Titel"
var_dump($newMessage->message());
// string(26) "The :attribute is invalid."
var_dump($newMessage === $message);
// bool(false)
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.