$billableQuarters Keyed by work type id. */ public function __construct( public int $jobId, public int $customerId, public ?int $billingUnitId, public ?string $reference, public array $billableQuarters = [], ) {} /** * @throws InvalidBillableHours */ public function withBillableQuarters(int $workTypeId, ?int $quarters): self { $billable = $this->billableQuarters; if ($quarters === null) { unset($billable[$workTypeId]); } elseif ($quarters < 0 || $quarters > self::MAX_QUARTERS) { throw new InvalidBillableHours; } else { $billable[$workTypeId] = $quarters; } return new self($this->jobId, $this->customerId, $this->billingUnitId, $this->reference, $billable); } /** * The billable quarters of a work type: management's own, or the approved ones. */ public function billableOf(int $workTypeId, int $approvedQuarters): int { return $this->billableQuarters[$workTypeId] ?? $approvedQuarters; } /** * Parses billable hours such as "2,5", "2.5" or "2" to quarters of an * hour; null for an empty value (back to the approved hours). * * @throws InvalidBillableHours */ public static function parseHours(string $value): ?int { $value = str_replace(',', '.', trim($value)); if ($value === '') { return null; } if (preg_match('/^\d{1,4}(\.\d{1,2})?$/', $value) !== 1) { throw new InvalidBillableHours; } $quarters = (float) $value * 4; if (abs($quarters - round($quarters)) > 0.0001) { throw new InvalidBillableHours; } return (int) round($quarters); } }