Skip to content

Commit

Permalink
Initial release.
Browse files Browse the repository at this point in the history
  • Loading branch information
Fosco Marotto committed Aug 4, 2014
1 parent 549291a commit c237d06
Show file tree
Hide file tree
Showing 53 changed files with 10,592 additions and 0 deletions.
34 changes: 34 additions & 0 deletions CONTRIBUTING.md
@@ -0,0 +1,34 @@
Contributing
------------

For us to accept contributions you will have to first have signed the
[Contributor License Agreement].

When committing, keep all lines to less than 80 characters, and try to
follow the existing style. Before creating a pull request, squash your commits
into a single commit. Please provide ample explanation in the commit message.

Installation
------------

Testing the Parse PHP SDK involves some set-up. You'll need to create a Parse
App just for testing, and deploy some cloud code to it.

* [Get Composer], the PHP package manager.
* Run "composer install" to download dependencies.
* Create a new app here: [Create Parse App]
* Use the Parse CLI to create a Cloud Code folder for the new app.
* Copy tests/cloudcode/cloud/main.js into the newly created cloud/ folder.
* Run "parse deploy" in your cloud folder.
* Paste your App ID, REST API Key, and Master Key in tests/ParseTestHelper.php

You should now be able to execute, from the tests/ folder:

../vendor/bin/phpunit --stderr .

At present the full suite of tests takes around 20 minutes.


[Get Composer]: https://getcomposer.org/download/
[Contributor License Agreement]: https://developers.facebook.com/opensource/cla
[Create Parse App]: https://parse.com/apps/new
17 changes: 17 additions & 0 deletions LICENSE
@@ -0,0 +1,17 @@
Copyright (c) 2014, Parse, LLC. All rights reserved.

You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
copy, modify, and distribute this software in source code or binary form for use
in connection with the web services and APIs provided by Parse.

As with any software that integrates with the Parse platform, your use of
this software is subject to the Parse Terms of Service at: https://www.parse.com/about/terms
This copyright notice shall be included in all copies or substantial portions of the
software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
172 changes: 172 additions & 0 deletions README.md
@@ -0,0 +1,172 @@
Parse PHP SDK
-------------

The Parse PHP SDK gives you access to the powerful Parse cloud platform
from your PHP app or script.

Installation
------------

[Get Composer], the PHP package manager. Then create a composer.json file in
your projects root folder, containing:

{
"require": {
"parse/php-sdk" : "1.0.*"
}
}

Run "composer install" to download the SDK and set up the autoloader,
and then require it from your PHP script:

require 'vendor/autoload.php';

Usage
-----

Check out the [Parse PHP Guide] for the full documentation.

Add the "use" declarations where you'll be using the classes. For all of the
sample code in this file:

use Parse\ParseObject;
use Parse\ParseQuery;
use Parse\ParseACL;
use Parse\ParsePush;
use Parse\ParseUser;
use Parse\ParseInstallation;
use Parse\ParseException;
use Parse\ParseAnalytics;
use Parse\ParseFile;
use Parse\ParseCloud;


Objects:

$object = ParseObject::create("TestObject");
$objectId = $object->getObjectId();
$php = $object->get("elephant");

// Set values:
$object->set("elephant", "php");
$object->set("today", new DateTime());
$object->setArray("mylist", [1, 2, 3]);
$object->setAssociativeArray(
"languageTypes", array("php" => "awesome", "ruby" => "wtf")
);

// Save:
$object->save();

Users:

// Signup
$user = new ParseUser();
$user->setUsername("foo");
$user->setPassword("Q2w#4!o)df");
try {
$user->signUp();
} catch (ParseException $ex) {
// error in $ex->getMessage();
}

// Login
try {
$user = ParseUser::logIn("foo", "Q2w#4!o)df");
} catch(ParseException $ex) {
// error in $ex->getMessage();
}

// Current user
$user = ParseUser::getCurrentUser();

Security:

// Access only by the ParseUser in $user
$userACL = ParseACL::createACLWithUser($user);

// Access only by master key
$restrictedACL = new ParseACL();

// Set individual access rights
$acl = new ParseACL();
$acl->setPublicReadAccess(true);
$acl->setPublicWriteAccess(false);
$acl->setUserWriteAccess($user, true);
$acl->setRoleWriteAccessWithName("PHPFans", true);

Queries:

$query = new ParseQuery("TestObject");

// Get a specific object:
$object = $query->get("anObjectId");

$query->limit(10); // default 100, max 1000

// All results:
$results = $query->find();

// Just the first result:
$first = $query->first();

