Просмотр файла vendor/nelexa/zip/src/Model/Extra/ZipExtraDriver.php

Размер файла: 3.35Kb
  1. <?php
  2.  
  3. namespace PhpZip\Model\Extra;
  4.  
  5. use PhpZip\Exception\InvalidArgumentException;
  6. use PhpZip\Model\Extra\Fields\ApkAlignmentExtraField;
  7. use PhpZip\Model\Extra\Fields\AsiExtraField;
  8. use PhpZip\Model\Extra\Fields\ExtendedTimestampExtraField;
  9. use PhpZip\Model\Extra\Fields\JarMarkerExtraField;
  10. use PhpZip\Model\Extra\Fields\NewUnixExtraField;
  11. use PhpZip\Model\Extra\Fields\NtfsExtraField;
  12. use PhpZip\Model\Extra\Fields\OldUnixExtraField;
  13. use PhpZip\Model\Extra\Fields\UnicodeCommentExtraField;
  14. use PhpZip\Model\Extra\Fields\UnicodePathExtraField;
  15. use PhpZip\Model\Extra\Fields\WinZipAesExtraField;
  16. use PhpZip\Model\Extra\Fields\Zip64ExtraField;
  17.  
  18. /**
  19. * Class ZipExtraManager.
  20. */
  21. final class ZipExtraDriver
  22. {
  23. /**
  24. * @var array<int, string>
  25. * @psalm-var array<int, class-string<\PhpZip\Model\Extra\ZipExtraField>>
  26. */
  27. private static $implementations = [
  28. ApkAlignmentExtraField::HEADER_ID => ApkAlignmentExtraField::class,
  29. AsiExtraField::HEADER_ID => AsiExtraField::class,
  30. ExtendedTimestampExtraField::HEADER_ID => ExtendedTimestampExtraField::class,
  31. JarMarkerExtraField::HEADER_ID => JarMarkerExtraField::class,
  32. NewUnixExtraField::HEADER_ID => NewUnixExtraField::class,
  33. NtfsExtraField::HEADER_ID => NtfsExtraField::class,
  34. OldUnixExtraField::HEADER_ID => OldUnixExtraField::class,
  35. UnicodeCommentExtraField::HEADER_ID => UnicodeCommentExtraField::class,
  36. UnicodePathExtraField::HEADER_ID => UnicodePathExtraField::class,
  37. WinZipAesExtraField::HEADER_ID => WinZipAesExtraField::class,
  38. Zip64ExtraField::HEADER_ID => Zip64ExtraField::class,
  39. ];
  40.  
  41. private function __construct()
  42. {
  43. }
  44.  
  45. /**
  46. * @param string|ZipExtraField $extraField ZipExtraField object or class name
  47. */
  48. public static function register($extraField)
  49. {
  50. if (!is_a($extraField, ZipExtraField::class, true)) {
  51. throw new InvalidArgumentException(
  52. sprintf(
  53. '$extraField "%s" is not implements interface %s',
  54. (string) $extraField,
  55. ZipExtraField::class
  56. )
  57. );
  58. }
  59. self::$implementations[\call_user_func([$extraField, 'getHeaderId'])] = $extraField;
  60. }
  61.  
  62. /**
  63. * @param int|string|ZipExtraField $extraType ZipExtraField object or class name or extra header id
  64. *
  65. * @return bool
  66. */
  67. public static function unregister($extraType)
  68. {
  69. $headerId = null;
  70.  
  71. if (\is_int($extraType)) {
  72. $headerId = $extraType;
  73. } elseif (is_a($extraType, ZipExtraField::class, true)) {
  74. $headerId = \call_user_func([$extraType, 'getHeaderId']);
  75. } else {
  76. return false;
  77. }
  78.  
  79. if (isset(self::$implementations[$headerId])) {
  80. unset(self::$implementations[$headerId]);
  81.  
  82. return true;
  83. }
  84.  
  85. return false;
  86. }
  87.  
  88. /**
  89. * @param int $headerId
  90. *
  91. * @return string|null
  92. */
  93. public static function getClassNameOrNull($headerId)
  94. {
  95. $headerId = (int) $headerId;
  96.  
  97. if ($headerId < 0 || $headerId > 0xffff) {
  98. throw new \InvalidArgumentException('$headerId out of range: ' . $headerId);
  99. }
  100.  
  101. if (isset(self::$implementations[$headerId])) {
  102. return self::$implementations[$headerId];
  103. }
  104.  
  105. return null;
  106. }
  107. }