Validates a XAPIAtom.
static public validateAtom ( Locker\XApi\Atom $atom, String $trace = null ) | ||
$atom | Locker\XApi\Atom | Atom to be validated. |
$trace | String | Where the atom has came from (i.e. request parameter name). |
/** * Constructs valid statements. * @param [\stdClass] $statements * @param StoreOptions $opts * @return [String => \stdClass] Array of statements mapped to their UUIDs. */ private function constructValidStatements(array $statements, StoreOptions $opts) { $generated_ids = []; $constructed = []; $this->hashes = []; foreach ($statements as $statement) { $statement->authority = $opts->getOpt('authority'); $statement->stored = Helpers::getCurrentDate(); if (!isset($statement->timestamp)) { $statement->timestamp = $statement->stored; } if (!isset($statement->id)) { $statement->id = $this->getUUID($generated_ids); $generated_ids[] = $statement->id; } // Validates statement. $constructed_statement = new XAPIStatement($statement); Helpers::validateAtom($constructed_statement, 'statement'); $statement = $constructed_statement->getValue(); // Gets attachment hashes. $attachments = !isset($statement->attachments) ? [] : $statement->attachments; foreach ($attachments as $attachment) { $this->hashes[] = $attachment->sha2; } // Adds $statement to $constructed. if (isset($constructed[$statement->id])) { $this->inserter->compareForConflict($statement, $constructed[$statement->id]); } else { $constructed[$statement->id] = $statement; } } return $constructed; }
/** * Validates the given options as index options. * @param [String => Mixed] $opts * @return [String => Mixed] */ protected function validate($opts) { foreach ($opts as $key => $value) { if ($value !== null && isset($this->types[$key]) && $this->types[$key] !== null) { if (is_array($this->types[$key])) { $class = '\\Locker\\XApi\\' . $this->types[$key][0]; if (!is_array($value)) { throw new Exceptions\Exception("{$key} must be an array."); } foreach ($value as $item) { Helpers::validateAtom(new $class($item)); } } else { $class = '\\Locker\\XApi\\' . $this->types[$key]; Helpers::validateAtom(new $class($value)); } } } return $opts; }
/** * Validates data. * @param [String => Mixed] $data Properties to be changed on the model. * @throws \Exception */ protected function validateData(array $data) { if (isset($data['authority'])) { Helpers::validateAtom(XApiAuthority::createFromJson(json_encode($data['authority'])), 'client.authority'); } }
/** * Creates statements from the content of the request. * @param [String => mixed] $options * @param Callable|null $modifier A function that modifies the statements before storing them. * @return AssocArray Result of storing the statements. */ private function createStatements($options, callable $modifier = null) { Helpers::validateAtom(new XApiImt(explode(';', LockerRequest::header('Content-Type'))[0])); // Gets parts of the request. $parts = $this->getParts(); $content = $parts['content']; // Decodes $statements from $content. try { if (Config::get('xapi.disable_duplicate_key_checks') !== true) { // Check incoming statements for duplicate keys and throw an error if found $jsonParser = new JsonParser(); $jsonParser->parse($content, JsonParser::DETECT_KEY_CONFLICTS); // this will catch any parsing issues } $statements = json_decode($content); if ($statements === null && $content !== '') { throw new Exceptions\Exception('Invalid JSON'); } else { if ($statements === null) { $statements = []; } } } catch (\Seld\JsonLint\DuplicateKeyException $e) { $details = $e->getDetails(); throw new Exceptions\Exception(sprintf('Invalid JSON: `%s` is a duplicate key on line %s', $details['key'], $details['line'])); } catch (\Exception $e) { // some other parsing error occured throw new Exceptions\Exception('Invalid JSON: JSON could not be parsed'); } // Ensures that $statements is an array. if (!is_array($statements)) { $statements = [$statements]; } // Runs the modifier if there is one and there are statements. if (count($statements) > 0 && $modifier !== null) { $statements = $modifier($statements); } // Saves $statements with attachments. return $this->statements->store($statements, is_array($parts['attachments']) ? $parts['attachments'] : [], array_merge(['authority' => $this->getAuthority($options['client'])], $options)); }
/** * Creates statements from the content of the request. * @param [String => mixed] $options * @param Callable|null $modifier A function that modifies the statements before storing them. * @return AssocArray Result of storing the statements. */ private function createStatements($options, callable $modifier = null) { Helpers::validateAtom(new XApiImt(explode(';', LockerRequest::header('Content-Type'))[0])); // Gets parts of the request. $parts = $this->getParts(); $content = $parts['content']; // Decodes $statements from $content. $statements = json_decode($content); if ($statements === null && $content !== '') { throw new Exceptions\Exception('Invalid JSON'); } else { if ($statements === null) { $statements = []; } } // Ensures that $statements is an array. if (!is_array($statements)) { $statements = [$statements]; } // Runs the modifier if there is one and there are statements. if (count($statements) > 0 && $modifier !== null) { $statements = $modifier($statements); } // Saves $statements with attachments. return $this->statements->store($statements, is_array($parts['attachments']) ? $parts['attachments'] : [], array_merge(['authority' => $this->getAuthority($options['client'])], $options)); }