Skip to content
This repository was archived by the owner on Nov 23, 2024. It is now read-only.

Commit 96af03d

Browse files
committed
Finished readme
1 parent 47a9ef1 commit 96af03d

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,53 @@ To initiate GetSet simply `use` the trait within your class
5151

5252
## Usage
5353

54+
Once setup, simply set or get any variables on the object you need.
5455

56+
$MyClass->foo_bar = 'barfoo';
57+
58+
echo $MyClass->foo_bar; // barfoo
59+
60+
This will set the variable into a `$data` array added to your object via the GetSet trait.
61+
62+
### Variable Transformers
63+
64+
You can create custom get / set transformers to change a variables data as it gets added to or retrieved from the `$data` variable. These should be in the format `setXAttr` and `getXAttr` where `X` is a CamelCased version of the variable you are setting or getting from the object.
65+
66+
In the example above, basic getter / setter methods for `foo_bar` could look like:
67+
68+
public function setFooBarAttr($value)
69+
{
70+
$this->data['foo_bar'] = strtoupper($value);
71+
}
72+
73+
public function getFooBarAttr()
74+
{
75+
return strtolower($this->data['foo_bar']);
76+
}
77+
78+
## Mass data assignment
79+
80+
If you need to assign a full array of data to your object and wish for the variables to be passed through any custom setters there is the `setAllData` method.
81+
82+
$MyClass->setAllData([
83+
'foo_bar' => 'barfoo'
84+
]);
85+
86+
## Data Export
87+
88+
If you need to export all the data set within your object you can use 1 of 2 methods.
89+
90+
### toArray
91+
92+
$MyClass->toArray();
93+
94+
This will simply return all data set onto the object as an array.
95+
96+
### toJson
97+
98+
$MyClass->toJson();
99+
100+
This will return all the data set onto the object as a Json object. This first uses the toArray method to retrieve an array before returning the data through `json_encode`.
55101

56102

57103
## Changelog

0 commit comments

Comments
 (0)