public function selectKey(string $type, ?Algorithm $algorithm = null, array $restrictions = []): ?JWK
    {
        if (!\in_array($type, ['enc', 'sig'], true)) {
            throw new InvalidArgumentException('Allowed key types are "sig" or "enc".');
        }

        $result = [];
        foreach ($this->keys as $key) {
            $ind = 0;

            $can_use = $this->canKeyBeUsedFor($type, $key);
            if (false === $can_use) {
                continue;
            }
            $ind += $can_use;

            $alg = $this->canKeyBeUsedWithAlgorithm($algorithm, $key);
            if (false === $alg) {
                continue;
            }
            $ind += $alg;

            if (false === $this->doesKeySatisfyRestrictions($restrictions, $key)) {
                continue;
            }

            $result[] = ['key' => $key, 'ind' => $ind];
        }

        if (empty($result)) {
            return null;
        }

        \usort($result, [$this, 'sortKeys']);

        return $result[0]['key'];
    }
public function selectKey(string $type, ?Algorithm $algorithm = null, array $restrictions = []): ?JWK
    {
        if (!\in_array($type, ['enc', 'sig'], true)) {
            throw new InvalidArgumentException('Allowed key types are "sig" or "enc".');
        }

        $result = [];
        foreach ($this->keys as $key) {
            $ind = 0;

            $can_use = $this->canKeyBeUsedFor($type, $key);
            if (false === $can_use) {
                continue;
            }
            $ind += $can_use;

            $alg = $this->canKeyBeUsedWithAlgorithm($algorithm, $key);
            if (false === $alg) {
                continue;
            }
            $ind += $alg;

            if (false === $this->doesKeySatisfyRestrictions($restrictions, $key)) {
                continue;
            }

            $result[] = ['key' => $key, 'ind' => $ind];
        }

        if (empty($result)) {
            return null;
        }

        \usort($result, [$this, 'sortKeys']);

        return $result[0]['key'];
    }
public function selectKey ( string $ type , ? Algorithm $ algorithm = null , array $ restrictions = [ ] ) : ? JWK { if ( ! \ in_array ( $ type , [ 'enc' , 'sig' ] , true ) ) { throw new InvalidArgumentException ( 'Allowed key types are "sig" or "enc".' ) ; } $ result = [ ] ; foreach ( $ this -> keys as $ key ) { $ ind = 0 ; $ can_use = $ this -> canKeyBeUsedFor ( $ type , $ key ) ; if ( false === $ can_use ) { continue ; } $ ind += $ can_use ; $ alg = $ this -> canKeyBeUsedWithAlgorithm ( $ algorithm , $ key ) ; if ( false === $ alg ) { continue ; } $ ind += $ alg ; if ( false === $ this -> doesKeySatisfyRestrictions ( $ restrictions , $ key ) ) { continue ; } $ result [ ] = [ 'key' => $ key , 'ind' => $ ind ] ; } if ( empty ( $ result ) ) { return null ; } \ usort ( $ result , [ $ this , 'sortKeys' ] ) ; return $ result [ 0 ] [ 'key' ] ; }
Try to find a key that fits on the selected requirements.
Returns null if not found.

@param string         $type         Must be 'sig' (signature) or 'enc' (encryption)
@param Algorithm|null $algorithm    Specifies the algorithm to be used
@param array          $restrictions More restrictions such as 'kid' or 'kty'
Try to find a key that fits on the selected requirements . Returns null if not found .