2017 © Pedro Peláez
 

library transformation

This library is a wrapper to transforms a string to another string (date format to another format, strings, regexp....

image

inetprocess/transformation

This library is a wrapper to transforms a string to another string (date format to another format, strings, regexp....

  • Wednesday, January 3, 2018
  • by edyan
  • Repository
  • 7 Watchers
  • 6 Stars
  • 179 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 16 Versions
  • 0 % Grown

The README.md

Build Status Scrutinizer Code Quality Code Coverage Build Status SensioLabsInsight, (*1)

inetprocess/transformation

Inspired by Respect/Validation, that library transforms an input string (or float / int / bool) to an output string, after applying rules., (*2)

The benefit using it is that it a generic wrapper for any kind of transformation. Also it throws Exceptions and allows to chain the transformations., (*3)

Usage

It's pretty simple to use. suppose you need to transform a date "31-12-2012" to another format such as "2012-12-31". Just do:, (*4)

<?php
require_once __DIR__ . '/vendor/autoload.php';
use Inet\Transformation\Transform as T;

$dateEntered = '31-12-2012';
$output = T::Date('d-m-Y', 'Y-m-d')->transform($dateEntered);
echo $output;

Now if you need to change the format then the Timezone:, (*5)

<?php
require_once __DIR__ . '/vendor/autoload.php';
use Inet\Transformation\Transform as T;

$dateEntered = '31-12-2012 23:21:58';
$output = T::Date('d-m-Y H:i:s', 'Y-m-d H:i:s')->Timezone('Y-m-d H:i:s', 'Asia/Calcutta')->transform($dateEntered);
// Displays: 2013-01-01 03:51:58
echo $output;

Transformation Rules

For now there is only a few rules. I'll add more later, and don't hesitate if you want to contribute., (*6)

Callback(string $functionName, [$param1, $param2, ...])

Call any php function on input. The input will be passed as the last argument of the function., (*7)

T::Callback('sprintf', "REF%'06d")->transform(1234); // REF001234

CopyFileToUuid(string $sourceDir, string $destinationDir)

Copy a file with a filename to a UUID generated name. Return the generated UUID., (*8)

T::CopyFileToUuid('/var/www/upload/', '/var/www/unified_upload')->transform('quotes/client_01.pdf'); // 123e4567-e89b-12d3-a456-426655440000

## Concat(string $before, [string $after])
Append and prepend string to input.
```php
T::Concat('REF')->transform('1234'); // REF1234
T::Concat('REF', 'AB')->transform('1234'); // REF1234AB

Date(string $inputFormat, string $outputFormat)

Transforms a date from a format to another., (*9)

See example above, (*10)

Explode(string $delimiter)

Explode a string to an array using a delimiter. Uses explode function from PHP. It returns an array., (*11)

T::Explode(',')->transform('foo,bar,baz'); // array('foo', 'bar', 'baz');
T::Explode(',')->Implode('|')->transform('foo,bar,baz'); // foo|bar|baz

Htmlspecialchars([array $flags, string $encoding, bool $double_encoding])

Encode special chars. Uses htmlspecialchars function from PHP., (*12)

T::Htmlspecialchars(['ENT_QUOTES'])->transform("l'arbre"); // l&#039;arbre
T::Htmlspecialchars(['ENT_QUOTES'], 'UTF-8', false)->transform("l&#039;arbre"); // l&#039;arbre

HtmlspecialcharsDecode([array $flags])

Decode special chars. Uses htmlspecialchars_decode function from PHP., (*13)

T::HtmlspecialcharsDecode(['ENT_QUOTES'])->transform("l&#039;arbre"); // l'arbre

Implode(string $delimiter)

Join an array elements to a string. Uses implode function from PHP., (*14)

T::Implode('@')->transform(array('foo', 'bar')); // foo@bar

Map(array $mapping)

Try to replace the input with the value in the mapping. It can also work with an array as input. Values not found in mapping are return without tranformations, (*15)

$mapping = array(
    '1' => 'key1',
    '10' => 'key10',
);
T::Map($mapping)->transform('1'); // key1
T::Map($mapping)->transform(array('10', '1')); // array('key10', 'key1')
T::Map($mapping)->transform('unknown key'); // unknown key

MimeType([string $root])

Return the MimeType of the file. The first optional parameter allow to specify a root directory prepend to the filename, (*16)

T::MimeType()->transform('test.jpg'); // image/jpeg
T::MimeType('uploads')->transform('logo.png'); // image/png

NormalizeURL(string $protocol)

Prepend a default protocol if not present to any url., (*17)

T::NormalizeURL('http')->transform('https://www.google.com'); // https://www.google.com
T::NormalizeURL('http')->transform('www.google.com'); // http://www.google.com
T::NormalizeURL('http')->transform('ssh://github.com'); // ssh://github.com
T::NormalizeURL('ssh')->transform('github.com'); // ssh://github.com

Replace(string $search, string $replace)

Replace a string by another (does the same than str_replace)., (*18)

T::Replace('a', 'b')->transform('ababa'); // bbbbb

ReplaceRegexp(string $pattern, string $replacement)

Replace a pattern by a replacement (does the same than preg_replace)., (*19)

T::ReplaceRegexp('/^fox/', 'rabbit')->transform('fox and foxes'); // rabbit and foxes

Slugify()

Uses Cocur\Slugify to Slugify a string., (*20)

T::Slugify()->transform('Bonjour tôôut le monde !'); // bonjour-toout-le-monde

SetType()

Uses settype from PHP for type casting., (*21)

T::SetType('bool')->transform('1'); // true
T::SetType('bool')->transform('0'); // false
T::SetType('bool')->transform(array()); // false

SugarCRMMapMultiEnum(array $mapping, [array $options])

Map multiple values from a string or an array and return a string encoded for database storage of SugarCRM multi enum field. String are exploded first with the default separator |. You can set the following options * separator: Separator to use to explode input string. Default: | * from_multi_enum: If true parse the input string as a SugarCRM multi enum field. Default: false, (*22)

$mapping = array(
    '1' => 'key1',
    '10' => 'key10',
);
T::SugarCRMMapMultiEnum($mapping)->transform('1|10'); // ^key1^,^key10^
T::SugarCRMMapMultiEnum($mapping)->transform(array('1', '10'); // ^key1^,^key10^
T::SugarCRMMapMultiEnum($mapping)->transform('^1^,^23^', array('from_multi_enum' => true)); // ^key1^,^23^

Timezone(string $inputFormat, string $targetTimezone, [string $currentTimezone])

Change the Timezone of a Date by providing the format, the target Timezone and optionnaly the timezone for the current date., (*23)

See example above, (*24)

The Versions

03/01 2018

dev-master

9999999-dev

This library is a wrapper to transforms a string to another string (date format to another format, strings, regexp....

  Sources   Download

GPL-2.0

The Requires

 

The Development Requires

by Emmanuel Dyan

transformation

03/01 2018

1.2.6

1.2.6.0

This library is a wrapper to transforms a string to another string (date format to another format, strings, regexp....

  Sources   Download

GPL-2.0

The Requires

 

The Development Requires

by Emmanuel Dyan

transformation

03/01 2018

1.2.5

1.2.5.0

This library is a wrapper to transforms a string to another string (date format to another format, strings, regexp....

  Sources   Download

GPL-2.0

The Requires

 

The Development Requires

by Emmanuel Dyan

transformation

23/08 2017

1.2.4

1.2.4.0

This library is a wrapper to transforms a string to another string (date format to another format, strings, regexp....

  Sources   Download

GPL-2.0

The Requires

 

The Development Requires

by Emmanuel Dyan

transformation

23/08 2017

1.2.3

1.2.3.0

This library is a wrapper to transforms a string to another string (date format to another format, strings, regexp....

  Sources   Download

GPL-2.0

The Requires

 

The Development Requires

by Emmanuel Dyan

transformation

03/08 2017

1.2.2

1.2.2.0

This library is a wrapper to transforms a string to another string (date format to another format, strings, regexp....

  Sources   Download

GPL-2.0

The Requires

 

The Development Requires

by Emmanuel Dyan

transformation

25/04 2017

1.2.1

1.2.1.0

This library is a wrapper to transforms a string to another string (date format to another format, strings, regexp....

  Sources   Download

GPL-2.0

The Requires

 

The Development Requires

by Emmanuel Dyan

transformation

25/04 2017

1.2.0

1.2.0.0

This library is a wrapper to transforms a string to another string (date format to another format, strings, regexp....

  Sources   Download

GPL-2.0

The Requires

 

The Development Requires

by Emmanuel Dyan

transformation

29/03 2017

1.1.0

1.1.0.0

This library is a wrapper to transforms a string to another string (date format to another format, strings, regexp....

  Sources   Download

GPL-2.0

The Requires

 

The Development Requires

by Emmanuel Dyan

transformation

24/03 2017

1.0.0

1.0.0.0

This library is a wrapper to transforms a string to another string (date format to another format, strings, regexp....

  Sources   Download

GPL-2.0

The Requires

 

The Development Requires

by Emmanuel Dyan

transformation

04/08 2016

0.6

0.6.0.0

This library is a wrapper to transforms a string to another string (date format to another format, strings, regexp....

  Sources   Download

GPL-2.0

The Requires

 

The Development Requires

by Emmanuel Dyan

transformation

13/11 2015

0.5

0.5.0.0

This library is a wrapper to transforms a string to another string (date format to another format, strings, regexp....

  Sources   Download

GPL-2.0

The Requires

 

The Development Requires

by Emmanuel Dyan

transformation

28/08 2015

0.4

0.4.0.0

This library is a wrapper to transforms a string to another string (date format to another format, strings, regexp....

  Sources   Download

GPL-2.0

The Requires

 

The Development Requires

by Emmanuel Dyan

transformation

28/08 2015

0.3

0.3.0.0

This library is a wrapper to transforms a string to another string (date format to another format, strings, regexp....

  Sources   Download

GPL-2.0

The Requires

 

The Development Requires

by Emmanuel Dyan

transformation

27/08 2015

0.2

0.2.0.0

This library is a wrapper to transforms a string to another string (date format to another format, strings, regexp....

  Sources   Download

The Requires

 

The Development Requires

by Emmanuel Dyan

transformation

27/08 2015

0.1

0.1.0.0

This library is a wrapper to transforms a string to another string (date format to another format, strings, regexp....

  Sources   Download

The Requires

 

The Development Requires

by Emmanuel Dyan

transformation