Commit 3df56e6c authored by Mickaël Desfrênes's avatar Mickaël Desfrênes
Browse files

add phpdoc

parent 6de7fc74
Loading
Loading
Loading
Loading

example.php

0 → 100644
+29 −0
Original line number Diff line number Diff line
<?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
+19 −2
Original line number Diff line number Diff line
@@ -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;
            }
        }
    }
+45 −12
Original line number Diff line number Diff line
@@ -11,24 +11,37 @@ 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"
    public function getTimestamp(): ?\DateTime
    {
        if (array_key_exists('timestamp', $this->log_dict)) {
            return new \DateTime($this->log_dict['timestamp']);
            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{
    public function getMessage(): ?string
    {
        if (array_key_exists('message', $this->log_dict)) {
            return $this->log_dict['message'];
            return (string)$this->log_dict['message'];
        }
        return null;
    }

    public function getLevel(): int{
    public function getLevel(): ?int
    {
        if (array_key_exists('level', $this->log_dict)) {
            switch ($this->log_dict['level']) {
                case 'DEBUG':
@@ -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