Commit 3df56e6c authored by Mickael Desfrenes's avatar Mickael Desfrenes
Browse files

add phpdoc

parent 6de7fc74
<?php
include __DIR__ . '/src/Certic/Circe/Exception.php';
include __DIR__ . '/src/Certic/Circe/LogEntry.php';
include __DIR__ . '/src/Certic/Circe/JobResult.php';
include __DIR__ . '/src/Certic/Circe/Job.php';
include __DIR__ . '/src/Certic/Circe/Client.php';
use \Certic\Circe\Client as Client;
$client = new Client("http://localhost:8000/", "not a key", "not a uuid");
$job = $client->newJob();
$job->addFile(__FILE__);
$job->addTransformation('does_not_exist');
$client->send($job, true); // synchronous call
$result = $job->getResult();
// should only show a 'No such transformation' warning
foreach ($result->getLog() as $entry) {
echo $entry->getTimeStamp()->format('Y-m-d H:i:s')
. ' [' . $entry->getLevelLabel() . ']: '
. $entry->getMessage() . PHP_EOL;
}
// should print the source of this file
foreach ($result->getDocumentsPaths() as $file_path) {
echo file_get_contents($file_path) . PHP_EOL;
}
\ No newline at end of file
......@@ -11,6 +11,12 @@ class JobResult
$this->result_file_path = $result_file_path;
}
/**
* Returns an Iterator over files contained
* in the returned archive.
*
* @return \Iterator
*/
public function getFiles(): \Iterator
{
$tar = new \PharData($this->result_file_path);
......@@ -19,11 +25,22 @@ class JobResult
}
}
public function getDocuments(): \Iterator
/**
* Return an Iterator over the documents paths,
* excluding the jobs definition file (job.json)
* and the log file (out.log).
*
* Paths are of type 'phar:///path/to/file.ext' and
* can be opened with any PHP builtin function
* compatible with stream wrappers.
*
* @return string[]
*/
public function getDocumentsPaths(): \Iterator
{
foreach($this->getFiles() as $file) {
if (!in_array($file->getFileName(), ['out.log', 'job.json'])) {
yield $file;
yield (string)$file;
}
}
}
......
......@@ -11,26 +11,39 @@ class LogEntry
const LEVEL_ERROR = 3;
const LEVEL_FATAL = 4;
private array $log_dict;
public function __construct(array $log_dict){
public function __construct(array $log_dict)
{
$this->log_dict = $log_dict;
}
public function getTimestamp(): \DateTime{
// "%Y-%m-%dT%H:%M:%S.%fZ"
if(array_key_exists('timestamp', $this->log_dict)){
return new \DateTime($this->log_dict['timestamp']);
public function getTimestamp(): ?\DateTime
{
if (array_key_exists('timestamp', $this->log_dict)) {
try {
// stamp should be in "%Y-%m-%dT%H:%M:%S.%fZ" form.
$stamp = new \DateTime($this->log_dict['timestamp']);
return $stamp;
} catch (\Exception $e) {
// parse date may fail
error_log($e->getMessage());
}
}
return null;
}
public function getMessage(): string{
if(array_key_exists('message', $this->log_dict)){
return $this->log_dict['message'];
public function getMessage(): ?string
{
if (array_key_exists('message', $this->log_dict)) {
return (string)$this->log_dict['message'];
}
return null;
}
public function getLevel(): int{
if(array_key_exists('level', $this->log_dict)){
switch ($this->log_dict['level']){
public function getLevel(): ?int
{
if (array_key_exists('level', $this->log_dict)) {
switch ($this->log_dict['level']) {
case 'DEBUG':
return self::LEVEL_DEBUG;
case 'WARNING':
......@@ -44,6 +57,26 @@ class LogEntry
return self::LEVEL_INFO;
}
}
return self::LEVEL_INFO;
return null;
}
public function getLevelLabel(): ?string
{
if (array_key_exists('level', $this->log_dict)) {
switch ($this->log_dict['level']) {
case 'DEBUG':
return 'DEBUG';
case 'WARNING':
return 'WARNING';
case 'ERROR':
return 'ERROR';
case 'FATAL':
case 'CRITICAL':
return 'FATAL';
default:
return 'INFO';
}
}
return null;
}
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment