ManifestDocumentMapper.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  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\Version;
  12. use PharIo\Version\Exception as VersionException;
  13. use PharIo\Version\VersionConstraintParser;
  14. class ManifestDocumentMapper {
  15. /**
  16. * @param ManifestDocument $document
  17. *
  18. * @returns Manifest
  19. *
  20. * @throws ManifestDocumentMapperException
  21. */
  22. public function map(ManifestDocument $document) {
  23. try {
  24. $contains = $document->getContainsElement();
  25. $type = $this->mapType($contains);
  26. $copyright = $this->mapCopyright($document->getCopyrightElement());
  27. $requirements = $this->mapRequirements($document->getRequiresElement());
  28. $bundledComponents = $this->mapBundledComponents($document);
  29. return new Manifest(
  30. new ApplicationName($contains->getName()),
  31. new Version($contains->getVersion()),
  32. $type,
  33. $copyright,
  34. $requirements,
  35. $bundledComponents
  36. );
  37. } catch (VersionException $e) {
  38. throw new ManifestDocumentMapperException($e->getMessage(), $e->getCode(), $e);
  39. } catch (Exception $e) {
  40. throw new ManifestDocumentMapperException($e->getMessage(), $e->getCode(), $e);
  41. }
  42. }
  43. /**
  44. * @param ContainsElement $contains
  45. *
  46. * @return Type
  47. *
  48. * @throws ManifestDocumentMapperException
  49. */
  50. private function mapType(ContainsElement $contains) {
  51. switch ($contains->getType()) {
  52. case 'application':
  53. return Type::application();
  54. case 'library':
  55. return Type::library();
  56. case 'extension':
  57. return $this->mapExtension($contains->getExtensionElement());
  58. }
  59. throw new ManifestDocumentMapperException(
  60. sprintf('Unsupported type %s', $contains->getType())
  61. );
  62. }
  63. /**
  64. * @param CopyrightElement $copyright
  65. *
  66. * @return CopyrightInformation
  67. *
  68. * @throws InvalidUrlException
  69. * @throws InvalidEmailException
  70. */
  71. private function mapCopyright(CopyrightElement $copyright) {
  72. $authors = new AuthorCollection();
  73. foreach($copyright->getAuthorElements() as $authorElement) {
  74. $authors->add(
  75. new Author(
  76. $authorElement->getName(),
  77. new Email($authorElement->getEmail())
  78. )
  79. );
  80. }
  81. $licenseElement = $copyright->getLicenseElement();
  82. $license = new License(
  83. $licenseElement->getType(),
  84. new Url($licenseElement->getUrl())
  85. );
  86. return new CopyrightInformation(
  87. $authors,
  88. $license
  89. );
  90. }
  91. /**
  92. * @param RequiresElement $requires
  93. *
  94. * @return RequirementCollection
  95. *
  96. * @throws ManifestDocumentMapperException
  97. */
  98. private function mapRequirements(RequiresElement $requires) {
  99. $collection = new RequirementCollection();
  100. $phpElement = $requires->getPHPElement();
  101. $parser = new VersionConstraintParser;
  102. try {
  103. $versionConstraint = $parser->parse($phpElement->getVersion());
  104. } catch (VersionException $e) {
  105. throw new ManifestDocumentMapperException(
  106. sprintf('Unsupported version constraint - %s', $e->getMessage()),
  107. $e->getCode(),
  108. $e
  109. );
  110. }
  111. $collection->add(
  112. new PhpVersionRequirement(
  113. $versionConstraint
  114. )
  115. );
  116. if (!$phpElement->hasExtElements()) {
  117. return $collection;
  118. }
  119. foreach($phpElement->getExtElements() as $extElement) {
  120. $collection->add(
  121. new PhpExtensionRequirement($extElement->getName())
  122. );
  123. }
  124. return $collection;
  125. }
  126. /**
  127. * @param ManifestDocument $document
  128. *
  129. * @return BundledComponentCollection
  130. */
  131. private function mapBundledComponents(ManifestDocument $document) {
  132. $collection = new BundledComponentCollection();
  133. if (!$document->hasBundlesElement()) {
  134. return $collection;
  135. }
  136. foreach($document->getBundlesElement()->getComponentElements() as $componentElement) {
  137. $collection->add(
  138. new BundledComponent(
  139. $componentElement->getName(),
  140. new Version(
  141. $componentElement->getVersion()
  142. )
  143. )
  144. );
  145. }
  146. return $collection;
  147. }
  148. /**
  149. * @param ExtensionElement $extension
  150. *
  151. * @return Extension
  152. *
  153. * @throws ManifestDocumentMapperException
  154. */
  155. private function mapExtension(ExtensionElement $extension) {
  156. try {
  157. $parser = new VersionConstraintParser;
  158. $versionConstraint = $parser->parse($extension->getCompatible());
  159. return Type::extension(
  160. new ApplicationName($extension->getFor()),
  161. $versionConstraint
  162. );
  163. } catch (VersionException $e) {
  164. throw new ManifestDocumentMapperException(
  165. sprintf('Unsupported version constraint - %s', $e->getMessage()),
  166. $e->getCode(),
  167. $e
  168. );
  169. }
  170. }
  171. }