Download the PHP package asseco-voice/laravel-inbox without Composer
On this page you can find all versions of the php package asseco-voice/laravel-inbox. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download asseco-voice/laravel-inbox
More information about asseco-voice/laravel-inbox
Files in asseco-voice/laravel-inbox
Package laravel-inbox
Short Description Laravel handling incoming communication
License MIT
Informations about the package laravel-inbox
Laravel inbox
Purpose of this package is to provide pattern-matching for incoming communication and executing custom callbacks if they match.
Credits to BeyondCode for the initial codebase. The idea for this package later on substantially diverged from the original, leaving no alternative than to separate it as a new package.
Installation
Require the package with composer require asseco-voice/laravel-inbox
.
Service provider will be registered automatically.
Usage
Interface
Before you start using the package, you need to have a class implementing a CanMatch
interface so that package knows what will it validate regex against.
I.e. if you want to validate against 'from', you need to defined where to fetch that piece of information from.
Inbox API
Inbox
is a class in which you provide regex patterns which you'd like to be
matched before the given callback is executed.
I.e. the callback defined under action()
will execute only if the .*@gmail.com
pattern is matched:
Patterns need to be surrounded within { }
.
from($pattern)
will targetfrom
fieldto($pattern)
will targetto
fieldcc($pattern)
will targetcc
fieldbcc($pattern)
will targetbcc
field-
subject($pattern)
will targetsubject
field -
is created for every other use case you might need. I.e.
from($pattern)
is just a shorthand forsetPattern('from', $pattern)
. -
meta([...])
for adding any other meta-data. -
action(callable)
is a callback to be executed and takes object implementing aCanMatch
interface as an only argument (can be omitted though). -
matchEither()
will act asOR
gate in case more than one pattern is defined. Default behavior is to match all patterns for a callback to execute. -
priority($number)
will set inbox priority which will be taken into account only ifInboxGroup
is used. run(CanMatch $message)
will take an instance of object implementingCanMatch
interface and will return a bool of whether the inbox was hit or not. This method must not be used when using inbox groups. They have their ownrun()
method.
Inbox group API
Should you require multiple cases covered, there is also a higher level
concept - InboxGroup
which acts as a container for multiple inboxes,
having a few fluent API methods as well.
add(Inbox $inbox)
will add an inbox to a groupcontinuousMatching()
will continue matching after a first match is hit. Default behavior is to stop after one inbox is matched.fallback(callable)
will add a (non-mandatory) fallback which will execute if no inbox is hit.run(CanMatch $message)
will take an instance of object implementingCanMatch
interface and will (unlike inboxrun()
method) return an array of matched inboxes. Inboxes will be ran by priority.
More examples
Examples will cover cases using mail, but it can be adapted to any incoming communication.
Matching functions can be used to either provide an exact match (i.e. from('[email protected]')
)
or providing a regex match which needs to be surrounded with curly braces { }
to be interpreted
as such.
Example:
More examples with outcomes:
- having an exact match
from('[email protected]')
:- only mails coming solely from
[email protected]
will be matched.
- only mails coming solely from
- having a partial match
from('{.*}@gmail.com')
:- any gmail address will be matched like
[email protected]
and[email protected]
will be matched, but[email protected]
won't).
- any gmail address will be matched like
- having a partial match
from('your.name@{.*}')
:- same as last example, but this time the name is fixed and provider is flexible.
It would match
[email protected]
,[email protected]
, but it wouldn't match[email protected]
.
- same as last example, but this time the name is fixed and provider is flexible.
It would match
- having a full regex match:
from('{.*}')
:- accepts anything.
Group example:
If you don't want to use groups, but a single inbox, you can call run method on it directly: