ManifestDocumentMapper.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php declare(strict_types = 1);
  2. /*
  3. * This file is part of PharIo\Manifest.
  4. *
  5. * (c) Arne Blankerts <arne@blankerts.de>, Sebastian Heuer <sebastian@phpeople.de>, Sebastian Bergmann <sebastian@phpunit.de>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace PharIo\Manifest;
  11. use PharIo\Version\Exception as VersionException;
  12. use PharIo\Version\Version;
  13. use PharIo\Version\VersionConstraintParser;
  14. class ManifestDocumentMapper {
  15. public function map(ManifestDocument $document): Manifest {
  16. try {
  17. $contains = $document->getContainsElement();
  18. $type = $this->mapType($contains);
  19. $copyright = $this->mapCopyright($document->getCopyrightElement());
  20. $requirements = $this->mapRequirements($document->getRequiresElement());
  21. $bundledComponents = $this->mapBundledComponents($document);
  22. return new Manifest(
  23. new ApplicationName($contains->getName()),
  24. new Version($contains->getVersion()),
  25. $type,
  26. $copyright,
  27. $requirements,
  28. $bundledComponents
  29. );
  30. } catch (VersionException $e) {
  31. throw new ManifestDocumentMapperException($e->getMessage(), (int)$e->getCode(), $e);
  32. } catch (Exception $e) {
  33. throw new ManifestDocumentMapperException($e->getMessage(), (int)$e->getCode(), $e);
  34. }
  35. }
  36. private function mapType(ContainsElement $contains): Type {
  37. switch ($contains->getType()) {
  38. case 'application':
  39. return Type::application();
  40. case 'library':
  41. return Type::library();
  42. case 'extension':
  43. return $this->mapExtension($contains->getExtensionElement());
  44. }
  45. throw new ManifestDocumentMapperException(
  46. \sprintf('Unsupported type %s', $contains->getType())
  47. );
  48. }
  49. private function mapCopyright(CopyrightElement $copyright): CopyrightInformation {
  50. $authors = new AuthorCollection();
  51. foreach ($copyright->getAuthorElements() as $authorElement) {
  52. $authors->add(
  53. new Author(
  54. $authorElement->getName(),
  55. new Email($authorElement->getEmail())
  56. )
  57. );
  58. }
  59. $licenseElement = $copyright->getLicenseElement();
  60. $license = new License(
  61. $licenseElement->getType(),
  62. new Url($licenseElement->getUrl())
  63. );
  64. return new CopyrightInformation(
  65. $authors,
  66. $license
  67. );
  68. }
  69. private function mapRequirements(RequiresElement $requires): RequirementCollection {
  70. $collection = new RequirementCollection();
  71. $phpElement = $requires->getPHPElement();
  72. $parser = new VersionConstraintParser;
  73. try {
  74. $versionConstraint = $parser->parse($phpElement->getVersion());
  75. } catch (VersionException $e) {
  76. throw new ManifestDocumentMapperException(
  77. \sprintf('Unsupported version constraint - %s', $e->getMessage()),
  78. (int)$e->getCode(),
  79. $e
  80. );
  81. }
  82. $collection->add(
  83. new PhpVersionRequirement(
  84. $versionConstraint
  85. )
  86. );
  87. if (!$phpElement->hasExtElements()) {
  88. return $collection;
  89. }
  90. foreach ($phpElement->getExtElements() as $extElement) {
  91. $collection->add(
  92. new PhpExtensionRequirement($extElement->getName())
  93. );
  94. }
  95. return $collection;
  96. }
  97. private function mapBundledComponents(ManifestDocument $document): BundledComponentCollection {
  98. $collection = new BundledComponentCollection();
  99. if (!$document->hasBundlesElement()) {
  100. return $collection;
  101. }
  102. foreach ($document->getBundlesElement()->getComponentElements() as $componentElement) {
  103. $collection->add(
  104. new BundledComponent(
  105. $componentElement->getName(),
  106. new Version(
  107. $componentElement->getVersion()
  108. )
  109. )
  110. );
  111. }
  112. return $collection;
  113. }
  114. private function mapExtension(ExtensionElement $extension): Extension {
  115. try {
  116. $versionConstraint = (new VersionConstraintParser)->parse($extension->getCompatible());
  117. return Type::extension(
  118. new ApplicationName($extension->getFor()),
  119. $versionConstraint
  120. );
  121. } catch (VersionException $e) {
  122. throw new ManifestDocumentMapperException(
  123. \sprintf('Unsupported version constraint - %s', $e->getMessage()),
  124. (int)$e->getCode(),
  125. $e
  126. );
  127. }
  128. }
  129. }