2017 © Pedro Peláez
 

library utilities

image

jimmyoak/utilities

  • Wednesday, January 25, 2017
  • by jimmyoak
  • Repository
  • 2 Watchers
  • 8 Stars
  • 42,246 Installations
  • PHP
  • 4 Dependents
  • 0 Suggesters
  • 4 Forks
  • 0 Open issues
  • 49 Versions
  • 11 % Grown

The README.md

JimmyOak Utilities

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

Installation

Use Composer to install the package:, (*2)

$ composer require jimmyoak/utilities

For PHP 5.3 compatibility use *.*.*b versions (yep, I marked them as beta versions):, (*3)

$ composer require jimmyoak/utilities:2.5.1b

Features

  • Collection and Set utilities (Typed/Untyped collections and sets)
  • Enum base class
  • SimpleValueObject base class
  • Event publisher
  • Array utilities
  • File utilities
  • String utilities

Examples

Collections

Collection

$collection = new \JimmyOak\Collection\Collection();
$collection[] = 'Foo';
$collection[] = 'Bar';

foreach ($collection as $value) {
    echo $value . ' ';
}

//prints: Foo Bar

TypedCollection

$collection = new \JimmyOak\Collection\TypedCollection(\DateTime::class);
$collection[] = new \DateTime();
$collection[] = new \DateInterval('P1D'); //Throws \JimmyOak\Exception\Collection\NotValidObjectTypeException

Set

You can fill the collection with object, scalars..., (*4)

$collection = new \JimmyOak\Collection\Set();
$collection[] = 'Foo';
$collection[] = 'Foo';
$collection[] = 'Bar';

foreach ($collection as $value) {
    echo $value . ' ';
}

//prints: Foo Bar

TypedSet

You can fill the collection with object, scalars..., (*5)

$collection = new \JimmyOak\Collection\TypedSet(\DateTime::class);
$aDateTime = new \DateTime('1992-10-07');
$collection[] = $aDateTime;
$collection[] = $aDateTime;
try {
    $collection[] = new \DateInterval('P1D'); //throws \JimmyOak\Exception\Collection\NotValidObjectTypeException
} catch (\JimmyOak\Exception\Collection\NotValidObjectTypeException $e) {
    //Do nothing ^^'
}

foreach ($collection as $value) {
    echo $value->format('Y-m-d') . ' ';
}

//prints: 1992-10-07

Of course you can hipervitaminate these classes:, (*6)

class DateTimeCollection extends \JimmyOak\Collection\TypedSet
{
    public function __construct() {
        $this->setObjectType(\DateTime::class);
    }

    public function asStrings()
    {
        $dates = [];
        foreach ($this as $value) {
            $dates[] = $value->format('Y-m-d');
        }

        return $dates;
    }
}

$dateTimeCollection = new \DateTimeCollection();
$aDateTime = new \DateTime('1992-10-07');
$dateTimeCollection[] = $aDateTime;
$dateTimeCollection[] = $aDateTime;
$dateTimeCollection[] = new \DateTime('1992-09-08');

foreach ($dateTimeCollection->asStrings() as $dateTimeString) {
    echo $dateTimeString . ' - ';
}

// prints: 1992-10-07 - 1992-09-08 - 

EventPublisher

class MessageEvent extends Event
{
    private $message;

    public function __construct($message)
    {
        parent::__construct();
        $this->message = $message;
    }

    public function getMessage()
    {
        return $this->message;
    }
}

class MessageSubscriber extends EventSubscriber
{
    public function isSubscribedTo(Event $event)
    {
        return $event instanceof MessageEvent;
    }

    public function handle(Event $event)
    {
        printf(
            '[%s]: %s %s',
            $event->getOccurredOn()->format('Y-m-d H:i:s'),
            $event->getMessage(),
            PHP_EOL
        );
    }
}

SingleEventPublisher::instance()
    ->subscribe(new MessageSubscriber())
    ->publish(new MessageEvent('Hi!'))
    ->publish(new MessageEvent('Bye!'));

DataType

Enum

class FuelType extends \JimmyOak\DataType\Enum
{
    const GASOLINE = 'gasoline';
    const DIESEL = 'diesel';
    const KEROSENE = 'kerosene';
}

