Skip to content

Commit

Permalink
Document all transformations in README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
Rémi Sauvat committed Mar 24, 2017
1 parent 6ea6df2 commit ac49b73
Showing 1 changed file with 70 additions and 20 deletions.
90 changes: 70 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,46 +36,96 @@ 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.

## Callback(string $functionName, [$param1, $param2, ...])
Call any php function on input. The input will be passed as the last argument of the function.
```php
T::Callback('sprintf', "REF%'06d")->transform(1234); // REF001234
```

## 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.

_See example above_

## Explode(string $delimiter)
Explode a string to an array using a delimiter. Uses `explode` function from PHP.
It returns an array.
```php
T::Explode(',')->transform('foo,bar,baz'); // array('foo', 'bar', 'baz');
T::Explode(',')->Implode('|')->transform('foo,bar,baz'); // foo|bar|baz
```

## Implode(string $delimiter)
Join an array elements to a string. Uses `implode` function from PHP.
```php
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

```php
$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
```

## NormalizeUrl(string $protocol)
Prepend a default protocol if not present to any url.
```php
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).
```php
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Inet\Transformation\Transform as T;

$str = 'abababababab';
$output = T::Replace('a', 'b')->transform($str);
echo $output;
T::Replace('a', 'b')->transform('ababa'); // bbbbb
```


## ReplaceRegexp(string $pattern, string $replacement)
Replace a pattern by a replacement (does the same than preg_replace).
```php
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Inet\Transformation\Transform as T;

$str = 'abababababab';
$output = T::ReplaceRegexp('/a/', 'b')->transform('abababababab');
echo $output;
T::ReplaceRegexp('/^fox/', 'rabbit')->transform('fox and foxes'); // rabbit and foxes
```

## Slugify()
Uses [Cocur\Slugify](https://github.com/cocur/slugify) to Slugify a string.
```php
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Inet\Transformation\Transform as T;
T::Slugify()->transform('Bonjour tôôut le monde !'); // bonjour-toout-le-monde
```

$output = T::Slugify()->transform('Bonjour tôôut le monde !');
// Displays: bonjour-toout-le-monde
echo $output;
## 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`
```php
$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])
Expand Down

0 comments on commit ac49b73

Please sign in to comment.