ManifestDocumentMapper.php 4.9 KB

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