echo 'Available fuels: ' . PHP_EOL;
foreach (FuelType::getConstList() as $constName => $value) {
    echo $constName . ' => ' . $value . PHP_EOL;
}

echo PHP_EOL;
//prints:
// Available fuels:
// GASOLINE => gasoline
// DIESEL => diesel
// KEROSENE => kerosene

$gasoline = new FuelType(FuelType::GASOLINE);
echo $gasoline->value() . PHP_EOL; //prints: 'gasoline'
echo (string) $gasoline . PHP_EOL; //prints: 'gasoline'

$nonExistentFuelType = new FuelType('grass'); //throws \InvalidArgumentException

SimpleValueObject

class Amount extends \JimmyOak\DataType\SimpleValueObject
{
    public function add(Amount $amount) {
        return $this->mutate($this->value() + $amount->value());
    }
}

$amount = new \Amount(500);
echo (string) $amount . PHP_EOL; //prints: 500
echo $amount->value() . PHP_EOL; //prints: 500

$anotherAmount = new \Amount(700);
echo ($amount->equals($anotherAmount) ? 'EQUAL' : 'NOT EQUAL') . PHP_EOL; //prints: NOT EQUAL

$newAmount = $amount->add(new Amount(200));
echo $amount->value() . PHP_EOL; //prints: 500
echo $newAmount->value() . PHP_EOL; //prints: 700

echo ($anotherAmount->equals($newAmount) ? 'EQUAL' : 'NOT EQUAL') . PHP_EOL; //prints: EQUAL

Utility

ArrayUtils

Flatten

$array = [
    'FOO',
    [ 'BAR' ],
    'CHILDREN' => [
        'FOO2' => 'FOOBAR',
        'BAR2' => 'FOOBAR2',
        [
            'FOO2' => 'FOOBAR3'
        ]
    ]
];

$notPreservedKeys = \JimmyOak\Utility\ArrayUtils::instance()->flatten($array, \JimmyOak\Utility\ArrayUtils::NO_PRESERVE_KEYS);
// Overrides existing keys (overrides keys 0 and FOO2 existing in children)
$preservedKeys = \JimmyOak\Utility\ArrayUtils::instance()->flatten($array, \JimmyOak\Utility\ArrayUtils::PRESERVE_KEYS);
// Overrides only ASSOCIATIVE KEYS
$preservedAssociativeKeys = \JimmyOak\Utility\ArrayUtils::instance()->flatten($array, \JimmyOak\Utility\ArrayUtils::PRESERVE_ASSOCIATIVE_KEYS);

var_export($notPreservedKeys);
echo PHP_EOL . PHP_EOL;
var_export($preservedKeys);
echo PHP_EOL . PHP_EOL;
var_export($preservedAssociativeKeys);

// prints:
// array (
//   0 => 'FOO',
//   1 => 'BAR',
//   2 => 'FOOBAR',
//   3 => 'FOOBAR2',
//   4 => 'FOOBAR3',
// )
// 
// array (
//   0 => 'BAR',
//   'FOO2' => 'FOOBAR3',
//   'BAR2' => 'FOOBAR2',
// )
// 
// array (
//   0 => 'FOO',
//   1 => 'BAR',
//   'FOO2' => 'FOOBAR3',
//   'BAR2' => 'FOOBAR2',
// )

ToXmlString and ToXml

$array = [
    'details' => [
        'media' => [
            'image' => [
                'anImage.png',
                'anotherImage.png',
            ],
            'video' => 'aVideo.mp4',
            'audio' => [],
        ]
    ]
];

$xml = \JimmyOak\Utility\ArrayUtils::instance()->toXmlString($array);
echo $xml . PHP_EOL . PHP_EOL;

// prints: <?xml version="1.0" encoding="UTF-8"? ><details><media><image>anImage.png</image><image>anotherImage.png</image><video>aVideo.mp4</video><audio/></media></details>

// Converts array into SimpleXmlElement
$xml = \JimmyOak\Utility\ArrayUtils::instance()->toXml($array);
var_dump($xml);

