<?php
 declare(strict_types=1); namespace Jose\Component\Checker; use function array_key_exists; use function count; use function sprintf; class ClaimCheckerManager { private array $checkers = []; public function __construct(iterable $checkers) { foreach ($checkers as $checker) { $this->add($checker); } } public function getCheckers(): array { return $this->checkers; } public function check(array $claims, array $mandatoryClaims = []): array { $this->checkMandatoryClaims($mandatoryClaims, $claims); $checkedClaims = []; foreach ($this->checkers as $claim => $checker) { if (array_key_exists($claim, $claims)) { $checker->checkClaim($claims[$claim]); $checkedClaims[$claim] = $claims[$claim]; } } return $checkedClaims; } private function add(ClaimChecker $checker): void { $claim = $checker->supportedClaim(); $this->checkers[$claim] = $checker; } private function checkMandatoryClaims(array $mandatoryClaims, array $claims): void { if (count($mandatoryClaims) === 0) { return; } $diff = array_keys(array_diff_key(array_flip($mandatoryClaims), $claims)); if (count($diff) !== 0) { throw new MissingMandatoryClaimException(sprintf( 'The following claims are mandatory: %s.', implode(', ', $diff) ), $diff); } } } 