2
0

ExtraFieldsFactory.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace PhpZip\Extra;
  3. use PhpZip\Exception\ZipException;
  4. use PhpZip\Extra\Fields\DefaultExtraField;
  5. use PhpZip\Extra\Fields\NtfsExtraField;
  6. use PhpZip\Extra\Fields\WinZipAesEntryExtraField;
  7. use PhpZip\Extra\Fields\Zip64ExtraField;
  8. use PhpZip\Model\ZipEntry;
  9. /**
  10. * Extra Fields Factory
  11. *
  12. * @author Ne-Lexa alexey@nelexa.ru
  13. * @license MIT
  14. */
  15. class ExtraFieldsFactory
  16. {
  17. /**
  18. * @var array|null
  19. */
  20. protected static $registry;
  21. private function __construct()
  22. {
  23. }
  24. /**
  25. * A static factory method which creates a new Extra Field based on the
  26. * given Header ID.
  27. * The returned Extra Field still requires proper initialization, for
  28. * example by calling ExtraField::readFrom.
  29. *
  30. * @param int $headerId An unsigned short integer (two bytes) which indicates
  31. * the type of the returned Extra Field.
  32. * @return ExtraField A new Extra Field or null if not support header id.
  33. * @throws ZipException If headerId is out of range.
  34. */
  35. public static function create($headerId)
  36. {
  37. if (0x0000 > $headerId || $headerId > 0xffff) {
  38. throw new ZipException('headerId out of range');
  39. }
  40. /**
  41. * @var ExtraField $extraField
  42. */
  43. if (isset(self::getRegistry()[$headerId])) {
  44. $extraClassName = self::getRegistry()[$headerId];
  45. $extraField = new $extraClassName;
  46. if ($extraField::getHeaderId() !== $headerId) {
  47. throw new ZipException('Runtime error support headerId ' . $headerId);
  48. }
  49. } else {
  50. $extraField = new DefaultExtraField($headerId);
  51. }
  52. return $extraField;
  53. }
  54. /**
  55. * Registered extra field classes.
  56. *
  57. * @return array
  58. */
  59. protected static function getRegistry()
  60. {
  61. if (null === self::$registry) {
  62. self::$registry[WinZipAesEntryExtraField::getHeaderId()] = WinZipAesEntryExtraField::class;
  63. self::$registry[NtfsExtraField::getHeaderId()] = NtfsExtraField::class;
  64. self::$registry[Zip64ExtraField::getHeaderId()] = Zip64ExtraField::class;
  65. }
  66. return self::$registry;
  67. }
  68. /**
  69. * @return WinZipAesEntryExtraField
  70. */
  71. public static function createWinZipAesEntryExtra()
  72. {
  73. return new WinZipAesEntryExtraField();
  74. }
  75. /**
  76. * @return NtfsExtraField
  77. */
  78. public static function createNtfsExtra()
  79. {
  80. return new NtfsExtraField();
  81. }
  82. /**
  83. * @param ZipEntry $entry
  84. * @return Zip64ExtraField
  85. */
  86. public static function createZip64Extra(ZipEntry $entry)
  87. {
  88. return new Zip64ExtraField($entry);
  89. }
  90. }