Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit 48fa5fc4f50cfe48734017540dd6474868faadf6 Mashape committed Nov 22, 2010
Showing with 4,121 additions and 0 deletions.
  1. +662 −0 LICENSE
  2. +4 −0 README
  3. +55 −0 api.php
  4. +43 −0 api.xml
  5. +125 −0 mashape/configuration/helpers/loadMethods.php
  6. +117 −0 mashape/configuration/helpers/loadObjects.php
  7. +52 −0 mashape/configuration/restConfiguration.php
  8. +110 −0 mashape/configuration/restConfigurationLoader.php
  9. +66 −0 mashape/configuration/restField.php
  10. +66 −0 mashape/configuration/restMethod.php
  11. +45 −0 mashape/configuration/restObject.php
  12. +81 −0 mashape/exceptions/exceptionMessages.php
  13. +37 −0 mashape/exceptions/mashapeException.php
  14. +39 −0 mashape/init/headers.php
  15. +33 −0 mashape/init/init.php
  16. +40 −0 mashape/init/json.php
  17. +30 −0 mashape/init/session.php
  18. +806 −0 mashape/json/jsonImpl.php
  19. +61 −0 mashape/json/jsonUtils.php
  20. +51 −0 mashape/mashape.php
  21. +44 −0 mashape/mashapeAPIError.php
  22. +101 −0 mashape/methods/call/call.php
  23. +69 −0 mashape/methods/call/helpers/callHelper.php
  24. +83 −0 mashape/methods/call/helpers/serializeMethodResult.php
  25. +113 −0 mashape/methods/call/helpers/serializeObject.php
  26. +76 −0 mashape/methods/call/helpers/validateParameters.php
  27. +83 −0 mashape/methods/discover/discover.php
  28. +80 −0 mashape/methods/discover/helpers/discoverMethods.php
  29. +78 −0 mashape/methods/discover/helpers/discoverObjects.php
  30. +144 −0 mashape/methods/handler.php
  31. +31 −0 mashape/methods/iMethodHandler.php
  32. +82 −0 mashape/net/httpUtils.php
  33. +35 −0 mashape/settings.php
  34. +42 −0 mashape/utils/arrayUtils.php
  35. +40 −0 tests/configuration/restConfigurationLoaderTest.php
  36. +44 −0 tests/json/jsonImplTest.php
  37. +39 −0 tests/json/jsonUtilsTest.php
  38. +272 −0 tests/methods/call/helpers/callHelperTest.php
  39. +10 −0 tests/methods/call/helpers/test.xml
  40. +10 −0 tests/methods/call/helpers/test2.xml
  41. +15 −0 tests/methods/call/helpers/test3.xml
  42. +10 −0 tests/methods/call/helpers/test4.xml
  43. +15 −0 tests/methods/call/helpers/test5.xml
  44. +18 −0 tests/methods/call/helpers/test6.xml
  45. +11 −0 tests/methods/call/helpers/test7.xml
  46. +53 −0 tests/methods/discover/discoverTest.php
