UnrecognizedExtraField.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of the nelexa/zip package.
  5. * (c) Ne-Lexa <https://github.com/Ne-Lexa/php-zip>
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. namespace PhpZip\Model\Extra\Fields;
  10. use PhpZip\Exception\RuntimeException;
  11. use PhpZip\Model\Extra\ZipExtraField;
  12. use PhpZip\Model\ZipEntry;
  13. /**
  14. * Simple placeholder for all those extra fields we don't want to deal with.
  15. */
  16. final class UnrecognizedExtraField implements ZipExtraField
  17. {
  18. private int $headerId;
  19. /** @var string extra field data without Header-ID or length specifier */
  20. private string $data;
  21. public function __construct(int $headerId, string $data)
  22. {
  23. $this->headerId = $headerId;
  24. $this->data = $data;
  25. }
  26. public function setHeaderId(int $headerId): void
  27. {
  28. $this->headerId = $headerId;
  29. }
  30. /**
  31. * Returns the Header ID (type) of this Extra Field.
  32. * The Header ID is an unsigned short integer (two bytes)
  33. * which must be constant during the life cycle of this object.
  34. */
  35. public function getHeaderId(): int
  36. {
  37. return $this->headerId;
  38. }
  39. /**
  40. * Populate data from this array as if it was in local file data.
  41. *
  42. * @param string $buffer the buffer to read data from
  43. * @param ZipEntry|null $entry optional zip entry
  44. *
  45. * @return UnrecognizedExtraField
  46. */
  47. public static function unpackLocalFileData(string $buffer, ?ZipEntry $entry = null): self
  48. {
  49. throw new RuntimeException('Unsupport parse');
  50. }
  51. /**
  52. * Populate data from this array as if it was in central directory data.
  53. *
  54. * @param string $buffer the buffer to read data from
  55. * @param ZipEntry|null $entry optional zip entry
  56. *
  57. * @return UnrecognizedExtraField
  58. */
  59. public static function unpackCentralDirData(string $buffer, ?ZipEntry $entry = null): self
  60. {
  61. throw new RuntimeException('Unsupport parse');
  62. }
  63. /**
  64. * {@inheritDoc}
  65. */
  66. public function packLocalFileData(): string
  67. {
  68. return $this->data;
  69. }
  70. /**
  71. * {@inheritDoc}
  72. */
  73. public function packCentralDirData(): string
  74. {
  75. return $this->data;
  76. }
  77. public function getData(): string
  78. {
  79. return $this->data;
  80. }
  81. public function setData(string $data): void
  82. {
  83. $this->data = $data;
  84. }
  85. public function __toString(): string
  86. {
  87. $args = [$this->headerId, $this->data];
  88. $format = '0x%04x Unrecognized Extra Field: "%s"';
  89. return vsprintf($format, $args);
  90. }
  91. }