Key.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace Firebase\JWT;
  3. use InvalidArgumentException;
  4. use OpenSSLAsymmetricKey;
  5. class Key
  6. {
  7. /** @var string $algorithm */
  8. private $algorithm;
  9. /** @var string|resource|OpenSSLAsymmetricKey $keyMaterial */
  10. private $keyMaterial;
  11. /**
  12. * @param string|resource|OpenSSLAsymmetricKey $keyMaterial
  13. * @param string $algorithm
  14. */
  15. public function __construct($keyMaterial, $algorithm)
  16. {
  17. if (
  18. !is_string($keyMaterial)
  19. && !is_resource($keyMaterial)
  20. && !$keyMaterial instanceof OpenSSLAsymmetricKey
  21. ) {
  22. throw new InvalidArgumentException('Type error: $keyMaterial must be a string, resource, or OpenSSLAsymmetricKey');
  23. }
  24. if (empty($keyMaterial)) {
  25. throw new InvalidArgumentException('Type error: $keyMaterial must not be empty');
  26. }
  27. if (!is_string($algorithm)|| empty($keyMaterial)) {
  28. throw new InvalidArgumentException('Type error: $algorithm must be a string');
  29. }
  30. $this->keyMaterial = $keyMaterial;
  31. $this->algorithm = $algorithm;
  32. }
  33. /**
  34. * Return the algorithm valid for this key
  35. *
  36. * @return string
  37. */
  38. public function getAlgorithm()
  39. {
  40. return $this->algorithm;
  41. }
  42. /**
  43. * @return string|resource|OpenSSLAsymmetricKey
  44. */
  45. public function getKeyMaterial()
  46. {
  47. return $this->keyMaterial;
  48. }
  49. }