selene / common
Selene common components
Requires
- php: >=5.4.0
Requires (Dev)
- league/phpunit-coverage-listener: dev-master
- selene/cryptography: dev-development
- selene/testsuite: dev-development
This package is not auto-updated.
Last update: 2015-12-05 10:10:37 UTC
README
The Common component is shared across almost ever other selene component.
Installation
Installation is done via composer.
Add selene/events
as requirement to your composer.json
file.
{ "require": { "selene/common":"dev-development" } }
Run composer install
or composer update
$ composer install --dev
Testing
Run tests with:
$ vendor/bin/phpunit
Data Structures
List
Lists are inspired by python lists
Usage<?php use \Selene\Components\Common\Data\BaseList; $list = new BaseList(1, 2, 3); // do operations $values = $list->toArray();append()
Adding values to the end of the list
<?php $list->append('foo'); // [1, 2, 3, 'foo'] // or $list->add('foo'); // [1, 2, 3, 'foo']insert()
Inserting a value a a specified index
<?php $list->insert(4, 3); // [1, 2, 3, 4, 'foo']pop()
Return the last value and remove it from the list.
<?php $list->pop(); // 'foo'pop($index)
Return a value at a specified index remove it from the list.
<?php $list->pop(2); // 3remove($value)
Remove a value from the list.
<?php $list->remove(1); // [2, 4]countValue($value)
Count the occurance of a value.
<?php $list->countValue(2); // 1sort()
Sort all values.
<?php $list->sort();reverse()
Reverse the list order.
<?php $list->reverse(); // [4, 2]extend($list)
Extend a list with a nother.
<?php $newList = new BaseList('foo', 'bar'); $list->extend($newList);
Collection
<?php use \Selene\Components\Common\Data\Collection; $collection = new Collection;
Traits
Getter
The Getter
trait contains a few accessor method for dealing with retreiving
data from an array.
Return a value from a dataset if the value is set for a given key, otherwies return a default value.
<?php use Getter; private $attributes = []; public function get($attribute, $default = null); { return $this->getDefault($this->attributes, $attribute, $default); }getDefaultUsingKey($data, $key, $defaultValue [optional])
Return a value from a dataset if the key is present, otherwies return a default value.
<?php use Getter; private $attributes = []; public function get($attribute, $default = null); { return $this->getDefaultUsingKey($this->attributes, $attribute, $default); }getDefaultUsing($data, $key, $closure [optional])
Return a value from a dataset if the value is set for a given key, otherwies return a default value using a callback function.
<?php use Getter; private $attributes = []; public function get($attribute); { return $this->getDefaultUsing($this->attributes, $attribute, function () { // get some default value return $result; }); }getDefaultArray($data, $key, $defaultValue [optional])
Return a value from a multi-dimensional dataset if the key is present, otherwies return a default
value. You can specify a key delimitter to access array keys, e.g. foo.bar
will search for a value in ['foo' => ['bar' => 12]]
<?php use Getter; private $attributes = []; public function get($attribute); { return $this->getDefaultArray($this->attributes, $attribute, $default, '.'); }