2017 © Pedro Peláez
 

library utils

General-purpose library for php.

image

telesto/utils

General-purpose library for php.

  • Friday, May 23, 2014
  • by mdopt
  • Repository
  • 1 Watchers
  • 0 Stars
  • 1 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

General-purpose library for php.

The following examples use shorter array syntax from php 5.4+ for brevity, but the library works also for older versions (5.3.2+ to be precise)., (*1)

Strings:

use Telesto\Utils\StringUtil;

StringUtil::explode (standard explode with escaping delimiter feature), (*2)


StringUtil::explode('.', 'first\.dont_explode_this.second', null, ['escapeChar'=> '\\']); // ['first.dont_explode_this', 'second']

StringUtil::implode (standard implode with escaping delimiter feature), (*3)

StringUtil::implode('.', ['first.escape_this_dot', 'second'], ['escapeChar'=> '\\']); 
// 'first\.escape_this_dot.second'

StringUtil::strposAll (works like strpos but returns all positions), (*4)

StringUtil::strposAll(' @ @@ @', '@'); 
// [1, 3, 4, 6]

StringUtil::substrConsecutiveCounts, (*5)

StringUtil::substrConsecutiveCounts(' @ @@@ @@', '@'); 
// [1, 3, 2]

StringUtil::substrMaxConsecutiveCount, (*6)

StringUtil::substrMaxConsecutiveCount(' @ @@@ @@', '@'); 
// 3

Arrays:

use Telesto\Utils\ArrayUtil;

Set of array functions that also work for objects implementing ArrayAccess interface., (*7)

ArrayUtil::getKeys (works like array_keys, but also supports iterators), (*8)

ArrayUtil::getKeys(new \ArrayObject(['x' => 10, 'y' => 20])); 
// ['x', 'y']

ArrayUtil::getElementByKeyPath, (*9)

ArrayUtil::getElementByKeyPath(
    [
        'points'    => [
            [
                'x' => 10,
                'y' => 20
            ],
            [
                'x' => 100,
                'y' => 200
            ]
        ]
    ],
    'points.1.y'
);
// 200

ArrayUtil::hasElementAtKeyPath, (*10)

ArrayUtil::hasElementAtKeyPath(
    [
        'point' => [
            'x' => 10,
            'y' => 20
        ]
    ],
    'point.x'
);
// true

ArrayUtil::hasElementAtKeyPath(
    [
        'point' => [
            'x' => 10,
            'y' => 20
        ]
    ],
    'point.z'
);
// false

ArrayUtil::setElementByKeyPath, (*11)

$array = [
    'point' => [
        'x' => 10,
        'y' => 20
    ]
];

ArrayUtil::setElementByKeyPath($array, 'point.z', 30);

/* $array is now [
    'point' => [
        'x' => 10,
        'y' => 20,
        'z' => 30
    ]
]
*/

ArrayUtil::unsetElementByKeyPath, (*12)

$array = [
    'point' => [
        'x' => 10,
        'y' => 20
    ]
];

ArrayUtil::unsetElementByKeyPath($array, 'point.y');

/* $array is now [
    'point' => [
        'x' => 10
    ]
]
*/

Array Operations

Telesto\Utils\Arrays component provides interface for generic operations on arrays and objects implementing ArrayObject interface(some operations have additional requirements)., (*13)

There are 2 types of operations: - transformations (they produce output given the input) - overwrites (they are given 2 arguments and use the first to overwrite the second), (*14)

Every transformation can be done by creating a new array and overwriting it(and that's the way it's implemented)., (*15)

Every transformer is an instance of Telesto\Utils\Arrays\Transformation\Transformer interface. Every overwriter is an instance of Telesto\Utils\Arrays\Overwriting\Overwriter interface., (*16)

You can create these instances by yourself. The following example shows how to create and use transformer to perform 'copy by key path map' operation., (*17)

use Telesto\Utils\Arrays\Overwriting\Copy\KeyPathMap\BasicOverwriter as KeyPathMapOverwriter;
use Telesto\Utils\Arrays\Transformation\CreateAndOverwriteTransformer;

$transformer = new CreateAndOverwriteTransformer(
    new KeyPathMapOverwriter([
        'points.0.x'    => 'p.x',
        'points.0.y'    => 'p.y'
    ])
);

$output = $transformer->transform([
    'points'    => [
        [
            'x' => 10,
            'y' => 20
        ]
    ]
]);

/*
$output is [
    'p' => [
        'x' => 10,
        'y' => 20
    ]
]
*/

But this is requires a lot of boilerplate code. Here's a shorter version that uses OperationFacade:, (*18)

use Telesto\Utils\Arrays\OperationFacade;

$transformer = OperationFacade::createTransformer('copy.byKeyPathMap', [
    'points.0.x'    => 'p.x',
    'points.0.y'    => 'p.y'
]);

$output = $transformer->transform([
    'points'    => [
        [
            'x' => 10,
            'y' => 20
        ]
    ]
]);

or, if you only need to perform one operation and never use the transformer again:, (*19)

use Telesto\Utils\Arrays\OperationFacade;

$output = OperationFacade::transform(
    [
        'points'    => [
            [
                'x' => 10,
                'y' => 20
            ]
        ]
    ],
    'copy.byKeyPathMap',
    [
        'points.0.x'    => 'p.x',
        'points.0.y'    => 'p.y'
    ]
);

License: MIT, (*20)

The Versions

23/05 2014

dev-master

9999999-dev

General-purpose library for php.

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

by Paweł Ulewicz