Base class for working with collections of things.
You can either create instances of the Collection object...
$foo = new \werx\Collections\Collection;
Or create a class that extends it.
class Foo extends \werx\Collections\Collection
{
}
$foo = new Foo();
$foo = new new \werx\Collections\Collection(['foo' => 'Foo', 'bar' => 'bar']);
$foo = new Foo();
$foo->set(['foo' => 'Foo', 'bar' => 'bar']);
var_dump($foo->count());
# OR
var_dump(count($foo)); // The Collection class implements the Countable() interface.
// 2
var_dump($foo->has('foo'));
// true
var_dump($foo->has('x'));
// false
var_dump($foo->get('foo'));
// Foo
var_dump($foo->get('x'));
// null
var_dump($foo->get('x', 'no'));
// no
$foo->set('name', 'Josh');
$foo->add('Josh');
$foo->add(['name' => 'Josh', 'location' => 'Arkansas']);
$foo->remove('name');
$foo->all();
$foo->toArray();
var_dump($foo->toJson());
// {"foo":"Foo","bar":"Bar"}
var_dump((string) $foo);
// {"foo":"Foo","bar":"Bar"}
$foo->clear();
Installation of this package is easy with Composer. If you aren't familiar with the Composer Dependency Manager for PHP, you should read this first.
"require": {
"werx/collections": "dev-master"
}
$ vendor/bin/phpunit
This library uses PHP_CodeSniffer to ensure coding standards are followed.
I have adopted the PHP FIG PSR-2 Coding Standard EXCEPT for the tabs vs spaces for indentation rule. PSR-2 says 4 spaces. I use tabs. No discussion.
To support indenting with tabs, I've defined a custom PSR-2 ruleset that extends the standard PSR-2 ruleset used by PHP_CodeSniffer. You can find this ruleset in the root of this project at PSR2Tabs.xml
Executing the codesniffer command from the root of this project to run the sniffer using these custom rules.
$ ./codesniffer