Download the PHP package shayanderson/exo without Composer
On this page you can find all versions of the php package shayanderson/exo. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package exo
Exo // Next-Gen Eco Framework
Classes
- App
- Response
- Entity
- Event
- Exception
- Factory
- Singleton
- Logger
- Map
- Model
- Options
- Share
- Validator
Functions
- token()
Classes
Exo\App\Cli
Cli
is a CLI helper. The Exo\Factory::cli()
will only return a Exo\App\Cli
object if used with the CLI, all non-CLI usage will return null
.
These examples use the
Exo\Factory
helper functionapp()
.
By default the SCRIPT
and COMMAND
keys are automatically set based on the php [SCRIPT] [COMMAND] ...options
pattern. This default pattern can be changed by using the map()
method, example using the same command in the example above:
Output
A CLI output helper can be used like:
Arrays are supported:
Output messages on the same line:
An output buffer can be used:
Output using color:
Output using indent:
Using other methods (like colors and indent) with the prepend()
method:
Confirm
Confirm example:
Methods
confirm(string $allow): bool
- confirm methodget(string $key)
- gettergetArgs(): array
- get all argsgetMap(): array
- args map getterhas(string $key): bool
- check if key existsmap(array $map)
- args map setteroutput($message): \Exo\App\Cli\Output
- output helperOutput Methods
buffer(): array
- buffer getterenableBuffering()
- enable bufferingline()
- print empty lineindent($message = null)
- indentoutput($message)
- print messageprepend($message)
- print without newlineOutput Color Methods
Color methods are:
colorBlue()
,colorCyan()
,colorGray()
,colorGreen()
,colorMagenta()
,colorRed()
,colorYellow()
Light color methods are: colorLightBlue()
, colorLightCyan()
, colorLightGray()
, colorLightGreen()
, colorLightMagenta()
, colorLightRed()
, colorLightYellow()
Exo\App\Env
Env
is an application environment variables helper. Keys are case-sensitive.
This example uses the helper function
app()
.
Example .env
file:
Example usage:
PHP environment variables from $_ENV
(prefixed with ENV.
) and $_SERVER
(prefixed with SERVER.
) are also accessible, example:
Methods
fromArray(array $array)
- from array setterget(string $key, $defaultValue = null, bool $invalidKeyException = false)
- getterhas(string $key): bool
- check if key existsload(string $path)
- load filetoArray(array $filter = null): array
- to array
Exo\App\Http\Request
Request
is an HTTP request helper.
These examples use the
Exo\Factory
helper functionapp()
.
Example POST
request:
Example GET
request:
Session in request example:
Session flash can be used to store short-term data where the data is available from when set through the following request, example:
Cookie in request example:
Methods
body(bool $convertHtmlEntities = true): string
- request body raw data gettercontentType(): string
- content-type gettercookie(string $key, $default = null): Request\Cookie
- cookie input object getterhasHeader(string $key): bool
- check if header key existsheader(string $key): string
- header value getterheaders(): array
- get all headershost(): string
- HTTP host value getter, ex:www.example.com
input(string $key, $default = null): Request\Input
- input (POST) object getteripAddress(): string
- IP address getterisContentType(string $contentType): bool
- validate request content-typeisMethod(string $method): bool
- validate request methodisSecure(): bool
- check if request is secure (HTTPS)json(bool $returnArray = false)
- JSON request payload helpermethod(): string
- request method getterpath(): string
- path getter, ex:/the/path
pathWithQueryString(): string
- path with query string getter, ex:/the/path?x=1
port(): int
- port getterquery(string $key, $default = null): Request\Query
- query (GET) input object getterqueryString(): string
- query string getter, ex:x=1&y=2
scheme(): string
- URI scheme getter, ex:http
session(): Request\Session
- session object getteruri(): string
- URI getter, ex:http://example.com/example?key=x
Input Methods
Input methods include methods for request input objects:
Cookie
,Input
andQuery
.email()
- value getter, sanitize as emailfloat()
- value getter, sanitize as floathas(): bool
- check if key existsinteger()
- value getter, sanitize as integerstring()
- value getter, sanitize as stringurl()
- value getter, sanitize as URLvalidator(\Exo\Validator\AbstractType $validator): AbstractInput
- validator setterSession Methods
Session methods
clear()
,get()
,has()
andset()
all use dot notation for keys, for example:set('user.isActive', 1)
equals:[user => [isActive => 1]]
clear(string $key)
- clear a keystatic cookieOptions(array $options)
- set cookie options- default options are:
['lifetime' => 0, 'path' => '/', 'domain' => '', 'secure' => false, 'httponly' => false]
- default options are:
destroy()
- destroy a sessionget(string $key)
- value getterhas(string $key): bool
- check if key existsisSession(): bool
- check if session existsset(string $key, $value)
- key/value settertoArray(): array
- session array getter
Exo\App\Http\Response
Response
is an HTTP response helper.
These examples use the
Exo\Factory
helper functionapp()
.
Methods
All methods return Exo\App\Http\Response
, unless otherwise stated.
cacheOff()
- disable cache using cache-controlcontentType(string $contentType)
- content-type settercookie($key, $value, $expires, $path, $domain, $secure, $httpOnly): bool
- cookie settercookieClear(string $key, string $path = '/'): bool
- remove cookieheader(string $key, $value)
- header setterheaderClear(string $key)
- remove header keyheaders(array $headers)
- headers setter using arrayjson($data): void
- respond with JSON andContent-type: application/json
in headersredirect(string $location, bool $statusCode301 = false): void
- send redirectstatusCode(int $code)
- status code setter
Exo\Entity
Entity is an object helper.
The apply()
method can be used to apply a callback to a property value, example:
The bind()
method can be used to bind an external Entity reference to objects or arrays, example:
The Property voidable()
method allows a property to be missing from the entity. This differs from the validator rule optional
because optional requires the property to be present. If a property is voidable the default value will not be used when using the toArray()
method and using allow voidables. Example usage for the property createdAt
that may only be set once (during create operation):
The Entity voidable()
method allows all properties to be missing from the entity, unless a property uses the notVoidable()
method. Example:
Methods
assert($name, $value)
- single prop value assertionderegisterProperty($name)
- deregister a propertyfromArray(array $data)
- properties values setterhasProperty($name): bool
- check if property existshasVoidableProperty(): bool
- check if any property is voidableisVoidable(): bool
- check if globally voidableproperty($name, $default = null): \Exo\Entity\Property
- register propertytoArray(array $filter = null, bool $voidable = false): array
- to array method$filter
- allows filtering fields- remove specific fields:
[field => 0, ...]
- include only specific fields:
[field => 1, ...]
- remove specific fields:
validate($name, $value)
- single prop value validationvoidable()
- set all properties as voidable (unless properties usenotVoidable()
method)Property Methods
apply(callable $callback): \Exo\Entity\Property
- apply callback to value- note:
$callback(value)
is only called if a value or default value exists for the property
- note:
bind(\Exo\Entity $entity): \Exo\Entity\Property
- create a reference to another EntitynotVoidable(): \Exo\Entity\Property
- cannot be set as voidablevalidator(): \Exo\Validator
- validator object getter- must be called after all other property methods
voidable(): \Exo\Entity\Property
- make a property voidable
trait Exo\Event
Event is an event helper.
Callable Chain
Multiple callables can be bound to the same event:
Returning true
from any bound callable will interrupt the chain of callables:
Exo\Exception
Exceptions can be improved by using or extending the Exo\Exception
class, or the other available Exo\App\Http\Exception\*
exception classes. Example:
Exceptions can be handled using the Exo\Exception
class:
Exo\Factory
The Exo factory is a factory helper that can be inherited.
Methods
cli(): \Exo\App\Cli
env(): \Exo\App\Env
logger(): \Exo\Logger
map(array $map = null): \Exo\Map
options(array $options = null): \Exo\Options
request(): \Exo\App\Http\Request
response(): \Exo\App\Http\Response
session(): \Exo\App\Http\Request\Session
share(): \Exo\Share
validator(string $name): \Exo\Validator
Exo\Factory\Annotation
Annotation is a class loading helper that utilizes class annotations.
Singleton Pattern
Use the singleton pattern in factory classes by inheriting the Exo\Factory\Singleton
class:
Inheritance Chain
Helper Function
Helper function example:
Exo\Factory\Dynamic
Dynamic class factory.
If a class doesn't exist an exception (Exo\Exception
) with be thrown, use try/catch to handle missing classes:
Exo\Factory\Mapper
Mapper is a class loading helper.
Singleton Pattern
Use the singleton pattern in factory classes by inheriting the Exo\Factory\Singleton
class:
Helper Function
Helper function example:
Exo\Factory\Singleton
Singleton is a singleton pattern helper.
Helper Function
Helper function example:
Exo\Logger
Logger is a logging helper.
This example uses the
Exo\Factory
helper functionapp()
.
Example custom log handler:
The debug()
method can be used with or without a message, and with or without context. Example:
Methods
critical(?string $message, array $context): \Exo\Logger
- critical log recorddebug(?string $message, array $context): \Exo\Logger
- debug log recorderror(?string $message, array $context): \Exo\Logger
- error log recordstatic globalContext(array $context)
- add context to global context- Local context will overwrite global context
static handler(\Exo\Logger\Handler $handler)
- add log handlerinfo(?string $message, array $context): \Exo\Logger
- info log recordwarning(?string $message, array $context): \Exo\Logger
- warning log record
Exo\Map
Map is a helper class for handling arrays. Map implements Countable
and Iterator
.
Methods
__construct(array $map)
- overridableclear($key)
- clearcount()
- get countfilterKeys(array $filter)
- filter map elements by key (exclude or include)get($key)
- getterhas($key)
- check if existshasValue($value)
- check if value existsisEmpty(): bool
- check it map is emptymerge(array $map)
- merge with another mapset($key, $value)
- settertoArray(): array
- get as arrayStatic Methods
arrayFilterKeys(array $array, array $filter): array
- either include or exclude array keys based on the filter$filter
- allows filtering keys- exclude:
[key => 0, ...]
- include:
[key => 1, ...]
- exclude:
&extract(array $array, $key, $valueKey = null): array
- extract key or key/value from multidimensional array to one dimensional
Exo\Model
Model is a model helper class that can be used with Exo\Entity
objects.
For reference see
Exo\Entity
example above
Methods
entity($data = null): \Exo\Entity
- entity factory methodentityArray(array $data, array $filter = null, bool $voidable = false): array
- array of entities factory
Exo\Options
Options is a helper class for handling options with validation.
Usage example:
Methods
get(string $key): mixed
- value getterhas(string $key): bool
- check if key existsoption(string $key, $defaultValue): \Exo\Validator
- optional default value setter and validationread(array &$map)
- abstract read allset($key, $value)
- value setter, or use array for keys/values settertoArray(): array
- get options as array[key => value, ...]
write(string $key, $value): bool
- abstract write key/value
Exo\Share
Share is a global key/value helper.
Methods
clear(string $key)
- clear keyget(string $key)
- getterhas(string $key): bool
- check if key existsset(string $key, $value)
- setter
Exo\Validator
Validator is a validation helper.
Optional
Custom Messages
Use custom validation exception messages:
Also a singe validation message can be used for an entire group:
Assert Callback
Use callback with assert()
method:
Custom Rules
Use custom rule:
Validate Method
Usage with validate()
method instead:
Use Custom Assertion Exception Class
A custom exception class can be used instead of the default Exo\Validator\Exception
class when an assertion fails, example:
Display Value in Assertion Exception Message
The value that fails validation can be displayed in the assertion exception message for debugging purposes:
Types & Rules
- Array - must be a non-empty array
depth(int $depth)
- must be an array with specific depthlength(int $length)
- must be a specific number of array itemsmax(int $max)
- array items must be a maximum ofmin(int $min)
- array items must be a minimum ofoptional()
- array can be emptyunique()
- array items must be unique
- Boolean - must be valid boolean value
optional()
- ignored because""
andnull
are consideredfalse
(unlesstype()
is used)type()
- must be primitive type boolean- when
type()
is not used acceptable boolean values are:- for true:
true
,"1"
,"true"
,"on"
,"yes"
- for false:
false
,"0"
,"false"
,"off"
,"no"
,""
,null
- for true:
- when
- Number - must be a valid number
between(int $min, int $max)
- must be between both valuesgreaterThan(int $compareValue)
- must be greater thaninteger()
- must be an integerlessThan(int $compareValue)
- must be less thanmax(int $max)
- must be a maximum ofmin(int $min)
- must be a minimum ofnegative()
- must be a negative numberoptional()
- allows""
andnull
positive()
- must be a positive numbertypeFloat()
- must be primitive type floattypeInteger()
- must be primitive type integerunique(callable $callback)
- must be unique (callable returnstrue
if unique)
- Object - must be an object
optional()
- allowsnull
- String - must be a non-empty string
allowed(array $list)
- must be allowedalnum(bool $allowWhitespaces = false)
- must only contain alphanumeric charactersalpha(bool $allowWhitespaces = false)
- must only contain alphabetic characterscontains($containsValue, bool $caseSensitive = true)
- must contain valueemail()
- must be a valid email addresshash(string $knownHash)
- hashes must be equalipv4()
- must be valid IPv4 addressipv6()
- must be valid IPv6 addressjson()
- must be a valid JSONlength(int $length)
- length must be exact number of charactersmatch(string $compareValue, bool $caseSensitive = true)
- values must be equalmax(int $max)
- length must be a maximum number of charactersmin(int $min)
- length must be a minimum number of charactersnotAllowed(array $list)
- is not allowedoptional
- allows""
andnull
password(string $hash)
- passwords must be equalregex(string $pattern)
- must match regular expression patterntype()
- must be primitive type stringunique(callable $callback)
- must be unique (callable returnstrue
if unique)url()
- must be valid URLMethods
assert($value, callable $callback)
- throws exception if validation failsgetMessage()
- get first validation message after validationgetMessages(): array
- get all validation messages after validationgroupMessage(string $message)
- set group single validation messagemessage(string $message): \Exo\Validator\AbstractType
- set validation message for last rulerule(\Exo\Validator\RuleInterface $rule): \Exo\Validator\AbstractType
- add custom rulevalidate($value): bool
- validate value
Functions
bind(): string
Formatted string helper.
Any depth beyond the allowed depth of two is auto-serialized, example:
debug($message = null, $context = null): \Exo\Logger
Logger debug alias.
env(string $key, $default)
The env()
function is a getter helper function for Env.
logger(string $channel = ''): \Exo\Logger
Logger helper function.
pa(...$values): void
HTML and CLI friendly printer for all PHP types.
share(string $key, $value)
The share()
function is a getter/setter helper function for Share.
token(int $length = 32): string
Generate tokens: