Locker\Helpers\Helpers::validateAtom PHP Method

validateAtom() static public method

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).
    static function validateAtom(XAPIAtom $atom, $trace = null)
    {
        $errors = $atom->validate();
        if (count($errors) > 0) {
            throw new Exceptions\Validation(array_map(function (XAPIError $error) use($trace) {
                return (string) ($trace === null ? $error : $error->addTrace($trace));
            }, $errors));
        }
    }

Usage Example

 /**
  * 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;
 }
All Usage Examples Of Locker\Helpers\Helpers::validateAtom