Skip to content

Commit

Permalink
Merge pull request #441 from jpfuentes2/igorsantos07-aliased_methods_…
Browse files Browse the repository at this point in the history
…in_serialization

Allows for aliased methods in serialization
  • Loading branch information
koenpunt committed Dec 6, 2014
2 parents b84bc4d + ae50f6e commit 975878c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
7 changes: 5 additions & 2 deletions lib/Serialization.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,13 @@ private function check_methods()
{
$this->options_to_a('methods');

foreach ($this->options['methods'] as $method)
foreach ($this->options['methods'] as $method => $name)
{
if (is_numeric($method))
$method = $name;

if (method_exists($this->model, $method))
$this->attributes[$method] = $this->model->$method();
$this->attributes[$name] = $this->model->$method();
}
}
}
Expand Down
11 changes: 9 additions & 2 deletions test/SerializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,21 @@ public function test_methods_takes_a_string()
$this->assert_equals('ANCIENT ART OF MAIN TANKING', $a['upper_name']);
}

// methods added last should we shuld have value of the method in our json
// rather than the regular attribute value
// methods should take precedence over attributes
public function test_methods_method_same_as_attribute()
{
$a = $this->_a(array('methods' => 'name'));
$this->assert_equals('ancient art of main tanking', $a['name']);
}

public function test_methods_method_alias()
{
$a = $this->_a(array('methods' => array('name' => 'alias_name')));
$this->assert_equals('ancient art of main tanking', $a['alias_name']);
$a = $this->_a(array('methods' => array('upper_name' => 'name')));
$this->assert_equals('ANCIENT ART OF MAIN TANKING', $a['name']);
}

public function test_include()
{
$a = $this->_a(array('include' => array('author')));
Expand Down

0 comments on commit 975878c

Please sign in to comment.