Skip to content

Commit

Permalink
Bugfix, added exception if method is not implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita Chernyi committed May 4, 2017
1 parent 01ccc81 commit eb81bc4
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 22 deletions.
11 changes: 3 additions & 8 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@

# Created by https://www.gitignore.io/api/composer

### Composer ###
composer.phar
composer.lock
test.php
.php_cs.cache
/vendor/

# Commit your application's lock file http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
# composer.lock
12 changes: 12 additions & 0 deletions .php_cs.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
$finder = \PhpCsFixer\Finder::create()
->in(__DIR__.'/src');

return \PhpCsFixer\Config::create()
->setRules(array(
'@PSR1' => true,
'@PSR2' => true,
'@Symfony' => true,
'array_syntax' => ['syntax' => 'short'],
))
->setFinder($finder);
41 changes: 27 additions & 14 deletions src/GetSetTrait.php
Original file line number Diff line number Diff line change
@@ -1,42 +1,53 @@
<?php

namespace Rakshazi;

/**
* Dynamic getter/setter library for PHP 5.4+
* Dynamic getter/setter library for PHP 5.4+.
*
* Changelog:
* Current realization - rakshazi/get-set-trait
* First realization - rakshazi/get-set-go-improved
* Idea - usmanhalalit/get-set-go
*
* @link https://github.com/rakshazi/GetSetTrait/blob/master/README.md
* @see https://github.com/rakshazi/GetSetTrait/blob/master/README.md
*/
trait GetSetTrait
{
/**
* Call method or getter/setter for property
* Call method or getter/setter for property.
*
* @param string $method
* @param mixed $data
* @param mixed $data
*
* @return mixed Data from object property
*
* @throws \Exception if method not implemented in class
*/
public function __call($method = null, $params = array())
public function __call($method = null, $params = [])
{
$parts = preg_split('/([A-Z][^A-Z]*)/', $method, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
$type = array_shift($parts);

if ($type == 'get' || $type = 'set') {
if ($type == 'get' || $type == 'set') {
$property = strtolower(implode('_', $parts));
$params = (isset($params[0])) ? array($property, $params[0]) : array($property);
return call_user_func_array(array($this, $type . 'Data'), $params);
$params = (isset($params[0])) ? [$property, $params[0]] : [$property];

return call_user_func_array([$this, $type.'Data'], $params);
}
if (method_exists($this, $method)) {
return call_user_func_array([$this, $method], $params);
}

return call_user_func_array(array($this, $method), $params);
throw new \Exception('Method "'.$method.'" not implemented.');
}

/**
* Get property data, eg getData('post_id')
* Get property data, eg getData('post_id').
*
* @param string $property
* @return $this
*
* @return mixed
*/
public function getData($property)
{
Expand All @@ -48,15 +59,17 @@ public function getData($property)
}

/**
* Set property data, eg getData('post_id',1)
* Set property data, eg getData('post_id',1).
*
* @param string $property
* @param mixed $data
* @param mixed $data
*
* @return $this
*/
public function setData($property, $data = null)
{
$this->$property = $data;

return $this;
}
}

0 comments on commit eb81bc4

Please sign in to comment.