// prints:
// class SimpleXMLElement#3 (1) {
//     public $media =>
//     class SimpleXMLElement#4 (3) {
//         public $image =>
//         array(2) {
//             [0] =>
//             string(11) "anImage.png"
//             [1] =>
//             string(16) "anotherImage.png"
//         }
//         public $video =>
//         string(10) "aVideo.mp4"
//         public $audio =>
//         class SimpleXMLElement#5 (0) {
//         }
//     }
// }

ObjectUtils

ToArray

class Foo
{
    public $public = 'public';
    protected $protected = 'protected';
    private $private = 'private';
    private $anObject;

    /**
     * Foo constructor.
     *
     * @param $anObject
     */
    public function __construct($anObject)
    {
        $this->anObject = $anObject;
    }
}

class Bar
{
    private $value = 'value';
}

$foo = new Foo(new Bar());

//Shallow
$arrayed = \JimmyOak\Utility\ObjectUtils::instance()->toArray($foo, \JimmyOak\Utility\ObjectUtils::SHALLOW);
var_export($arrayed);
// prints:
//array (
//    'public' => 'public',
//)

echo PHP_EOL . PHP_EOL;

//Deep
$arrayed = \JimmyOak\Utility\ObjectUtils::instance()->toArray($foo, \JimmyOak\Utility\ObjectUtils::DEEP);
var_export($arrayed);
// prints:
//array (
//    'public' => 'public',
//    'protected' => 'protected',
//    'private' => 'private',
//    'anObject' =>
//        array (
//            'value' => 'value',
//        ),
//)

ToXmlString and ToXml

Note: ToXml would do the same but returns a SimpleXml object, (*7)

class Foo
{
    public $public = 'public';
    protected $protected = 'protected';
    private $private = 'private';
    private $anObject;

    /**
     * Foo constructor.
     *
     * @param $anObject
     */
    public function __construct($anObject)
    {
        $this->anObject = $anObject;
    }
}

class Bar
{
    private $value = 'value';
}

$foo = new Foo(new Bar());

$xml = \JimmyOak\Utility\ObjectUtils::instance()->toXmlString($foo, \JimmyOak\Utility\ObjectUtils::SHALLOW);
echo $xml . PHP_EOL;
// prints: <?xml version="1.0" encoding="UTF-8"? ><public>public</public>

$xml = \JimmyOak\Utility\ObjectUtils::instance()->toXmlString($foo, \JimmyOak\Utility\ObjectUtils::DEEP);
echo $xml . PHP_EOL;
// prints: <?xml version="1.0" encoding="UTF-8"? ><public>public</public><protected>protected</protected><private>private</private><anObject><value>value</value></anObject>

StringUtils

BeginsWith

echo (\JimmyOak\Utility\StringUtils::instance()->beginsWith('Foo', 'fo') ? 'true' : 'false') . PHP_EOL;
//prints: false
echo (\JimmyOak\Utility\StringUtils::instance()->beginsWith('Foo', 'fo', \JimmyOak\Utility\StringUtils::CASE_INSENSITIVE) ? 'true' : 'false') . PHP_EOL;
//returns: true

echo (\JimmyOak\Utility\StringUtils::instance()->beginsWith('Fóo', 'Fo') ? 'true' : 'false') . PHP_EOL;
//prints: false
echo (\JimmyOak\Utility\StringUtils::instance()->beginsWith('Fóo', 'Fo', \JimmyOak\Utility\StringUtils::ACCENT_INSENSITIVE) ? 'true' : 'false') . PHP_EOL;
//returns: true

echo (\JimmyOak\Utility\StringUtils::instance()->beginsWith(
        'Fóo',
        'fo',
        \JimmyOak\Utility\StringUtils::ACCENT_INSENSITIVE | \JimmyOak\Utility\StringUtils::CASE_INSENSITIVE
    ) ? 'true' : 'false') . PHP_EOL;
//returns: true

EndsWith

Same behaviour as beginsWith but with ending needle., (*8)

RemoveAccents

echo \JimmyOak\Utility\StringUtils::instance()->removeAccents('Fóôñ');
// prints: Foon

RemoveExtraSpaces

