Skip to content

Commit 975878c

Browse files
committed
Merge pull request #441 from jpfuentes2/igorsantos07-aliased_methods_in_serialization
Allows for aliased methods in serialization
2 parents b84bc4d + ae50f6e commit 975878c

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

lib/Serialization.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,13 @@ private function check_methods()
136136
{
137137
$this->options_to_a('methods');
138138

139-
foreach ($this->options['methods'] as $method)
139+
foreach ($this->options['methods'] as $method => $name)
140140
{
141+
if (is_numeric($method))
142+
$method = $name;
143+
141144
if (method_exists($this->model, $method))
142-
$this->attributes[$method] = $this->model->$method();
145+
$this->attributes[$name] = $this->model->$method();
143146
}
144147
}
145148
}

test/SerializationTest.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,21 @@ public function test_methods_takes_a_string()
6464
$this->assert_equals('ANCIENT ART OF MAIN TANKING', $a['upper_name']);
6565
}
6666

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

74+
public function test_methods_method_alias()
75+
{
76+
$a = $this->_a(array('methods' => array('name' => 'alias_name')));
77+
$this->assert_equals('ancient art of main tanking', $a['alias_name']);
78+
$a = $this->_a(array('methods' => array('upper_name' => 'name')));
79+
$this->assert_equals('ANCIENT ART OF MAIN TANKING', $a['name']);
80+
}
81+
7582
public function test_include()
7683
{
7784
$a = $this->_a(array('include' => array('author')));

0 commit comments

Comments
 (0)