/** * Sends a push notification. * * @param array $data The data of the push notification. Valid fields * are: * channels - An Array of channels to push to. * push_time - A Date object for when to send the push. * expiration_time - A Date object for when to expire * the push. * expiration_interval - The seconds from now to expire the push. * where - A ParseQuery over ParseInstallation that is used to match * a set of installations to push to. * data - The data to send as part of the push * @param bool $useMasterKey Whether to use the Master Key for the request * * @throws \Exception, ParseException * * @return mixed */ public static function send($data, $useMasterKey = false) { if (isset($data['expiration_time']) && isset($data['expiration_interval'])) { throw new Exception('Both expiration_time and expiration_interval can\'t be set.'); } if (isset($data['where'])) { if ($data['where'] instanceof ParseQuery) { $where_options = $data['where']->_getOptions(); if (!isset($where_options['where'])) { $data['where'] = '{}'; } else { $dd = $data['where']->_getOptions(); $data['where'] = $dd['where']; } } else { throw new Exception('Where parameter for Parse Push must be of type ParseQuery'); } } if (isset($data['push_time'])) { //Local push date format is different from iso format generally used in Parse //Schedule does not work if date format not correct $data['push_time'] = ParseClient::getPushDateFormat($data['push_time'], isset($data['local_time'])); } if (isset($data['expiration_time'])) { $cc = ParseClient::_encode($data['expiration_time'], false); $data['expiration_time'] = $cc['iso']; } return ParseClient::_request('POST', 'push', null, json_encode(ParseClient::_encode($data, true)), $useMasterKey); }
/** * Retrieves the Session object for the currently logged in user. * * @param boolean $useMasterKey If the Master Key should be used to override security. * * @return ParseSession */ public static function getCurrentSession($useMasterKey = false) { $token = ParseUser::getCurrentUser()->getSessionToken(); $response = ParseClient::_request('GET', '/1/sessions/me', $token, null, $useMasterKey); $session = new ParseSession(); $session->_mergeAfterFetch($response); $session->handleSaveResult(); return $session; }
/** * Tracks the occurrence of a custom event with additional dimensions. * Parse 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' * ); * ParseAnalytics::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 = []) { $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 ParseClient::_request('POST', 'events/' . $name, null, static::_toSaveJSON($dimensions)); }
/** * Use schema.json to create new Parse 'classes'. Pulls out fields in the schema that Parse creates by default. * @throws \Parse\ParseException */ public function parseAction() { $schema = file_get_contents('schema.json'); $schema = json_decode($schema, true); foreach ($schema as $table) { foreach ($this->parseFields as $field) { unset($table['fields'][$field]); } if (empty($table['fields'])) { unset($table['fields']); } //will throw exception on error ParseClient::_request('POST', 'schemas/' . $table['className'], null, json_encode($table), true); } echo 'Schema Created' . PHP_EOL; }
/** * Save object and unsaved children within. * * @param ParseObject|array $target * @param bool $useMasterKey Whether to use the Master Key. * * @throws Exception * @throws ParseAggregateException * @throws ParseException */ private static function deepSave($target, $useMasterKey = false) { $unsavedChildren = []; $unsavedFiles = []; static::findUnsavedChildren($target, $unsavedChildren, $unsavedFiles); $sessionToken = null; if (ParseUser::getCurrentUser()) { $sessionToken = ParseUser::getCurrentUser()->getSessionToken(); } foreach ($unsavedFiles as &$file) { $file->save(); } $objects = []; // 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 = []; $newRemaining = []; 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 = []; foreach ($batch as $obj) { $json = $obj->getSaveJSON(); $method = 'POST'; $path = 'classes/' . $obj->getClassName(); if ($obj->getObjectId()) { $path .= '/' . $obj->getObjectId(); $method = 'PUT'; } $requests[] = ['method' => $method, 'path' => $path, 'body' => $json]; } if (count($requests) === 1) { $req = $requests[0]; $result = ParseClient::_request($req['method'], $req['path'], $sessionToken, json_encode($req['body']), $useMasterKey); $batch[0]->mergeAfterSave($result); } else { foreach ($requests as &$r) { $r['path'] = '/' . ParseClient::getMountPath() . $r['path']; } $result = ParseClient::_request('POST', 'batch', $sessionToken, json_encode(['requests' => $requests]), $useMasterKey); $errorCollection = []; foreach ($batch as $key => &$obj) { if (isset($result[$key]['success'])) { $obj->mergeAfterSave($result[$key]['success']); } elseif (isset($result[$key]['error'])) { $response = $result[$key]; $error = $response['error']['error']; $code = isset($response['error']['code']) ? $response['error']['code'] : -1; $errorCollection[] = ['error' => $error, 'code' => $code, 'object' => $obj]; } else { $errorCollection[] = ['error' => 'Unknown error in batch save.', 'code' => -1, 'object' => $obj]; } } if (count($errorCollection)) { throw new ParseAggregateException('Errors during batch save.', $errorCollection); } } } }
/** * Execute a find query and return the results. * * @param bool $useMasterKey * * @return ParseObject[] */ public function find($useMasterKey = false) { $sessionToken = null; if (ParseUser::getCurrentUser()) { $sessionToken = ParseUser::getCurrentUser()->getSessionToken(); } $queryString = $this->buildQueryString($this->_getOptions()); $result = ParseClient::_request('GET', 'classes/' . $this->className . '?' . $queryString, $sessionToken, null, $useMasterKey); $output = []; foreach ($result['results'] as $row) { $obj = ParseObject::create($this->className, $row['objectId']); $obj->_mergeAfterFetchWithSelectedKeys($row, $this->selectedKeys); $output[] = $obj; } return $output; }
/** * Removing a Schema from Parse. * You can only remove a schema from your app if it is empty (has 0 objects). * * @throws \Exception * * @return array */ public function delete() { self::assertClassName(); $sessionToken = null; if (ParseUser::getCurrentUser()) { $sessionToken = ParseUser::getCurrentUser()->getSessionToken(); } $result = ParseClient::_request('DELETE', 'schemas/' . $this->className, $sessionToken, null, self::$useMasterKey); if (!empty($result)) { throw new Exception('Error on delete Schema "' . $this->className . '"', 101); } return true; }