Oops, something went wrong.
@@ -0,0 +1,4 @@
+Mashape PHP library v0.2
+Copyright (C) 2010 Mashape, Inc.
+
+For the documentation, please visit http://www.mashape.com/guide/publish/php
@@ -0,0 +1,55 @@
+<?php
+
+require_once("mashape/mashape.php");
+
+class Ret {
+ public $name;
+ public $age;
+ public $nick;
+}
+
+class User {
+ public $name;
+ public $email;
+ public $ret;
+}
+
+class Tag {
+ public $text;
+ public $author;
+}
+
+class Second {
+ public $tag;
+ public $rating;
+ public $user;
+}
+
+class HelloWorldAPI extends MashapeRestAPI
+{
+ public function sayHello($name, $nick = "default nickname") {
+ $ret = new Ret();
+ $ret->name = $name;
+ $ret->age = array(3, 4, 10);
+ $ret->nick = $nick;
+ return $ret;
+ }
+
+ public function complex1($param1, $param2, $param3) {
+ return new User();
+ }
+
+ public function complex2($param1) {
+ return new Second();
+ }
+
+ public function touch() {
+ return "Ouch!";
+ }
+}
+
+// Handle the requests
+MashapeHandler::handleAPI(new HelloWorldAPI());
+
+?>
+
@@ -0,0 +1,43 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<api serverkey="my-server-key">
+
+ <method http="GET" name="sayHello">
+ <result type="object" name="Ret" />
+ </method>
+
+ <method http="GET" name="complex1">
+ <result type="object" name="User" />
+ </method>
+
+ <method http="GET" name="complex2">
+ <result type="object" name="Second" />
+ </method>
+
+ <method http="GET" name="touch">
+ <result type="simple" name="message"/>
+ </method>
+
+ <object class="Ret">
+ <field>name</field>
+ <field optional="true" array="true">age</field>
+ <field>nick</field>
+ </object>
+
+ <object class="User">
+ <field>name</field>
+ <field>email</field>
+ <field object="Ret">ret</field>
+ </object>
+
+ <object class="Second">
+ <field object="Tag">tag</field>
+ <field>rating</field>
+ <field object="User">user</field>
+ </object>
+
+ <object class="Tag">
+ <field>text</field>
+ <field>author</field>
+ </object>
+</api>
+
@@ -0,0 +1,125 @@
+<?php
+
+/*
+ * Mashape PHP library.
+ *
+ * Copyright (C) 2010 Mashape, Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ *
+ * The author of this software is Mashape, Inc.
+ * For any question or feedback please contact us at: support@mashape.com
+ *
+ */
+
+require_once(dirname(__FILE__) . "/../../exceptions/mashapeException.php");
+require_once(dirname(__FILE__) . "/../restMethod.php");
+
+define("XML_METHOD", "method");
+define("XML_METHOD_NAME", "name");
+define("XML_METHOD_HTTP", "http");
+
+define("XML_RESULT", "result");
+define("XML_RESULT_ARRAY", "array");
+define("XML_RESULT_TYPE", "type");
+define("XML_RESULT_NAME", "name");
+
+function loadMethodsFromXML($xmlDoc) {
+ $methods = array();
+
+ $xmlMethods= $xmlDoc->getElementsByTagName(XML_METHOD);
+ foreach ($xmlMethods as $xmlMethod) {
+ $name = $xmlMethod->getAttribute(XML_METHOD_NAME);
+ if (empty($name)) {
+ throw new MashapeException(EXCEPTION_METHOD_EMPTY_NAME, EXCEPTION_XML_CODE);
+ } else if (existMethod($methods, $name)) {
+ throw new MashapeException(sprintf(EXCEPTION_METHOD_DUPLICATE_NAME, $name), EXCEPTION_XML_CODE);
+ }
+
+ $http = $xmlMethod->getAttribute(XML_METHOD_HTTP);
+ if (empty($http)) {
+ throw new MashapeException(EXCEPTION_METHOD_EMPTY_HTTP, EXCEPTION_XML_CODE);
+ } else {
+ $http = strtolower($http);
+ if ($http != "get" && $http != "post" && $http != "put" && $http != "delete") {
+ throw new MashapeException(sprintf(EXCEPTION_METHOD_INVALID_HTTP, $http),EXCEPTION_XML_CODE);
+ }
+ }
+
+ // Get the result
+ $resultsNode = $xmlMethod->getElementsByTagName(XML_RESULT);
+ $resultNode = null;
+ if ($resultsNode->length > 1) {
+ throw new MashapeException(sprintf(EXCEPTION_RESULT_MULTIPLE, $name), EXCEPTION_XML_CODE);
+ } elseif ($resultsNode->length==1) {
+ $resultNode = $resultsNode->item(0);
+ } else {
+ throw new MashapeException(sprintf(EXCEPTION_RESULT_MISSING, $name), EXCEPTION_XML_CODE);
+ }
+
+ $object = null;
+ $array = null;
+ $resultName = null;
+
+ if ($resultNode != null) {
+ $array = $resultNode->getAttribute(XML_RESULT_ARRAY);
+ if ($array != null && strtolower($array) == "true") {
+ $array = true;
+ } else {
+ $array = false;
+ }
+
+ $type = $resultNode->getAttribute(XML_RESULT_TYPE);
+ if (strtolower($type=="simple")) {
+ $resultName = $resultNode->getAttribute(XML_RESULT_NAME);
+ if (empty($resultName)) {
+ throw new MashapeException(sprintf(EXCEPTION_RESULT_EMPTY_NAME_SIMPLE, $name), EXCEPTION_XML_CODE);
+ }
+ } else if (strtolower($type=="object")) {
+ $object = $resultNode->getAttribute(XML_RESULT_NAME);
+ if (empty($object)) {
+ throw new MashapeException(sprintf(EXCEPTION_RESULT_EMPTY_NAME_OBJECT, $name), EXCEPTION_XML_CODE);
+ }
+ } else if (empty($type)) {
+ throw new MashapeException(sprintf(EXCEPTION_RESULT_EMPTY_TYPE, $name), EXCEPTION_XML_CODE);
+ } else {
+ throw new MashapeException(sprintf(EXCEPTION_RESULT_INVALID_TYPE, $type, $name), EXCEPTION_XML_CODE);
+ }
+ }
+
+ $method = new RESTMethod();
+ $method->setName($name);
+ $method->setObject($object);
+ $method->setResult($resultName);
+ $method->setArray($array);
+ $method->setHttp($http);
+
+ //Save method
+ array_push($methods, $method);
+
+ }
+ return $methods;
+}
+
+function existMethod($methods, $name) {
+ foreach ($methods as $method) {
+ if ($method->getName() == $name) {
+ return true;
+ }
+ }
+ return false;
+}
+
+?>
@@ -0,0 +1,117 @@
+<?php
+
+/*
+ * Mashape PHP library.
+ *
+ * Copyright (C) 2010 Mashape, Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ *
+ * The author of this software is Mashape, Inc.
+ * For any question or feedback please contact us at: support@mashape.com
+ *
+ */
+
+require_once(dirname(__FILE__) . "/../../exceptions/mashapeException.php");
+require_once(dirname(__FILE__) . "/../restField.php");
+require_once(dirname(__FILE__) . "/../restObject.php");
+
+define("XML_OBJECT", "object");
+define("XML_OBJECT_CLASS", "class");
+
+define("XML_FIELD", "field");
+define("XML_FIELD_OBJECT", "object");
+define("XML_FIELD_METHOD", "method");
+define("XML_FIELD_ARRAY", "array");
+define("XML_FIELD_OPTIONAL", "optional");
+
+function loadObjectsFromXML($xmlDocument) {
+ $objects = array();
+
+ $xmlObjects = $xmlDocument->getElementsByTagName(XML_OBJECT);
+ foreach($xmlObjects as $xmlObject)
+ {
+ $className = $xmlObject->getAttribute(XML_OBJECT_CLASS);
+ if (empty($className)) {
+ throw new MashapeException(EXCEPTION_OBJECT_EMPTY_CLASS, EXCEPTION_XML_CODE);
+ } else if (existClassName($objects, $className)) {
+ throw new MashapeException(sprintf(EXCEPTION_OBJECT_DUPLICATE_CLASS, $className), EXCEPTION_XML_CODE);
+ }
+
+ //Get fields
+ $fields = array();
+ $xmlFields = $xmlObject->getElementsByTagName(XML_FIELD);
+ foreach($xmlFields as $xmlField)
+ {
+ $field_name = $xmlField->textContent;
+ if (empty($field_name)) {
+ throw new MashapeException(EXCEPTION_FIELD_EMPTY_NAME, EXCEPTION_XML_CODE);
+ } else if (existFieldName($fields, $field_name)) {
+ throw new MashapeException(sprintf(EXCEPTION_FIELD_NAME_DUPLICATE, $field_name, $className), EXCEPTION_XML_CODE);
+ }
+
+ $field_object = $xmlField->getAttribute(XML_FIELD_OBJECT);
+ $field_method = $xmlField->getAttribute(XML_FIELD_METHOD);
+
+ $field_array = $xmlField->getAttribute(XML_FIELD_ARRAY);
+ if ($field_array != null && strtolower($field_array) == "true") {
+ $field_array = true;
+ } else {
+ $field_array = false;
+ }
+
+ $field_optional = $xmlField->getAttribute(XML_FIELD_OPTIONAL);
+ if ($field_optional != null && strtolower($field_optional) == "true") {
+ $field_optional = true;
+ } else {
+ $field_optional = false;
+ }
+
+ $field = new RESTField();
+ $field->setName($field_name);
+ $field->setObject($field_object);
+ $field->setMethod($field_method);
+ $field->setArray($field_array);
+ $field->setOptional($field_optional);
+ array_push($fields, $field);
+ }
+
+ $object = new RESTObject();
+ $object->setClassName($className);
+ $object->setFields($fields);
+ array_push($objects, $object);
+ }
+ return $objects;
+}
+
+function existClassName($objects, $className) {
+ foreach ($objects as $object) {
+ if ($object->getClassName() == $className) {
+ return true;
+ }
+ }
+ return false;
+}
+
+function existFieldName($fields, $fieldName) {
+ foreach ($fields as $field) {
+ if ($field->getName() == $fieldName) {
+ return true;
+ }
+ }
+ return false;
+}
+
+?>
Oops, something went wrong.

0 comments on commit 48fa5fc

Please sign in to comment.