Skip to content

Commit 6a8e9b6

Browse files
committed
feat: add array export for progress snapshots
1 parent 364249c commit 6a8e9b6

2 files changed

Lines changed: 61 additions & 0 deletions

File tree

src/Progressable.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,4 +644,25 @@ public function incrementStep(int $amount = 1): static {
644644

645645
return $this->setStep($current + $amount);
646646
}
647+
648+
/**
649+
* Get the current progress snapshot as an array.
650+
*
651+
* @return array<string, mixed>
652+
*/
653+
public function toArray(): array {
654+
$hasUniqueName = isset($this->overallUniqueName);
655+
656+
return [
657+
'progress' => $this->getLocalProgress(),
658+
'overall_progress' => $hasUniqueName ? $this->getOverallProgress() : null,
659+
'is_complete' => $this->isComplete(),
660+
'is_overall_complete' => $hasUniqueName ? $this->isOverallComplete() : null,
661+
'estimated_time_remaining' => null,
662+
'message' => $this->getStatusMessage(),
663+
'metadata' => $this->getMetadata(),
664+
'total_steps' => $this->getTotalSteps(),
665+
'current_step' => $this->getStep(),
666+
];
667+
}
647668
}

tests/ProgressableTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,4 +553,44 @@ public function test_set_step_without_total_steps(): void {
553553
$this->assertEquals(5, $this->getStep());
554554
$this->assertEquals(0, $this->getLocalProgress());
555555
}
556+
557+
public function test_to_array(): void {
558+
$this->setOverallUniqueName('test_to_array_'.$this->testId);
559+
$this->setTotalSteps(10);
560+
$this->setStep(5);
561+
$this->setStatusMessage('Halfway there');
562+
$this->setMetadata(['foo' => 'bar']);
563+
564+
$array = $this->toArray();
565+
566+
$this->assertEquals(50.0, $array['progress']);
567+
$this->assertEquals(50.0, $array['overall_progress']);
568+
$this->assertFalse($array['is_complete']);
569+
$this->assertFalse($array['is_overall_complete']);
570+
$this->assertNull($array['estimated_time_remaining']);
571+
$this->assertEquals('Halfway there', $array['message']);
572+
$this->assertEquals(['foo' => 'bar'], $array['metadata']);
573+
$this->assertEquals(10, $array['total_steps']);
574+
$this->assertEquals(5, $array['current_step']);
575+
}
576+
577+
public function test_to_array_without_unique_name(): void {
578+
$this->progress = 50.0;
579+
$this->totalSteps = 10;
580+
$this->currentStep = 5;
581+
$this->statusMessage = 'Halfway there';
582+
$this->metadata = ['foo' => 'bar'];
583+
584+
$this->assertEquals([
585+
'progress' => 50.0,
586+
'overall_progress' => null,
587+
'is_complete' => false,
588+
'is_overall_complete' => null,
589+
'estimated_time_remaining' => null,
590+
'message' => 'Halfway there',
591+
'metadata' => ['foo' => 'bar'],
592+
'total_steps' => 10,
593+
'current_step' => 5,
594+
], $this->toArray());
595+
}
556596
}

0 commit comments

Comments
 (0)