// Process ALL (without limit) results with "each".
// Will throw if sort, skip, or limit is used.
$query->each(function($obj) {
echo $obj->getObjectId();
});

Cloud Functions:

$results = ParseCloud::run("aCloudFunction", array("from" => "php"));

Analytics:

PFAnalytics::trackEvent("logoReaction", array(
"saw" => "elephant",
"said" => "cute"
));

Files:

// Get from a Parse Object:
$file = $aParseObject->get("aFileColumn");
$name = $file->getName();
$url = $file->getURL();
// Download the contents:
$contents = $file->getData();

// Upload from a local file:
$file = ParseFile::createFromFile(
"/tmp/foo.bar", "Parse.txt", "text/plain"
);

// Upload from variable contents (string, binary)
$file = ParseFile::createFromData($contents, "Parse.txt", "text/plain");

Push:

$data = array("alert" => "Hi!");

// Push to Channels
ParsePush::send(array(
"channels" => ["PHPFans"],
"data" => $data
));

// Push to Query
$query = ParseInstallation::query();
$query->equalTo("design", "rad");
ParsePush::send(array(
"where" => $query,
"data" => $data
));

Contributing / Testing
----------------------

See the CONTRIBUTORS.md file for information on testing and contributing to
the Parse PHP SDK. We welcome fixes and enhancements.

[Get Composer]: https://getcomposer.org/download/
[Parse PHP Guide]: https://www.parse.com/docs/php_guide
29 changes: 29 additions & 0 deletions composer.json
@@ -0,0 +1,29 @@
{
"name": "parse/php-sdk",
"description": "Parse PHP SDK",
"keywords": ["parse", "sdk"],
"type": "library",
"homepage": "https://github.com/parseplatform/parse-php-sdk",
"license": "Parse Platform License",
"authors": [
{
"name": "Parse",
"homepage": "https://github.com/parseplatform/parse-php-sdk/contributors"
}
],
"require": {
"php": ">=5.4.0",
"ext-curl": "*",
"ext-json": "*"
},
"require-dev": {
"phpunit/phpunit": "3.7.*",
"squizlabs/php_codesniffer": "1.*",
"phpdocumentor/phpdocumentor": "*"
},
"autoload": {
"psr-4": {
"Parse\\": "src/Parse/"
}
}
}
108 changes: 108 additions & 0 deletions src/Parse/Internal/AddOperation.php
@@ -0,0 +1,108 @@
<?php

namespace Parse\Internal;

use Parse\ParseClient;
use Parse\ParseException;

/**
* Class AddOperation - FieldOperation for adding object(s) to array fields
*
* @package Parse
* @author Fosco Marotto <fjm@fb.com>
*/
class AddOperation implements FieldOperation
{

/**
* @var - Array with objects to add.
*/
private $objects;

/**
* Creates an AddOperation with the provided objects.
*
* @param array $objects Objects to add.
*
* @throws ParseException
*/
public function __construct($objects)
{
if (!is_array($objects)) {
throw new ParseException("AddOperation requires an array.");
}
$this->objects = $objects;
}

/**
* Gets the objects for this operation.
*
* @return mixed
*/
public function getValue()
{
return $this->objects;
}

/**
* Returns associative array representing encoded operation.
*
* @return array
*/
public function _encode()
{
return array('__op' => 'Add',
'objects' => ParseClient::_encode($this->objects, true));
}

/**
* Takes a previous operation and returns a merged operation to replace it.
*
* @param FieldOperation $previous Previous operation.
*
* @return FieldOperation Merged operation.
* @throws ParseException
*/
public function _mergeWithPrevious($previous)
{
if (!$previous) {
return $this;
}
if ($previous instanceof DeleteOperation) {
return new SetOperation($this->objects);
}
if ($previous instanceof SetOperation) {
$oldList = $previous->getValue();
return new SetOperation(
array_merge((array)$oldList, (array)$this->objects)
);
}
if ($previous instanceof AddOperation) {
$oldList = $previous->getValue();
return new SetOperation(
array_merge((array)$oldList, (array)$this->objects)
);
}
throw new ParseException(
'Operation is invalid after previous operation.'
);
}

/**
* Applies current operation, returns resulting value.
*
* @param mixed $oldValue Value prior to this operation.
* @param mixed $obj Value being applied.
* @param string $key Key this operation affects.
*
* @return array
*/
public function _apply($oldValue, $obj, $key)
{
if (!$oldValue) {
return $this->objects;
}
return array_merge((array)$oldValue, (array)$this->objects);
}

}

0 comments on commit c237d06

Please sign in to comment.