thirteen / fetchable
Convert classes into dynamic objects.
This package's canonical repository appears to be gone and the package has been frozen as a result.
1.0.3
2016-03-12 16:25 UTC
Requires
- illuminate/support: ^5.2
Requires (Dev)
- phpunit/phpunit: ^5.2
This package is not auto-updated.
Last update: 2020-02-05 02:18:05 UTC
README
This is just a trait to give the laravel-esque model attributes.
Create a new Fetchable class
class Doctor
{
use FetchableProperties;
}
$doctor = new Doctor([
'name' => 'Doctor Who?',
]);
$doctor->name; // Doctor Who?
Accessor
Here's a way to modify the data when fetching the attribute.
class Doctor
{
use FetchableProperties;
public function getNameAttribute() : string
{
return 'John Smith';
}
}
$doctor = new Doctor([
'name' => 'Doctor Who?',
]);
$doctor->name; // John Smith
Mutator
Here we'll change the data before storing it to the attributes array.
class Doctor
{
use FetchableProperties;
public function setNameAttribute($value)
{
$this->attributes['name'] = 'I am ' . $value;
// or
$this->name = 'I am ' . $value;
}
}
$doctor = new Doctor([
'name' => 'John Smith',
]);
$doctor->name; // I am John Smith
Convert to Array
Let's convert the data into an array.
class Doctor
{
use FetchableProperties;
public function setNameAttribute($value)
{
$this->attributes['name'] = 'I am ' . $value;
// or
$this->name = 'I am ' . $value;
}
}
$doctor = new Doctor([
'name' => 'John Smith',
]);
$doctor->toArray(); // ['name' => 'I am John Smith']