UnrecognizedExtraField.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. public static function unpackLocalFileData(string $buffer, ?ZipEntry $entry = null): self
  46. {
  47. throw new RuntimeException('Parsing is not supported for unrecognized extra fields.');
  48. }
  49. /**
  50. * Populate data from this array as if it was in central directory data.
  51. *
  52. * @param string $buffer the buffer to read data from
  53. * @param ZipEntry|null $entry optional zip entry
  54. */
  55. public static function unpackCentralDirData(string $buffer, ?ZipEntry $entry = null): self
  56. {
  57. throw new RuntimeException('Parsing is not supported for unrecognized extra fields.');
  58. }
  59. /**
  60. * {@inheritDoc}
  61. */
  62. public function packLocalFileData(): string
  63. {
  64. return $this->data;
  65. }
  66. /**
  67. * {@inheritDoc}
  68. */
  69. public function packCentralDirData(): string
  70. {
  71. return $this->data;
  72. }
  73. public function getData(): string
  74. {
  75. return $this->data;
  76. }
  77. public function setData(string $data): void
  78. {
  79. $this->data = $data;
  80. }
  81. public function __toString(): string
  82. {
  83. $args = [$this->headerId, $this->data];
  84. $format = '0x%04x Unrecognized Extra Field: "%s"';
  85. return vsprintf($format, $args);
  86. }
  87. }