Browse files

Merge pull request #9 from jbroadway/master

Saves original element name (singular) to $element_name and now uses $element_name_plural to store plural version
  • Loading branch information...
2 parents 22ba6d5 + 3ee5fcc commit 25555c4a751216fac89c8cc5be3a60130fca603a @jbroadway committed Oct 5, 2011
Showing with 26 additions and 14 deletions.
  1. +20 −13 ActiveResource.php
  2. +6 −1 tests/ActiveResource.php
View
33 ActiveResource.php
@@ -66,11 +66,16 @@ 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.
*/
var $_data = array ();
@@ -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, '\\'))
@@ -181,6 +187,7 @@ function pluralize ($word) {
$word = preg_replace ('/sises$/', 'ses', $word);
$word = preg_replace ('/([^aeiouy]|qu)ys$/', '\1ies', $word);
$word = preg_replace ('/(?:([^f])fe|([lr])f)s$/', '\1\2ves', $word);
+ $word = preg_replace ('/ieses$/', 'ies', $word);
if (isset ($this->pleural_corrections[$word])) {
return $this->pleural_corrections[$word];
}
@@ -202,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
}
/**
@@ -213,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');
}
/**
@@ -227,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');
}
/**
@@ -243,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'];
}
@@ -260,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'];
}
@@ -274,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'];
}
@@ -338,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') {
@@ -394,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);
@@ -455,7 +462,7 @@ function _fetch ($url, $method, $params) {
}
if ($this->request_format == 'xml') {
- curl_setopt ($ch, CURLOPT_HTTPHEADER, array ("Content-Type: text/xml", "Length: " . strlen ($params)));
+ curl_setopt ($ch, CURLOPT_HTTPHEADER, array ("Expect:", "Content-Type: text/xml", "Length: " . strlen ($params)));
}
switch ($method) {
case 'POST':
View
7 tests/ActiveResource.php
@@ -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 () {
@@ -66,6 +66,11 @@ function test_pleuralize () {
$this->assertEquals ($t->pluralize ('life'), 'lives');
$this->assertEquals ($t->pluralize ('wife'), 'wives');
$this->assertEquals ($t->pluralize ('song'), 'songs');
+ $this->assertEquals ($t->pluralize ('try'), 'tries');
+ $this->assertEquals ($t->pluralize ('tree'), 'trees');
+ $this->assertEquals ($t->pluralize ('tries'), 'tries');
+ $this->assertEquals ($t->pluralize ('entry'), 'entries');
+ $this->assertEquals ($t->pluralize ('entries'), 'entries');
}
}

0 comments on commit 25555c4

Please sign in to comment.