You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@shindig.apache.org by ch...@apache.org on 2008/05/19 15:06:08 UTC

svn commit: r657816 - in /incubator/shindig/trunk/php/src/socialdata: DataResponse.php ResponseItem.php

Author: chabotc
Date: Mon May 19 06:06:08 2008
New Revision: 657816

URL: http://svn.apache.org/viewvc?rev=657816&view=rev
Log:
Trim NULL values from social data json responses

Modified:
    incubator/shindig/trunk/php/src/socialdata/DataResponse.php
    incubator/shindig/trunk/php/src/socialdata/ResponseItem.php

Modified: incubator/shindig/trunk/php/src/socialdata/DataResponse.php
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/php/src/socialdata/DataResponse.php?rev=657816&r1=657815&r2=657816&view=diff
==============================================================================
--- incubator/shindig/trunk/php/src/socialdata/DataResponse.php (original)
+++ incubator/shindig/trunk/php/src/socialdata/DataResponse.php Mon May 19 06:06:08 2008
@@ -26,6 +26,10 @@
 	{
 		$this->error = $error;
 		$this->responses = $responses;
+		if ($this->error === null) {
+			// trim NULL values here too
+			unset($this->error);
+		}
 	}
 
 	public function getResponses()

Modified: incubator/shindig/trunk/php/src/socialdata/ResponseItem.php
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/php/src/socialdata/ResponseItem.php?rev=657816&r1=657815&r2=657816&view=diff
==============================================================================
--- incubator/shindig/trunk/php/src/socialdata/ResponseItem.php (original)
+++ incubator/shindig/trunk/php/src/socialdata/ResponseItem.php Mon May 19 06:06:08 2008
@@ -30,7 +30,42 @@
 	{
 		$this->error = $error;
 		$this->errorMessage = $errorMessage;
-		$this->response = $response;
+		$this->response = $this->trimResponse($response);
+		if ($this->error === null && $this->errorMessage === null) {
+			// trim null values of self too
+			unset($this->error);
+			unset($this->errorMessage);
+		}
+	}
+	
+	/**
+	 * the json_encode function does not trim null values,
+	 * so we do this manually
+	 *
+	 * @param mixed $object
+	 */
+	private function trimResponse(&$object)
+	{
+		if (is_array($object)) {
+			foreach ($object as $key => $val) {
+				// binary compare, otherwise false == 0 == null too
+				if ($val === null) {
+					unset($object[$key]);
+				} elseif (is_array($val) || is_object($val)) {
+					$object[$key] = $this->trimResponse($val);
+				}
+			}
+		} elseif (is_object($object)) {
+			$vars = get_object_vars($object);
+			foreach ($vars as $key => $val) {
+				if ($val === null) {
+					unset($object->$key);
+				} elseif (is_array($val) || is_object($val)) {
+					$object->$key = $this->trimResponse($val);
+				}
+			}
+		}
+		return $object;
 	}
 	
 	public function getError()