InMemory.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. namespace Lcobucci\JWT\Signer\Key;
  3. use Lcobucci\JWT\Encoding\CannotDecodeContent;
  4. use Lcobucci\JWT\Signer\Key;
  5. use function base64_decode;
  6. final class InMemory extends Key
  7. {
  8. /**
  9. * @param string $contents
  10. * @param string $passphrase
  11. *
  12. * @return self
  13. */
  14. public static function plainText($contents, $passphrase = '')
  15. {
  16. return new self($contents, $passphrase);
  17. }
  18. /**
  19. * @param string $contents
  20. * @param string $passphrase
  21. *
  22. * @return self
  23. */
  24. public static function base64Encoded($contents, $passphrase = '')
  25. {
  26. $decoded = base64_decode($contents, true);
  27. if ($decoded === false) {
  28. throw CannotDecodeContent::invalidBase64String();
  29. }
  30. return new self($decoded, $passphrase);
  31. }
  32. /**
  33. * @param string $path
  34. * @param string $passphrase
  35. *
  36. * @return InMemory
  37. *
  38. * @throws FileCouldNotBeRead
  39. */
  40. public static function file($path, $passphrase = '')
  41. {
  42. return new self('file://' . $path, $passphrase);
  43. }
  44. }