Navigation Menu

Skip to content

Commit

Permalink
Merge pull request #9 from jbroadway/master
Browse files Browse the repository at this point in the history
Saves original element name (singular) to $element_name and now uses $element_name_plural to store plural version
  • Loading branch information
jbroadway committed Oct 5, 2011
2 parents 22ba6d5 + 3ee5fcc commit 25555c4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
33 changes: 20 additions & 13 deletions ActiveResource.php
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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, '\\'))
Expand Down Expand Up @@ -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];
}
Expand All @@ -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
}

/**
Expand All @@ -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');
}

/**
Expand All @@ -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');
}

/**
Expand All @@ -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'];
}
Expand All @@ -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'];
}
Expand All @@ -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'];
}
Expand Down Expand Up @@ -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') {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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':
Expand Down
7 changes: 6 additions & 1 deletion tests/ActiveResource.php
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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');
}
}

Expand Down

0 comments on commit 25555c4

Please sign in to comment.