Download the PHP package iqomp/formatter without Composer

On this page you can find all versions of the php package iqomp/formatter. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package formatter

iqomp/formatter

This is a library that format your single or multiple object to suit your needs based on formats config. This is the library that you need before sending your object ( from database ) send to client or view.

Installation

Publishing Config

Configuration

You should define one format for each object you save on your database. Please follow this steps to create new object format config.

Add your object formatter config under file config/autoload/formatter.php with content as below:

Usage

This module create new global class that you can use to format your object, the class Iqomp\Formatter\Formatter is the only class that you'll ever need from this module.

Methods

Class Iqomp\Formatter\Formatter define a few method that can be used from everywhere

static function format(string $format, object $object, array $options=[]): ?object

Format a single object. This method accept arguments:

  1. string $format The format name as defined on global config.
  2. object $object The object to format.
  3. array $options List of additional option to send to each format type handler.

See above usage for example.

static function formatApply(array $formats, array $objects, array $options=[], string $askey=null): ?array

Apply list of rules to an object without registering it on global config. This method accept arguments:

  1. array $formats List of format types to apply to the object
  2. array $objects List of objects to format.
  3. array $options List of additional option to send to each format type handler.
  4. string $askey The object property to use as array key of format result. If this field is not set, indexed array will be returned.

static function formatMany(string $format, array $objects, array $options=[], string $askey=null): array

As of format but for many objects. See above methods for arguments.

static function typeApply(string $type, $value, string $field, object $object, $format, $options )

Apply single formatter type to a value. Internally, all above method use this method to apply format type for every object properties. This method accept parameters:

  1. string $type Format type to apply to the value.
  2. $value The value to format.
  3. string $field The field name from the object this value taken from.
  4. object $object The original object the value taken from.
  5. mixed $format The value of field format config.
  6. mixed $options The options to send to format type handler.

See below example:

Custom Handler

In same conditoin, you may find that format types we provide is not enough or is not match your condition. In this case, you may prefer create your own format type handler.

There's two type of format type handler this module know. Non collective and collective action.

Collective

If your handler is collective type, formatter will collect all object's property and call the handler at once. This type is good if your handler need to call external resouces like database, or curl.

The method will be called as below:

Returned value of this method is expecting to be array oldvalue => newvalue pair.

If the method return null, no data will be changed.

This method is called with below arguments:

  1. array $values Indexed array of all objects property that being formatted or other objects property based on handler config.
  2. string $field The field name that being processed.
  3. array $objects All object that being processed.
  4. array $format Format config that being implemented to the field.
  5. mixed $options Format option that send by formatter caller.

Below is an example of collective handler:

Non Collective

This is the common forma type. All format type handler on this module is using this type.

The method will be called as below:

If the method return null, no data will be changed.

This method is called with below arguments:

  1. mixed $value The object property that being formatted.
  2. string $field The field name that being processed.
  3. object $object Object that being processed.
  4. array $format Format config that being implemented to the field.
  5. mixed $options Format option that send by formatter caller.

Below is an example of non collective handler:

Creating Custom Handler

Follow this step to create new format type handler:

Create Type Handler

Create new class that handle the object property:

Returning null on type handler will not modify object property.

Register Handler Config

Create new file on your module named ConfigProvider and return array as below in the __invoke public method:

Then, update your module composer.json file to register the new config as below:

Special Options

This is some special format option that can help you in some condition:

@default

Set default value for the property if the value is falsy:

@rest

Apply current format option to all object properties that don't have format type options:

Above option will return object with only id and created_at property. The rest of object properties will apply 'type' => 'delete' option.

@clone

Clone other object property value to current new property, the clone action is done before format type applied to the property:

The property user will clone the value of unformatted property user_id, and apply format type std-id to the user property.

@rename

Rename current object property name. The rename action is done after the object is formatted.

Above option will rename object property user_id to user.

@finalize

Re-call one more format type to the object property right after all object properties format already applied. This format type will be applied right before returning the format result to the caller.

Format Types

Below is all known format types defined by this module so far:

bool/boolean

Convert the value to boolean type

clone

Clone other object property value with condition, the different between this option and special option @clone is this type don't format the cloned value and this type can clone only part of sub property of object property.

It can optionally convert the value with other format type:

Above option will create new object property named /field/ with data taken from $object->user->name->first. And conver the value to type text. The final value of property /field/ is now Iqomp\Formatter\Object\Text object.

To create new property with value taken from multiple object property, use option sources:

Above option will create new object property named /field/ with type object. The object property name taken from $object->user->name->first and conver the value to type text. The second property is bdate that taken from $object->birthdate and convert the value to type date.

custom

Use other handler to modify object property value.

The callback will get exactly as format type handler non-collective arguments.

date

Convert the value to Iqomp\Formatter\Object\DateTime object. If timezone not set, it'll back to php xdefault timezone.

Please see below for details of this object.

delete

Remove object property.

embed

Convert the value to Iqomp\Formatter\Object\Embed. It's the one that generate embed html code of popular video service like youtube, etc.

interval

Conver the value to Iqomp\Formatter\Object\Interval.

Please see below for more information about this object.

multiple-text

Convert the value to array of Iqomp\Formatter\Object\Text with same separator. It conver the original value string to array of object type text.

If the value of separator is null, it will use PHP_EOL as the separator. While if it's 'json', it will use json_decode to separate the value.

number

Conver the value to Iqomp\Formatter\Object\Number.

The final value will be int if decimal is not set, or float if the value is bigger than 0. Please see below for more information about this object.

text

Conver the value to Iqomp\Formatter\Object\Text.

Please see below for more information about this object.

json

Conver the value of json string to array/object with json_decode. If property format is there, the value of decoded value will be formatted with the format supplied.

join

Combine text and object property valeu to fill current property. To get the value of object property, add prefix $ to the value of array member:

Above example will create a text My name is (:name) where placeholder (:name) value will taken from $object->user->name->first.

rename

Rename object property name.

std-id

Convert the value to Iqomp\Formatter\Object\Std, which is an object that only has 1 property that is id.

If you json encode an object property with format type std-id, it will be something like {"id":val}.

switch

A format type that allow you to apply format type to the object property based on value of some of object property.

Based on above config, the value of object property /field/ will apply config result of config above if condition match.

If the value of object->/object-property-name/ is equal to 1, current property will be formatted with 'type' => 'number'. Or if the value is bigger than 2, it will be formatted with 'type' => 'text'.

Known operator so far are =, !=, >, <, >=, <=, in, and !in. For operator in and !in, the config expected expecting an array.

Type Object

Some object format type convert the value of object property to an interal object. The object is implementing \JsonSerializable that make it possible to json_encode.

Below is list of known object so far:

Iqomp\Formatter\Object\DateTime

The object that extending \DateTime with custom property.

Iqomp\Formatter\Object\Embed

The object that identify and make embed html script of popular video service url.

It will return final URL for __toString() and json_encode.

Iqomp\Formatter\Object\Interval

The object that handle interval string.

Iqomp\Formatter\Object\Number

The object that work with number.

Iqomp\Formatter\Object\Std

Simple object that has only one property, which is id that taken from original value of the property.

Iqomp\Formatter\Object\Text

The object that work with text.

Getting property safe and clean will return new object ~\Text. The safe property return htmlspecialschars($, ENT_QUOTES) of the original value. The clean property return text with only characters a-zA-Z0-9.

Linter


All versions of formatter with dependencies

PHP Build Version
Package Version
No informations.
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package iqomp/formatter contains the following files

Loading the files please wait ....