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