Files
openidea/fixtures/php-app/src/Shipping/Shipment.php
T
lleviathe f50d3099ce test(smoke): smoke-test PHP on a php-app fixture (#2189)
PHP had no regression coverage. Adds a php-app fixture (Composer + Pest) with language, run and debug smoke cases, and a php entry in the Language smoke workflow: PHP 8.5 with Xdebug via setup-php, Intelephense pinned like the other servers. Other languages' selectors are unchanged.

Co-authored-by: lleviathe <95321986+lleviathe@users.noreply.github.com>
2026-11-10 12:10:51 +08:00

74 lines
1.7 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Shipping;
use ArrayIterator;
use Countable;
use IteratorAggregate;
/** @implements IteratorAggregate<int, Parcel> */
final class Shipment implements Countable, IteratorAggregate
{
/** @var list<Parcel> */
public private(set) array $parcels = [];
public function __construct(
public readonly string $flight,
public readonly Route $route,
) {}
public function add(Parcel $parcel): self
{
$this->parcels[] = $parcel;
return $this;
}
public function heaviest(): ?Parcel
{
return $this->parcels
|> (fn (array $parcels): array => $this->sortedByBillableWeight($parcels))
|> array_last(...);
}
public function isReadyForDeparture(): bool
{
return array_all($this->parcels, fn (Parcel $parcel): bool => $parcel->status === ParcelStatus::ContentsConfirmed);
}
/** @return array<string, list<Parcel>> */
public function byStore(): array
{
$groups = [];
foreach ($this->parcels as $parcel) {
$groups[$parcel->store][] = $parcel;
}
return $groups;
}
/** @return ArrayIterator<int, Parcel> */
public function getIterator(): ArrayIterator
{
return new ArrayIterator($this->parcels);
}
public function count(): int
{
return count($this->parcels);
}
/**
* @param list<Parcel> $parcels
* @return list<Parcel>
*/
private function sortedByBillableWeight(array $parcels): array
{
usort($parcels, fn (Parcel $a, Parcel $b): int => $a->billableGrams($this->route) <=> $b->billableGrams($this->route));
return $parcels;
}
}