-
Notifications
You must be signed in to change notification settings - Fork 88
Repository
Repository is an abstract class extended by all type classes. It's the core of Underscore.php and is what allows you to both chain and do static calls of the methods, as well as switching from types to types.
The Arrays
class wil be used in the following examples but you can replace it with any of the type classes.
Repositories can be created the following ways :
With Arrays::create()
or new Arrays
. This will create an empty repository, containing a subject matching the type you called (so new Arrays
has array()
as subject).
With Arrays::from($data)
or new Arrays($data)
– note this will convert the existing data to make it match the type of the class, so objects passed to Arrays::from
will become arrays.
Repositories have special properties that allow you to manipulate their subject. This make working with both arrays and objects smoother by allowing you to use $repository->property
and $repository->property = 'something'
, whether the subject is an array or an object.
You can replace the subject of a repository at any time by doing $repository->setSubject($newSubject)
.
You can also check if a repository's subject is empty by doing $repository->isEmpty
.
Finally to get the subject of a repository, do $repository->obtain()
.
Repositories will also use Underscore's Parse::toString
method when the __toString()
method is being called on them. Meaning the following :
echo Arrays::from(['foo' => 'bar']) // Prints out {"foo":"bar"}
Although you can create a repository from any type class, you're not limited to the methods of that class. Underscore will automatically check the type of the subject and call the right class, so if per example you do Arrays::from()->size()
and thus have an integer as subject, you can then call the Number
methods on it.