ZipEncryptionMethod.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace PhpZip\Constants;
  3. use PhpZip\Exception\InvalidArgumentException;
  4. /**
  5. * Class ZipEncryptionMethod.
  6. */
  7. final class ZipEncryptionMethod
  8. {
  9. const NONE = -1;
  10. /** @var int Traditional PKWARE encryption. */
  11. const PKWARE = 0;
  12. /** @var int WinZip AES-256 */
  13. const WINZIP_AES_256 = 1;
  14. /** @var int WinZip AES-128 */
  15. const WINZIP_AES_128 = 2;
  16. /** @var int WinZip AES-192 */
  17. const WINZIP_AES_192 = 3;
  18. /** @var array<int, string> */
  19. private static $ENCRYPTION_METHODS = [
  20. self::NONE => 'no encryption',
  21. self::PKWARE => 'Traditional PKWARE encryption',
  22. self::WINZIP_AES_128 => 'WinZip AES-128',
  23. self::WINZIP_AES_192 => 'WinZip AES-192',
  24. self::WINZIP_AES_256 => 'WinZip AES-256',
  25. ];
  26. /**
  27. * @param int $value
  28. *
  29. * @return string
  30. */
  31. public static function getEncryptionMethodName($value)
  32. {
  33. $value = (int) $value;
  34. return isset(self::$ENCRYPTION_METHODS[$value]) ?
  35. self::$ENCRYPTION_METHODS[$value] :
  36. 'Unknown Encryption Method';
  37. }
  38. /**
  39. * @param int $encryptionMethod
  40. *
  41. * @return bool
  42. */
  43. public static function hasEncryptionMethod($encryptionMethod)
  44. {
  45. return isset(self::$ENCRYPTION_METHODS[$encryptionMethod]);
  46. }
  47. /**
  48. * @param int $encryptionMethod
  49. *
  50. * @return bool
  51. */
  52. public static function isWinZipAesMethod($encryptionMethod)
  53. {
  54. return \in_array(
  55. (int) $encryptionMethod,
  56. [
  57. self::WINZIP_AES_256,
  58. self::WINZIP_AES_192,
  59. self::WINZIP_AES_128,
  60. ],
  61. true
  62. );
  63. }
  64. /**
  65. * @param int $encryptionMethod
  66. *
  67. * @throws InvalidArgumentException
  68. */
  69. public static function checkSupport($encryptionMethod)
  70. {
  71. $encryptionMethod = (int) $encryptionMethod;
  72. if (!self::hasEncryptionMethod($encryptionMethod)) {
  73. throw new InvalidArgumentException(sprintf(
  74. 'Encryption method %d is not supported.',
  75. $encryptionMethod
  76. ));
  77. }
  78. }
  79. }