[ Index ] |
PHP Cross Reference of Joomla 4.2.2 documentation |
[Summary view] [Print] [Text view]
1 <?php 2 3 declare(strict_types=1); 4 5 /* 6 * The MIT License (MIT) 7 * 8 * Copyright (c) 2014-2020 Spomky-Labs 9 * 10 * This software may be modified and distributed under the terms 11 * of the MIT license. See the LICENSE file for details. 12 */ 13 14 namespace Jose\Component\Signature\Serializer; 15 16 use InvalidArgumentException; 17 use Jose\Component\Signature\JWS; 18 19 class JWSSerializerManager 20 { 21 /** 22 * @var JWSSerializer[] 23 */ 24 private $serializers = []; 25 26 /** 27 * @param JWSSerializer[] $serializers 28 */ 29 public function __construct(array $serializers) 30 { 31 foreach ($serializers as $serializer) { 32 $this->add($serializer); 33 } 34 } 35 36 /** 37 * @return string[] 38 */ 39 public function list(): array 40 { 41 return array_keys($this->serializers); 42 } 43 44 /** 45 * Converts a JWS into a string. 46 * 47 * @throws InvalidArgumentException if the serializer is not supported 48 */ 49 public function serialize(string $name, JWS $jws, ?int $signatureIndex = null): string 50 { 51 if (!isset($this->serializers[$name])) { 52 throw new InvalidArgumentException(sprintf('Unsupported serializer "%s".', $name)); 53 } 54 55 return $this->serializers[$name]->serialize($jws, $signatureIndex); 56 } 57 58 /** 59 * Loads data and return a JWS object. 60 * 61 * @param string $input A string that represents a JWS 62 * @param null|string $name the name of the serializer if the input is unserialized 63 * 64 * @throws InvalidArgumentException if the input is not supported 65 */ 66 public function unserialize(string $input, ?string &$name = null): JWS 67 { 68 foreach ($this->serializers as $serializer) { 69 try { 70 $jws = $serializer->unserialize($input); 71 $name = $serializer->name(); 72 73 return $jws; 74 } catch (InvalidArgumentException $e) { 75 continue; 76 } 77 } 78 79 throw new InvalidArgumentException('Unsupported input.'); 80 } 81 82 private function add(JWSSerializer $serializer): void 83 { 84 $this->serializers[$serializer->name()] = $serializer; 85 } 86 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Sep 7 05:41:13 2022 | Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer |