File size: 2,435 Bytes
097eaf0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php

declare(strict_types=1);

namespace Firecrawl\Models;

final class CrawlJob
{
    /** @var list<Document> */
    private array $data;

    /**
     * @param list<Document> $data
     */
    public function __construct(
        private readonly ?string $id = null,
        private readonly ?string $status = null,
        private readonly int $total = 0,
        private readonly int $completed = 0,
        private readonly ?int $creditsUsed = null,
        private readonly ?string $expiresAt = null,
        private ?string $next = null,
        array $data = [],
    ) {
        $this->data = $data;
    }

    /** @param array<string, mixed> $raw */
    public static function fromArray(array $raw): self
    {
        $docs = [];
        foreach (($raw['data'] ?? []) as $item) {
            $docs[] = Document::fromArray($item);
        }

        return new self(
            id: $raw['id'] ?? null,
            status: $raw['status'] ?? null,
            total: (int) ($raw['total'] ?? 0),
            completed: (int) ($raw['completed'] ?? 0),
            creditsUsed: isset($raw['creditsUsed']) ? (int) $raw['creditsUsed'] : null,
            expiresAt: $raw['expiresAt'] ?? null,
            next: $raw['next'] ?? null,
            data: $docs,
        );
    }

    public function isDone(): bool
    {
        return in_array($this->status, ['completed', 'failed', 'cancelled'], true);
    }

    public function getId(): ?string
    {
        return $this->id;
    }

    public function getStatus(): ?string
    {
        return $this->status;
    }

    public function getTotal(): int
    {
        return $this->total;
    }

    public function getCompleted(): int
    {
        return $this->completed;
    }

    public function getCreditsUsed(): ?int
    {
        return $this->creditsUsed;
    }

    public function getExpiresAt(): ?string
    {
        return $this->expiresAt;
    }

    public function getNext(): ?string
    {
        return $this->next;
    }

    public function setNext(?string $next): void
    {
        $this->next = $next;
    }

    /** @return list<Document> */
    public function getData(): array
    {
        return $this->data;
    }

    /** @param list<Document> $data */
    public function setData(array $data): void
    {
        $this->data = $data;
    }

    public function appendData(Document $document): void
    {
        $this->data[] = $document;
    }
}