Permalink
Browse files

Saves original element name (singular) to $element_name and now uses …

…$element_name_plural to store plural version
  • Loading branch information...
1 parent 7eed470 commit 3ee5fcc2b127a5f0ba3a25d331de51eceab5b2a2 @jbroadway committed Oct 5, 2011
Showing with 19 additions and 13 deletions.
  1. +18 −12 ActiveResource.php
  2. +1 −1 tests/ActiveResource.php
View
@@ -66,10 +66,15 @@ class ActiveResource {
var $password = null;
/**
- * The remote collection, e.g., person or things
+ * The remote collection, e.g., person or thing
*/
var $element_name = false;
+ /**
+ * Pleural form of the element name, e.g., people or things
+ */
+ var $element_name_plural = '';
+
/**
* The data of the current object, accessed via the anonymous get/set methods.
*/
@@ -152,7 +157,8 @@ class ActiveResource {
function __construct ($data = array ()) {
$this->_data = $data;
// Allow class-defined element name or use class name if not defined
- $this->element_name = ($this->element_name ? $this->pluralize ($this->element_name) : $this->pluralize (strtolower (get_class ($this))));
+ $this->element_name = $this->element_name ? $this->element_name : strtolower (get_class ($this));
+ $this->element_name_plural = $this->pluralize ($this->element_name);
// Detect for namespaces, and take just the class name
if (stripos($this->element_name, '\\'))
@@ -203,9 +209,9 @@ function pleuralize ($word) {
*/
function save () {
if (isset ($this->_data['id'])) {
- return $this->_send_and_receive ($this->site . $this->element_name . '/' . $this->_data['id'] . '.xml', 'PUT', $this->_data); // update
+ return $this->_send_and_receive ($this->site . $this->element_name_plural . '/' . $this->_data['id'] . '.xml', 'PUT', $this->_data); // update
}
- return $this->_send_and_receive ($this->site . $this->element_name . '.xml', 'POST', $this->_data); // create
+ return $this->_send_and_receive ($this->site . $this->element_name_plural . '.xml', 'POST', $this->_data); // create
}
/**
@@ -214,7 +220,7 @@ function save () {
* DELETE /collection/id.xml
*/
function destroy () {
- return $this->_send_and_receive ($this->site . $this->element_name . '/' . $this->_data['id'] . '.xml', 'DELETE');
+ return $this->_send_and_receive ($this->site . $this->element_name_plural . '/' . $this->_data['id'] . '.xml', 'DELETE');
}
/**
@@ -228,13 +234,13 @@ function find ($id = false, $options = array ()) {
$id = $this->_data['id'];
}
if ($id == 'all') {
- $url = $this->site . $this->element_name . '.xml';
+ $url = $this->site . $this->element_name_plural . '.xml';
if (count ($options) > 0) {
$url .= '?' . http_build_query ($options);
}
return $this->_send_and_receive ($url, 'GET');
}
- return $this->_send_and_receive ($this->site . $this->element_name . '/' . $id . '.xml', 'GET');
+ return $this->_send_and_receive ($this->site . $this->element_name_plural . '/' . $id . '.xml', 'GET');
}
/**
@@ -244,7 +250,7 @@ function find ($id = false, $options = array ()) {
* GET /collection/id/method.xml?attr=value
*/
function get ($method, $options = array ()) {
- $req = $this->site . $this->element_name;
+ $req = $this->site . $this->element_name_plural;
if ($this->_data['id']) {
$req .= '/' . $this->_data['id'];
}
@@ -261,7 +267,7 @@ function get ($method, $options = array ()) {
* POST /collection/id/method.xml
*/
function post ($method, $options = array ()) {
- $req = $this->site . $this->element_name;
+ $req = $this->site . $this->element_name_plural;
if ($this->_data['id']) {
$req .= '/' . $this->_data['id'];
}
@@ -275,7 +281,7 @@ function post ($method, $options = array ()) {
* PUT /collection/id/method.xml
*/
function put ($method, $options = array ()) {
- $req = $this->site . $this->element_name;
+ $req = $this->site . $this->element_name_plural;
if ($this->_data['id']) {
$req .= '/' . $this->_data['id'];
}
@@ -339,7 +345,7 @@ function _xml_entities ($string) {
*/
function _send_and_receive ($url, $method, $data = array ()) {
$params = '';
- $el = substr ($this->element_name, 0, -1);
+ $el = $this->element_name; // Singular this time
if ($this->request_format == 'url') {
foreach ($data as $k => $v) {
if ($k != 'id' && $k != 'created-at' && $k != 'updated-at') {
@@ -395,7 +401,7 @@ function _send_and_receive ($url, $method, $data = array ()) {
// parse XML response
$xml = new SimpleXMLElement ($res);
- if ($xml->getName () == $this->element_name) {
+ if ($xml->getName () == $this->element_name_plural) {
// multiple
$res = array ();
$cls = get_class ($this);
View
@@ -13,7 +13,7 @@ function test_construct () {
$t->foo = 'asdf';
$this->assertEquals ($t->foo, 'asdf');
$this->assertEquals ($t->_data, array ('foo' => 'asdf'));
- $this->assertEquals ($t->element_name, 'tests');
+ $this->assertEquals ($t->element_name_plural, 'tests');
}
function test_build_xml () {

0 comments on commit 3ee5fcc

Please sign in to comment.