Skip to content

Commit

Permalink
Added ability to choose data property
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita Chernyi committed May 4, 2017
1 parent 1ca3320 commit 5a5cd0e
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 4 deletions.
56 changes: 55 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# GetSetTrait ([Next gen of GetSetGo](https://github.com/rakshazi/GetSetGoImproved))
# GetSetTrait
A dynamic setter-getter library for PHP 5.4+.

You can use methods like `setFoo('bar')` and `getFoo()`, which you DON'T have to create (in your class).
Expand Down Expand Up @@ -49,3 +49,57 @@ var_dump($dummy);
```

**That's basically it.**

## Advanced usage

### Data property

If you want save all data in `$object->someProperty` array instead of saving each property as object's property (`$object->property_name`),
you can use `setDataProperty('data')` function, example:

```php
<?php
class Dummy
{
//Add ability to use dynamic getters and setters
use \Rakshazi\GetSetTrait;

public function __construct()
{
$this->setDataProperty('data');
}
}

//Init dummy class
$dummy = new Dummy;
//Set new var 'message_for_world' with value
$dummy->setMessageForWorld('Hello');
//Will return "Hello\n"
echo $dummy->getMessageForWorld()."\n";
//Will return the same text
echo $dummy->getData('message_for_world')."\n";
//Set new message for our world!
$dummy->setData('message_for_world', 'Bye-bye!');
//Will return "Bye-bye!\n"
echo $dummy->getData('message_for_world')."\n";
//Will set new var 'new_message'
$dummy->setNewMessage('Use me now!');
//Will show all object data
var_dump($dummy);
```

**Result** (all data saved in `data` property)

```
object(Dummy)#1 (2) {
["_data_property":"Dummy":private]=>
string(4) "data"
["data"]=>
array(2) {
["message_for_world"]=>
string(8) "Bye-bye!"
["new_message"]=>
string(11) "Use me now!"
}
}
```
39 changes: 36 additions & 3 deletions src/GetSetTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@
*/
trait GetSetTrait
{
/**
* Name of data property.
*
* @see $this::setDataProperty()
*
* @var null|string
*/
private $_data_property = null;

/**
* Call method or getter/setter for property.
*
Expand Down Expand Up @@ -49,13 +58,17 @@ public function __call($method = null, $params = [])
*
* @return mixed
*/
public function getData($property)
public function getData($property, $default = null)
{
if ($this->_data_property && isset($this->{$this->_data_property}[$property])) {
return $this->{$this->_data_property}[$property];
}

if (property_exists($this, $property)) {
return $this->$property;
}

return null;
return $default;
}

/**
Expand All @@ -68,7 +81,27 @@ public function getData($property)
*/
public function setData($property, $data = null)
{
$this->$property = $data;
if ($this->_data_property) {
$this->{$this->_data_property}[$property] = $data;
} else {
$this->$property = $data;
}

return $this;
}

/**
* If you want use getter and setter only for data array
* you can set property name with that function.
*
* @param string $property
*
* @return $this
*/
public function setDataProperty(string $property)
{
$this->_data_property = $property;
$this->$property = [];

return $this;
}
Expand Down

0 comments on commit 5a5cd0e

Please sign in to comment.