echo \JimmyOak\Utility\StringUtils::instance()->removeExtraSpaces('  Foo    Bar     ');
// prints: Foo Bar

IsUrl and IsEmail

echo (\JimmyOak\Utility\StringUtils::instance()->isUrl('http://github.com/jimmyoak') ? 'true' : 'false') . PHP_EOL;
// prints: true

echo (\JimmyOak\Utility\StringUtils::instance()->isUrl('github.com/jimmyoak') ? 'true' : 'false') . PHP_EOL;
// prints: false

echo (\JimmyOak\Utility\StringUtils::instance()->isEmail('adrian.robles.maiz@gmail.com') ? 'true' : 'false') . PHP_EOL;
// prints: true

echo (\JimmyOak\Utility\StringUtils::instance()->isEmail('adrian.robles.maiz') ? 'true' : 'false') . PHP_EOL;
// prints: false

FileUtils

ExtensionIs

echo (\JimmyOak\Utility\FileUtils::instance()->extensionIs('foo.php', 'php') ? 'true' : 'false') . PHP_EOL;
// prints: true

echo (\JimmyOak\Utility\FileUtils::instance()->extensionIs('foo.php', 'bar') ? 'true' : 'false') . PHP_EOL;
// prints: false

MakePath

echo \JimmyOak\Utility\FileUtils::instance()->makePath('/some', 'awesome/', 'and/incredible', 'nice.file');
// prints: /some/awesome/and/incredible/nice.file

GetExtension

echo \JimmyOak\Utility\FileUtils::instance()->getExtension('file.php');
// prints: php

GetNameWithoutExtension

echo \JimmyOak\Utility\FileUtils::instance()->getExtension('file.php');
// prints: file

ScanDir

See FileUtilsTest better :P, (*9)


Quality

To run the PHPUnit tests at the command line, go to the tests directory and issue phpunit., (*10)

This library attempts to comply with PSR-2 and PSR-4., (*11)

If you notice compliance oversights, please send a patch via pull request., (*12)

Contribute

Contributions to the package are always welcome!, (*13)

