<?php

declare(strict_types=1);

namespace PhpWA\Crypto\Signal;

final class SenderKeyRecord
{
    public const MAX_STATES = 5;
      private $states;
     
    public function __construct(array $states = []) { if (count($states) > self::MAX_STATES) { throw new \InvalidArgumentException('Sender key record contains too many states.'); } foreach ($states as $state) { if (!$state instanceof SenderKeyState) { throw new \InvalidArgumentException('Sender key record is invalid.'); } } $this->states = array_values($states); }
    public function isEmpty(): bool { return $this->states === []; }
      public function states(): array { return $this->states; }
    public function currentState(): ?SenderKeyState { return $this->states === [] ? null : $this->states[count($this->states) - 1]; }
    public function state(int $keyId): ?SenderKeyState { foreach ($this->states as $state) { if ($state->keyId() === $keyId) { return $state; } } return null; }
    public function replace(SenderKeyState $state): void { $this->states = [$state]; }
    public function add(SenderKeyState $state): void { $this->states[] = $state; if (count($this->states) > self::MAX_STATES) { array_shift($this->states); } }
}
