You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Nov 23, 2024. It is now read-only.
Copy file name to clipboardExpand all lines: README.md
+46Lines changed: 46 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -51,7 +51,53 @@ To initiate GetSet simply `use` the trait within your class
51
51
52
52
## Usage
53
53
54
+
Once setup, simply set or get any variables on the object you need.
54
55
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`.
0 commit comments