TokenCount.php
- Pfad:
src/Domain/ValueObject/TokenCount.php - Namespace: Domain\ValueObject
- Zeilen: 129 | Größe: 2,819 Bytes
- Geändert: 2025-12-25 09:52:39 | Gescannt: 2025-12-31 10:22:15
Code Hygiene Score: 94
- Dependencies: 100 (25%)
- LOC: 100 (20%)
- Methods: 70 (20%)
- Secrets: 100 (15%)
- Classes: 100 (10%)
- Magic Numbers: 100 (10%)
Keine Issues gefunden.
Dependencies 1
- use InvalidArgumentException
Klassen 1
-
TokenCountclass Zeile 11
Funktionen 13
-
__construct()private Zeile 16 -
create()public Zeile 27 -
fromNullable()public Zeile 43 -
zero()public Zeile 51 -
input()public Zeile 56 -
output()public Zeile 61 -
total()public Zeile 66 -
estimatedCostUsd()public Zeile 75 -
hasTokens()public Zeile 86 -
add()public Zeile 94 -
format()public Zeile 105 -
equals()public Zeile 110 -
toArray()public Zeile 120
Verwendet von 6
- ChatMessage.php constructor
- ChatMessage.php use
- ChatMessageFactory.php use
- ExecutionMetrics.php constructor
- TaskResult.php use
- TaskResultBuilder.php use
Code
<?php
declare(strict_types=1);
namespace Domain\ValueObject;
// @responsibility: Immutables Value Object für Token-Zählung (Input/Output)
use InvalidArgumentException;
final class TokenCount
{
private int $input;
private int $output;
private function __construct(int $input, int $output)
{
$this->input = $input;
$this->output = $output;
}
/**
* Create from input and output counts.
*
* @throws InvalidArgumentException
*/
public static function create(int $input, int $output): self
{
if ($input < 0) {
throw new InvalidArgumentException('Input token count cannot be negative');
}
if ($output < 0) {
throw new InvalidArgumentException('Output token count cannot be negative');
}
return new self($input, $output);
}
/**
* Create from nullable values (for database rows).
*/
public static function fromNullable(?int $input, ?int $output): self
{
return new self($input ?? 0, $output ?? 0);
}
/**
* Create zero token count.
*/
public static function zero(): self
{
return new self(0, 0);
}
public function input(): int
{
return $this->input;
}
public function output(): int
{
return $this->output;
}
public function total(): int
{
return $this->input + $this->output;
}
/**
* Calculate estimated cost in USD based on Claude pricing.
* Input: $15/1M tokens, Output: $75/1M tokens
*/
public function estimatedCostUsd(): float
{
$inputCost = $this->input * 0.000015;
$outputCost = $this->output * 0.000075;
return $inputCost + $outputCost;
}
/**
* Check if any tokens were used.
*/
public function hasTokens(): bool
{
return $this->total() > 0;
}
/**
* Add another token count to this one.
*/
public function add(self $other): self
{
return new self(
$this->input + $other->input,
$this->output + $other->output
);
}
/**
* Format as human-readable string.
*/
public function format(): string
{
return sprintf('%s in / %s out', number_format($this->input), number_format($this->output));
}
public function equals(self $other): bool
{
return $this->input === $other->input && $this->output === $other->output;
}
/**
* Convert to array for serialization.
*
* @return array{input: int, output: int, total: int}
*/
public function toArray(): array
{
return [
'input' => $this->input,
'output' => $this->output,
'total' => $this->total(),
];
}
}