Skip to content

PSR-2 #80

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 13 additions & 15 deletions autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,44 @@
/**
* You only need this file if you are not using composer.
*/

if (version_compare(PHP_VERSION, '5.4.0', '<')) {
throw new Exception('The Parse SDK requires PHP version 5.4 or higher.');
throw new Exception('The Parse SDK requires PHP version 5.4 or higher.');
}

/**
/*
* Register the autoloader for the Parse SDK
* Based off the official PSR-4 autoloader example found here:
* https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader-examples.md
*
* @param string $class The fully-qualified class name.
* @return void
*/
spl_autoload_register(function ($class)
{
spl_autoload_register(function ($class) {
// Parse class prefix
$prefix = 'Parse\\';

// base directory for the namespace prefix
$base_dir = defined('PARSE_SDK_DIR') ? PARSE_SDK_DIR : __DIR__ . '/src/Parse/';
$base_dir = defined('PARSE_SDK_DIR') ? PARSE_SDK_DIR : __DIR__.'/src/Parse/';

// does the class use the namespace prefix?
$len = strlen( $prefix );
if ( strncmp($prefix, $class, $len) !== 0 ) {
// no, move to the next registered autoloader
$len = strlen($prefix);
if (strncmp($prefix, $class, $len) !== 0) {
// no, move to the next registered autoloader
return;
}

// get the relative class name
$relative_class = substr( $class, $len );
$relative_class = substr($class, $len);

// replace the namespace prefix with the base directory, replace namespace
// separators with directory separators in the relative class name, append
// with .php
$file = $base_dir . str_replace( '\\', '/', $relative_class ) . '.php';
$file = $base_dir.str_replace('\\', '/', $relative_class).'.php';

// echo $relative_class . '<br/>';

// if the file exists, require it
if ( file_exists( $file ) ) {
require $file;
if (file_exists($file)) {
require $file;
}
});
});
69 changes: 35 additions & 34 deletions src/Parse/Internal/AddOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@
use Parse\ParseException;

/**
* Class AddOperation - FieldOperation for adding object(s) to array fields
* Class AddOperation - FieldOperation for adding object(s) to array fields.
*
* @package Parse
* @author Fosco Marotto <[email protected]>
*/
class AddOperation implements FieldOperation
{

/**
* @var - Array with objects to add.
*/
Expand All @@ -28,10 +26,10 @@ class AddOperation implements FieldOperation
*/
public function __construct($objects)
{
if (!is_array($objects)) {
throw new ParseException("AddOperation requires an array.");
}
$this->objects = $objects;
if (!is_array($objects)) {
throw new ParseException("AddOperation requires an array.");
}
$this->objects = $objects;
}

/**
Expand All @@ -41,7 +39,7 @@ public function __construct($objects)
*/
public function getValue()
{
return $this->objects;
return $this->objects;
}

/**
Expand All @@ -51,39 +49,42 @@ public function getValue()
*/
public function _encode()
{
return array('__op' => 'Add',
'objects' => ParseClient::_encode($this->objects, true));
return ['__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
*
* @return FieldOperation Merged operation.
*/
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) {
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)
}
if ($previous instanceof AddOperation) {
$oldList = $previous->getValue();

return new SetOperation(
array_merge((array) $oldList, (array) $this->objects)
);
}
throw new ParseException(
}
throw new ParseException(
'Operation is invalid after previous operation.'
);
}
Expand All @@ -99,10 +100,10 @@ public function _mergeWithPrevious($previous)
*/
public function _apply($oldValue, $obj, $key)
{
if (!$oldValue) {
return $this->objects;
}
return array_merge((array)$oldValue, (array)$this->objects);
}
if (!$oldValue) {
return $this->objects;
}

}
return array_merge((array) $oldValue, (array) $this->objects);
}
}
120 changes: 61 additions & 59 deletions src/Parse/Internal/AddUniqueOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@
/**
* Class AddUniqueOperation - Operation to add unique objects to an array key.
*
* @package Parse
* @author Fosco Marotto <[email protected]>
*/
class AddUniqueOperation implements FieldOperation
{

/**
* @var - Array containing objects to add.
*/
Expand All @@ -28,10 +26,10 @@ class AddUniqueOperation implements FieldOperation
*/
public function __construct($objects)
{
if (!is_array($objects)) {
throw new ParseException("AddUniqueOperation requires an array.");
}
$this->objects = $objects;
if (!is_array($objects)) {
throw new ParseException("AddUniqueOperation requires an array.");
}
$this->objects = $objects;
}

/**
Expand All @@ -41,7 +39,7 @@ public function __construct($objects)
*/
public function getValue()
{
return $this->objects;
return $this->objects;
}

/**
Expand All @@ -51,37 +49,40 @@ public function getValue()
*/
public function _encode()
{
return array('__op' => 'AddUnique',
'objects' => ParseClient::_encode($this->objects, true));
return ['__op' => 'AddUnique',
'objects' => ParseClient::_encode($this->objects, true), ];
}

/**
* Merge this operation with the previous operation and return the result.
*
* @param FieldOperation $previous Previous Operation.
*
* @return FieldOperation Merged Operation.
* @throws ParseException
*
* @return FieldOperation Merged Operation.
*/
public function _mergeWithPrevious($previous)
{
if (!$previous) {
return $this;
}
if ($previous instanceof DeleteOperation) {
return new SetOperation($this->objects);
}
if ($previous instanceof SetOperation) {
$oldValue = $previous->getValue();
$result = $this->_apply($oldValue, null, null);
return new SetOperation($result);
}
if ($previous instanceof AddUniqueOperation) {
$oldList = $previous->getValue();
$result = $this->_apply($oldList, null, null);
return new AddUniqueOperation($result);
}
throw new ParseException(
if (!$previous) {
return $this;
}
if ($previous instanceof DeleteOperation) {
return new SetOperation($this->objects);
}
if ($previous instanceof SetOperation) {
$oldValue = $previous->getValue();
$result = $this->_apply($oldValue, null, null);

return new SetOperation($result);
}
if ($previous instanceof AddUniqueOperation) {
$oldList = $previous->getValue();
$result = $this->_apply($oldList, null, null);

return new AddUniqueOperation($result);
}
throw new ParseException(
'Operation is invalid after previous operation.'
);
}
Expand All @@ -97,40 +98,41 @@ public function _mergeWithPrevious($previous)
*/
public function _apply($oldValue, $obj, $key)
{
if (!$oldValue) {
return $this->objects;
}
if (!is_array($oldValue)) {
$oldValue = (array)$oldValue;
}
foreach ($this->objects as $object) {
if ($object instanceof ParseObject && $object->getObjectId()) {
if (!$this->isParseObjectInArray($object, $oldValue)) {
$oldValue[] = $object;
}
} else if (is_object($object)) {
if (!in_array($object, $oldValue, true)) {
$oldValue[] = $object;
}
} else {
if (!in_array($object, $oldValue, true)) {
$oldValue[] = $object;
}
if (!$oldValue) {
return $this->objects;
}
}
return $oldValue;
if (!is_array($oldValue)) {
$oldValue = (array) $oldValue;
}
foreach ($this->objects as $object) {
if ($object instanceof ParseObject && $object->getObjectId()) {
if (!$this->isParseObjectInArray($object, $oldValue)) {
$oldValue[] = $object;
}
} elseif (is_object($object)) {
if (!in_array($object, $oldValue, true)) {
$oldValue[] = $object;
}
} else {
if (!in_array($object, $oldValue, true)) {
$oldValue[] = $object;
}
}
}

return $oldValue;
}

private function isParseObjectInArray($parseObject, $oldValue)
{
foreach ($oldValue as $object) {
if ($object instanceof ParseObject && $object->getObjectId() != null) {
if ($object->getObjectId() == $parseObject->getObjectId()) {
return true;
private function isParseObjectInArray($parseObject, $oldValue)
{
foreach ($oldValue as $object) {
if ($object instanceof ParseObject && $object->getObjectId() != null) {
if ($object->getObjectId() == $parseObject->getObjectId()) {
return true;
}
}
}
}
}
return false;
}

}
return false;
}
}
11 changes: 4 additions & 7 deletions src/Parse/Internal/DeleteOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,18 @@
/**
* Class DeleteOperation - FieldOperation to remove a key from an object.
*
* @package Parse
* @author Fosco Marotto <[email protected]>
*/
class DeleteOperation implements FieldOperation
{

/**
* Returns an associative array encoding of the current operation.
*
* @return array Associative array encoding the operation.
*/
public function _encode()
{
return array('__op' => 'Delete');
return ['__op' => 'Delete'];
}

/**
Expand All @@ -32,7 +30,7 @@ public function _encode()
*/
public function _apply($oldValue, $object, $key)
{
return null;
return;
}

/**
Expand All @@ -44,7 +42,6 @@ public function _apply($oldValue, $object, $key)
*/
public function _mergeWithPrevious($previous)
{
return $this;
return $this;
}

}
}
Loading