Ouzo Goodies
What is it
Utility classes, test assertions and mocking framework extracted from Ouzo framework. We are compatible with PHP 7.2 and later., (*1)
, (*2)
How to use it
Couple of examples., (*3)
Fluent arrays:, (*4)
$result = FluentArray::from($users)
->map(Functions::extractField('name'))
->filter(Functions::notEmpty())
->unique()
->toArray();
Fluent iterator:, (*5)
$result = FluentIterator::fromArray([1, 2, 3])
->cycle()
->limit(10)
->reindex()
->toArray(); // [1, 2, 3, 1, 2, 3, 1, 2, 3, 1]
Fluent functions:, (*6)
$product = new Product(['name' => 'super phone']);
$function = FluentFunctions::extractField('name')
->removePrefix('super')
->prepend(' extra')
->append('! ')
->surroundWith("***");
$result = Functions::call($function, $product); //=> '*** extra phone! ***'
$phones = Arrays::filter($products, FluentFunctions::extractField('type')->equals('PHONE'));
Extract (from Functions):, (*7)
$cities = Arrays::map($users, Functions::extract()->getAddress('home')->city);
Clock:, (*8)
$string = Clock::now()
->plusYears(1)
->plusMonths(2)
->minusDays(3)
->format();
Comparators:, (*9)
$product1 = new Product(['name' => 'b']);
$product2 = new Product(['name' => 'c']);
$product3 = new Product(['name' => 'a']);
$result = Arrays::sort([$product1, $product2, $product3], Comparator::compareBy('name'));
Fluent assertions for arrays:, (*10)
$animals = ['cat', 'dog', 'pig'];
Assert::thatArray($animals)->hasSize(3)->contains('cat');
Fluent assertions for strings:, (*11)
Assert::thatString("Frodo")
->startsWith("Fro")
->endsWith("do")
->contains("rod")
->doesNotContain("fro")
->hasSize(5);
Mocking:, (*12)
$mock = Mock::create();
Mock::when($mock)->someMethod('arg')->thenReturn('123');
$result = $mock->someMethod('arg');
$this->assertEquals('123', $result);
Mock::verify($mock)->method('arg');
Exception assertions:, (*13)
$foo = new Foo();
CatchException::when($foo)->method();
CatchException::assertThat()->isInstanceOf("FooException");
This is just a taste of Ouzo. Look at the documentation for more goodies., (*14)
Where to get it
Download from github or simply add composer dependency:, (*15)
composer require letsdrink/ouzo-goodies
Ouzo Goodies at packagist., (*16)
Documentation
Tutorials:
* Functional programming with Ouzo, (*17)
Utilities:
* Arrays - Helper functions for arrays.
* FluentArray - Interface for manipulating arrays in a chained fashion.
* Iterators - Helper functions for iterators.
* FluentIterator- Interface for manipulating iterators in a chained fashion.
* Strings - Helper functions for strings.
* Objects- Helper functions that can operate on any PHP object.
* Functions - Static utility methods returning closures that can be used with Arrays and FluentArray, or other PHP functions.
* FluentFunctions - Fluent utility for function composition.
* Cache - General-purpose cache.
* Path - Helper functions for path operations.
* Clock - DateTime replacement.
* Comparators - Sorting., (*18)
Tests:
* Assertions for arrays
* Assertions for exceptions
* Assertions for strings
* Mocking
* Testing time-dependent code, (*19)
Check out full docs at http://ouzo.readthedocs.org, (*20)
PhpStorm plugins:
For ideas, questions, discussions write to ouzo-framework@googlegroups.com., (*21)
Support for PHP 5.6, 7.0 and 7.1
Ouzo has dropped support for PHP versions older than 7.2 since Ouzo 2.x. If you want to use Ouzo with PHP 5.6, 7.0 or 7.1, please try Ouzo 1.x branch., (*22)