Skip to content

Commit aaf1d17

Browse files
committed
Documents basic use.
1 parent 4fc7d41 commit aaf1d17

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

docs/readme.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Getting Started
2+
3+
Basic use.
4+
```php
5+
use \Locker\XApi\Statement as Statement;
6+
7+
// Returns the given statement as a string of JSON.
8+
function getJsonStatement($statement) {
9+
return $statement->toJson();
10+
}
11+
12+
// Returns the given errors as a string of JSON.
13+
function getJsonValidationErrors($errors) {
14+
return json_encode(array_map(function ($error) {
15+
$error->addTrace('statement');
16+
return (string) $error;
17+
}, $errors));
18+
}
19+
20+
// Creates a statement and validates it.
21+
$statement = Statement::createFromJson($json_string);
22+
$errors = $statement->validate();
23+
24+
// Outputs validation errors or the statement as JSON.
25+
if (empty($errors)) {
26+
echo getJsonStatement($statement);
27+
} else {
28+
echo getJsonValidationErrors($errors);
29+
}
30+
```

0 commit comments

Comments
 (0)