/** * Returns an associative array encoding of the current operation. * * @return mixed */ public function _encode() { if ($this->isAssociativeArray) { $object = new \stdClass(); foreach ($this->value as $key => $value) { $object->{$key} = AVClient::_encode($value, true); } return AVClient::_encode($object, true); } return AVClient::_encode($this->value, true); }
/** * Tracks the occurrence of a custom event with additional dimensions. * Avos will store a data point at the time of invocation with the given * event name. * * Dimensions will allow segmentation of the occurrences of this custom * event. Keys and values should be strings, and will throw * otherwise. * * To track a user signup along with additional metadata, consider the * following: * <pre> * $dimensions = array( * 'gender' => 'm', * 'source' => 'web', * 'dayType' => 'weekend' * ); * AVAnalytics::track('signup', $dimensions); * </pre> * * There is a default limit of 4 dimensions per event tracked. * * @param string $name The name of the custom event * @param array $dimensions The dictionary of segment information * * @throws \Exception * @return mixed */ public static function track($name, $dimensions = array()) { $name = trim($name); if (strlen($name) === 0) { throw new Exception('A name for the custom event must be provided.'); } foreach ($dimensions as $key => $value) { if (!is_string($key) || !is_string($value)) { throw new Exception('Dimensions expected string keys and values.'); } } return AVClient::_request('POST', '/events/' . $name, null, static::_toSaveJSON($dimensions)); }
<?php ini_set("display_errors", "On"); error_reporting(E_ALL | E_STRICT); require 'vendor/autoload.php'; use Avos\AVClient; use Avos\AVSessionStorage; use Avos\AVUser; use Avos\AVException; use Avos\AVObject; session_start(); AVClient::initialize("app_id", "rest_key", "master_key"); AVClient::setStorage(new AVSessionStorage()); $object = AVObject::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(); echo "saved object id:" . $object->getObjectId();
/** * Returns associative array representing encoded operation. * * @return array */ public function _encode() { return array('__op' => 'Add', 'objects' => AVClient::_encode($this->objects, true)); }
/** * Save Object and unsaved children within. * * @param $target * @param bool $useMasterKey Whether to use the Master Key. * * @return null * * @throws AVException */ private static function deepSave($target, $useMasterKey = false) { $unsavedChildren = array(); $unsavedFiles = array(); static::findUnsavedChildren($target, $unsavedChildren, $unsavedFiles); $sessionToken = null; if (AVUser::getCurrentUser()) { $sessionToken = AVUser::getCurrentUser()->getSessionToken(); } foreach ($unsavedFiles as &$file) { $file->save(); } $objects = array(); // Get the set of unique objects among the children. foreach ($unsavedChildren as &$obj) { if (!in_array($obj, $objects, true)) { $objects[] = $obj; } } $remaining = $objects; while (count($remaining) > 0) { $batch = array(); $newRemaining = array(); foreach ($remaining as $key => &$object) { if (count($batch) > 40) { $newRemaining[] = $object; continue; } if ($object->canBeSerialized()) { $batch[] = $object; } else { $newRemaining[] = $object; } } $remaining = $newRemaining; if (count($batch) === 0) { throw new Exception("Tried to save a batch with a cycle."); } $requests = array(); foreach ($batch as $obj) { $json = $obj->getSaveJSON(); $method = 'POST'; $path = '/classes/' . $obj->getClassName(); if ($obj->getObjectId()) { $path .= '/' . $obj->getObjectId(); $method = 'PUT'; } $requests[] = array('method' => $method, 'path' => $path, 'body' => $json); } if (count($requests) === 1) { $req = $requests[0]; $result = AVClient::_request($req['method'], $req['path'], $sessionToken, json_encode($req['body']), $useMasterKey); $batch[0]->mergeAfterSave($result); } else { $result = AVClient::_request('POST', '/batch', $sessionToken, json_encode(array("requests" => $requests)), $useMasterKey); $errorCollection = array(); foreach ($batch as $key => &$obj) { if (isset($result[$key]['success'])) { $obj->mergeAfterSave($result[$key]['success']); } else { if (isset($result[$key]['error'])) { $response = $result[$key]; $error = $response['error']['error']; $code = isset($response['error']['code']) ? $response['error']['code'] : -1; $errorCollection[] = array('error' => $error, 'code' => $code, 'object' => $obj); } else { $errorCollection[] = array('error' => 'Unknown error in batch save.', 'code' => -1, 'object' => $obj); } } } if (count($errorCollection)) { throw new AVAggregateException("Errors during batch save.", $errorCollection); } } } }
/** * Returns an associative array encoding of the current operation. * * @return mixed * * @throws \Exception */ public function _encode() { $addRelation = array(); $removeRelation = array(); if (!empty($this->relationsToAdd)) { $addRelation = array('__op' => 'AddRelation', 'objects' => AVClient::_encode(self::convertToOneDimensionalArray($this->relationsToAdd), true)); } if (!empty($this->relationsToRemove)) { $removeRelation = array('__op' => 'RemoveRelation', 'objects' => AVClient::_encode(self::convertToOneDimensionalArray($this->relationsToRemove), true)); } if (!empty($addRelation) && !empty($removeRelation)) { return array('__op' => 'Batch', 'ops' => [$addRelation, $removeRelation]); } return empty($addRelation) ? $removeRelation : $addRelation; }