Authors

  • [Adrián Robles Maiz (a.k.a Jimmy K. Oak)] (http://github.com/jimmyoak)

License

The code base is licensed under the MIT license., (*14)

The Versions

25/01 2017

dev-master

9999999-dev

  Sources   Download

MIT

The Requires

 

The Development Requires

collection utilities jimmy jimmyoak

25/01 2017

3.3.0

3.3.0.0

  Sources   Download

MIT

The Requires

 

The Development Requires

collection utilities jimmy jimmyoak

28/12 2016

3.2.0

3.2.0.0

  Sources   Download

MIT

The Requires

 

The Development Requires

collection utilities jimmy jimmyoak

27/12 2016

3.1.0

3.1.0.0

  Sources   Download

MIT

The Requires

 

The Development Requires

collection utilities jimmy jimmyoak

27/12 2016

3.1.1

3.1.1.0

  Sources   Download

MIT

The Requires

 

The Development Requires

collection utilities jimmy jimmyoak

26/12 2016

3.0.0

3.0.0.0

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

collection utilities jimmy jimmyoak

20/01 2016

dev-master-php53-compatibility

dev-master-php53-compatibility

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

The Development Requires

collection utilities jimmy jimmyoak

20/01 2016

2.5.1b

2.5.1.0-beta

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

The Development Requires

collection utilities jimmy jimmyoak

20/01 2016

2.5.1

2.5.1.0

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

collection utilities jimmy jimmyoak

20/01 2016

2.5.0

2.5.0.0

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

collection utilities jimmy jimmyoak

23/09 2015

2.4.0

2.4.0.0

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

collection utilities jimmy jimmyoak

21/09 2015

2.3.7

2.3.7.0

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

collection utilities jimmy jimmyoak

21/09 2015

2.3.6

2.3.6.0

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

collection utilities jimmy jimmyoak

21/09 2015

2.3.5

2.3.5.0

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

collection utilities jimmy jimmyoak

16/09 2015

2.3.4

2.3.4.0

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

collection utilities jimmy jimmyoak

04/09 2015

2.3.3

2.3.3.0

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

collection utilities jimmy jimmyoak

04/09 2015

2.3.2

2.3.2.0

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

collection utilities jimmy jimmyoak

31/08 2015

2.3.1

2.3.1.0

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

collection utilities jimmy jimmyoak

27/08 2015

2.3.0

2.3.0.0

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

collection utilities jimmy jimmyoak

25/08 2015

2.2.0

2.2.0.0

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

collection utilities jimmy jimmyoak

24/08 2015

2.1.2

2.1.2.0

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

collection utilities jimmy jimmyoak

20/08 2015

2.1.1

2.1.1.0

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

collection utilities jimmy jimmyoak

20/08 2015

2.1.0

2.1.0.0

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

collection utilities jimmy jimmyoak

26/06 2015

2.0.0

2.0.0.0

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

collection utilities jimmy jimmyoak

20/06 2015

1.7.3

1.7.3.0

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

utilities instantiator arrayed jimmy jimmyoak

17/06 2015

1.7.2

1.7.2.0

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

utilities instantiator arrayed jimmy jimmyoak

17/06 2015

1.7.1

1.7.1.0

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

utilities instantiator arrayed jimmy jimmyoak

17/06 2015

1.7.0

1.7.0.0

  Sources   Download

The Requires

  • php >=5.4.0

 

The Development Requires

16/06 2015

1.6.2

1.6.2.0

  Sources   Download

The Requires

  • php >=5.4.0

 

The Development Requires

11/06 2015

1.6.1

1.6.1.0

  Sources   Download

The Requires

  • php >=5.4.0

 

The Development Requires

10/06 2015

1.6.0

1.6.0.0

  Sources   Download

The Requires

  • php >=5.4.0

 

The Development Requires

08/06 2015

1.5.1

1.5.1.0

  Sources   Download

The Requires

  • php >=5.4.0

 

The Development Requires

05/06 2015

1.5.0

1.5.0.0

  Sources   Download

The Requires

  • php >=5.4.0

 

The Development Requires

29/05 2015

1.4.0

1.4.0.0

  Sources   Download

The Requires

  • php >=5.4.0

 

The Development Requires

29/05 2015

1.3.6

1.3.6.0

  Sources   Download

The Requires

  • php >=5.4.0

 

The Development Requires

29/05 2015

1.3.5

1.3.5.0

  Sources   Download

The Requires

  • php >=5.4.0

 

The Development Requires

29/05 2015

1.3.4

1.3.4.0

  Sources   Download

The Requires

  • php >=5.4.0

 

The Development Requires

20/05 2015

1.3.3

1.3.3.0

  Sources   Download

The Requires

  • php >=5.4.0

 

The Development Requires

20/05 2015

1.3.2

1.3.2.0

  Sources   Download

The Requires

  • php >=5.4.0

 

The Development Requires

20/05 2015

1.3.1

1.3.1.0

  Sources   Download

The Requires

  • php >=5.4.0

 

The Development Requires

20/05 2015

1.3.0

1.3.0.0

  Sources   Download

The Requires

  • php >=5.4.0

 

The Development Requires

20/05 2015

1.2.3

1.2.3.0

  Sources   Download

The Requires

  • php >=5.4.0

 

The Development Requires

19/05 2015

1.2.2

1.2.2.0

  Sources   Download

The Requires

  • php >=5.4.0

 

The Development Requires

18/05 2015

1.2.1

1.2.1.0

  Sources   Download

The Requires

  • php >=5.4.0

 

The Development Requires

12/05 2015

1.1.2

1.1.2.0

  Sources   Download

The Requires

  • php >=5.4.0

 

The Development Requires

12/05 2015

1.2.0

1.2.0.0

  Sources   Download

The Requires

  • php >=5.4.0

 

The Development Requires

12/05 2015

1.1.1

1.1.1.0

  Sources   Download

The Requires

  • php >=5.4.0

 

The Development Requires

12/05 2015

1.0.2

1.0.2.0

  Sources   Download

The Requires

  • php >=5.4.0

 

The Development Requires

12/05 2015

1.1.0

1.1.0.0

  Sources   Download

The Requires

  • php >=5.4.0

 

The Development Requires