6.1. Data Traits
- 6.1.1. ArrayAccess Trait (4)
- 6.1.2. Countable Trait (1)
- 6.1.3. Iterator Trait (5)
- 6.1.4. Magic Trait (4)
- 6.1.5. Dot Trait (4)
- 6.1.6. Generator Trait (1)
- 6.1.7. Data Trait
This section is all about data management, all of which perform tasks on a
singular protected property called $data
.
6.1.1. ArrayAccess Trait
The ArrayAccess trait wraps the ArrayAccess interface of PHP and when mounted will allow your class to be accessed like an array. Mounting this trait can be done with the following example.
use Cradle\Data\ArrayAccessTrait
class MyClass
{
use ArrayAccessTrait;
protected $data = array();
}
The ArrayAccess trait implements the ArrayAccess interface exactly adding
offsetSet
, offsetGet
, offsetExists
, offsetUnset
methods to your class
which makes the following now possible.
$myClass = new MyClass;
if(!isset($myClass['foo'])) {
$myClass['foo'] = 'bar';
}
echo $myClass['foo']; //--> 'bar'
unset($myClass['foo']);
6.1.2. Countable Trait
The Countable trait wraps the Countable interface of PHP and when mounted will allow your class to be counted like an array. Mounting this trait can be done with the following example.
use Cradle\Data\CountableTrait
class MyClass
{
use CountableTrait;
protected $data = array();
}
The Countable trait implements the Countable interface exactly adding the
count
method to your class which makes the following now possible.
$myClass = new MyClass;
echo count($myClass); //--> 0
6.1.3. Iterator Trait
The Iterator trait wraps the Iterator interface of PHP and when mounted will allow your class to be iterated like an array. Mounting this trait can be done with the following example.
use Cradle\Data\IteratorTrait
class MyClass
{
use IteratorTrait;
protected $data = array();
}
The Iterator trait implements the Iterator interface exactly adding
rewind
, current
, next
, key
, valid
methods to your class
which makes the following now possible.
$myClass = new MyClass;
foreach($myClass as $key => $value) {
//Do something
}
6.1.4. Magic Trait
The Magic trait adds extra accessors for the data array. Mounting this trait can be done with the following example.
use Cradle\Data\MagicTrait;
class MyClass
{
use MagicTrait {
MagicTrait::__getData as __get;
MagicTrait::__setData as __set;
MagicTrait::__callData as __call;
MagicTrait::__toStringData as __toString;
}
protected $data = array();
}
The Magic trait implements three magic methods adding __call
, __get
,
__set
methods to your class which makes the following now possible.
We suffixed the magic methods with `'Data'` as in `__getData` to keep the
methods free for you to define as you can always alias them.
$myClass = new MyClass;
$myClass->setFooBar('zoo'); // $data['foo_bar']
echo $myClass->getFooBar(); //--> 'zoo'
$myClass->foo_bar = 'zoo'; // $data['foo_bar']
echo $myClass->foo_bar; //--> 'zoo'
echo $myClass; //--> <JSON string>
6.1.5. Dot Trait
The Dot trait adds accessors to help manage three or more dimensional arrays. Mounting this trait can be done with the following example.
use Cradle\Data\DotTrait;
class MyClass
{
use DotTrait;
protected $data = array();
}
The Dot trait implements a four methods called getDot
, setDot
,
isDot
, removeDot
which can be used like the following
$myClass = new MyClass;
$myClass->setDot('some.path.to.value', 'foobar'); // $data['some']['path']['to']['value'] = 'foobar'
$myClass->getDot('some.path.to.value'); //--> 'foobar'
$myClass->getDot('some.path.to'); //--> ['value' => 'foobar']
$myClass->removeDot('some.path');
$myClass->isDot('some.path'); //--> false
All dot methods have an extra parameter where you can specify the delimeter.
$myClass->getDot('some-path-to-value', '-');
6.1.6. Generator Trait
The Generator trait takes advantage of PHP generators. Mounting this trait can be done with the following example.
use Cradle\Data\GeneratorTrait;
class MyClass
{
use GeneratorTrait;
protected $data = array();
}
The Generator trait implements a single method called generator
which can be
used like the following.
$myClass = new MyClass;
foreach($myClass->generator() as $key => $value) {
//Do something
}
6.1.7. Data Trait
The data trait combines all the aforementioned traits so you wouldn’t have to declare them one by one (on the off chance you would want to use all of them). Mounting this trait can be done with the following example.
use Cradle\Data\DataTrait;
class MyClass
{
use DataTrait;
}