Version.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. <?php
  2. /*
  3. * This file is part of Packagist.
  4. *
  5. * (c) Jordi Boggiano <j.boggiano@seld.be>
  6. * Nils Adermann <naderman@naderman.de>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Packagist\WebBundle\Entity;
  12. use Doctrine\ORM\Mapping as ORM;
  13. use Symfony\Component\Validator\Constraints as Assert;
  14. /**
  15. * @ORM\Entity
  16. * @ORM\Table(
  17. * name="package_version",
  18. * uniqueConstraints={@ORM\UniqueConstraint(name="pkg_ver_idx",columns={"package_id","version"})}
  19. * )
  20. * @author Jordi Boggiano <j.boggiano@seld.be>
  21. */
  22. class Version
  23. {
  24. /**
  25. * @ORM\Id
  26. * @ORM\Column(type="integer")
  27. * @ORM\GeneratedValue(strategy="AUTO")
  28. */
  29. private $id;
  30. /**
  31. * @ORM\Column
  32. * @Assert\NotBlank()
  33. */
  34. private $name;
  35. /**
  36. * @ORM\Column(type="text", nullable="true")
  37. */
  38. private $description;
  39. /**
  40. * @ORM\ManyToMany(targetEntity="Packagist\WebBundle\Entity\Tag", inversedBy="versions")
  41. * @ORM\JoinTable(name="version_tag",
  42. * joinColumns={@ORM\JoinColumn(name="version_id", referencedColumnName="id")},
  43. * inverseJoinColumns={@ORM\JoinColumn(name="tag_id", referencedColumnName="id")}
  44. * )
  45. */
  46. private $tags;
  47. /**
  48. * @ORM\ManyToOne(targetEntity="Packagist\WebBundle\Entity\Package", fetch="EAGER", inversedBy="versions")
  49. * @Assert\Type(type="Packagist\WebBundle\Entity\Package")
  50. */
  51. private $package;
  52. /**
  53. * @ORM\Column(nullable="true")
  54. * @Assert\Url()
  55. */
  56. private $homepage;
  57. /**
  58. * @ORM\Column
  59. * @Assert\NotBlank()
  60. */
  61. private $version;
  62. /**
  63. * @ORM\Column(nullable="true")
  64. */
  65. private $license;
  66. /**
  67. * @ORM\ManyToMany(targetEntity="Packagist\WebBundle\Entity\Author", inversedBy="versions")
  68. * @ORM\JoinTable(name="version_author",
  69. * joinColumns={@ORM\JoinColumn(name="version_id", referencedColumnName="id")},
  70. * inverseJoinColumns={@ORM\JoinColumn(name="author_id", referencedColumnName="id")}
  71. * )
  72. */
  73. private $authors;
  74. /**
  75. * @ORM\OneToMany(targetEntity="Packagist\WebBundle\Entity\Requirement", mappedBy="version")
  76. */
  77. private $requirements;
  78. /**
  79. * @ORM\Column(type="text")
  80. */
  81. private $source;
  82. /**
  83. * @ORM\Column(type="text")
  84. */
  85. private $dist;
  86. /**
  87. * @ORM\Column(type="datetime")
  88. */
  89. private $createdAt;
  90. /**
  91. * @ORM\Column(type="datetime")
  92. */
  93. private $updatedAt;
  94. /**
  95. * @ORM\Column(type="datetime")
  96. * @Assert\NotBlank()
  97. */
  98. private $releasedAt;
  99. public function __construct()
  100. {
  101. $this->tags = new \Doctrine\Common\Collections\ArrayCollection();
  102. $this->createdAt = new \DateTime;
  103. $this->updatedAt = new \DateTime;
  104. }
  105. public function toArray()
  106. {
  107. $tags = array();
  108. foreach ($this->getTags() as $tag) {
  109. $tags[] = $tag->getName();
  110. }
  111. $authors = array();
  112. foreach ($this->getAuthors() as $author) {
  113. $authors[] = $author->toArray();
  114. }
  115. $requirements = array();
  116. foreach ($this->getRequirements() as $requirement) {
  117. $requirement = $requirement->toArray();
  118. $requirements[key($requirement)] = current($requirement);
  119. }
  120. return array(
  121. 'name' => $this->name,
  122. 'description' => $this->description,
  123. 'keywords' => $tags,
  124. 'homepage' => $this->homepage,
  125. 'version' => $this->version,
  126. 'license' => $this->license,
  127. 'authors' => $authors,
  128. 'require' => $requirements,
  129. 'source' => $this->getSource(),
  130. 'time' => $this->releasedAt ? $this->releasedAt->format('Y-m-d\TH:i:sP') : null,
  131. 'dist' => $this->getDist(),
  132. );
  133. }
  134. /**
  135. * Get id
  136. *
  137. * @return string $id
  138. */
  139. public function getId()
  140. {
  141. return $this->id;
  142. }
  143. /**
  144. * Set name
  145. *
  146. * @param string $name
  147. */
  148. public function setName($name)
  149. {
  150. $this->name = $name;
  151. }
  152. /**
  153. * Get name
  154. *
  155. * @return string $name
  156. */
  157. public function getName()
  158. {
  159. return $this->name;
  160. }
  161. /**
  162. * Set description
  163. *
  164. * @param text $description
  165. */
  166. public function setDescription($description)
  167. {
  168. $this->description = $description;
  169. }
  170. /**
  171. * Get description
  172. *
  173. * @return text $description
  174. */
  175. public function getDescription()
  176. {
  177. return $this->description;
  178. }
  179. /**
  180. * Set homepage
  181. *
  182. * @param string $homepage
  183. */
  184. public function setHomepage($homepage)
  185. {
  186. $this->homepage = $homepage;
  187. }
  188. /**
  189. * Get homepage
  190. *
  191. * @return string $homepage
  192. */
  193. public function getHomepage()
  194. {
  195. return $this->homepage;
  196. }
  197. /**
  198. * Set version
  199. *
  200. * @param string $version
  201. */
  202. public function setVersion($version)
  203. {
  204. $this->version = ltrim($version, 'vV.');
  205. }
  206. /**
  207. * Get version
  208. *
  209. * @return string $version
  210. */
  211. public function getVersion()
  212. {
  213. return $this->version;
  214. }
  215. /**
  216. * Set license
  217. *
  218. * @param string $license
  219. */
  220. public function setLicense($license)
  221. {
  222. $this->license = $license;
  223. }
  224. /**
  225. * Get license
  226. *
  227. * @return string $license
  228. */
  229. public function getLicense()
  230. {
  231. return $this->license;
  232. }
  233. /**
  234. * Set source
  235. *
  236. * @param text $source
  237. */
  238. public function setSource($source)
  239. {
  240. $this->source = json_encode($source);
  241. }
  242. /**
  243. * Get source
  244. *
  245. * @return text $source
  246. */
  247. public function getSource()
  248. {
  249. return json_decode($this->source, true);
  250. }
  251. /**
  252. * Set dist
  253. *
  254. * @param text $dist
  255. */
  256. public function setDist($dist)
  257. {
  258. $this->dist = json_encode($dist);
  259. }
  260. /**
  261. * Get dist
  262. *
  263. * @return text
  264. */
  265. public function getDist()
  266. {
  267. return json_decode($this->dist, true);
  268. }
  269. /**
  270. * Set createdAt
  271. *
  272. * @param datetime $createdAt
  273. */
  274. public function setCreatedAt($createdAt)
  275. {
  276. $this->createdAt = $createdAt;
  277. }
  278. /**
  279. * Get createdAt
  280. *
  281. * @return datetime $createdAt
  282. */
  283. public function getCreatedAt()
  284. {
  285. return $this->createdAt;
  286. }
  287. /**
  288. * Set releasedAt
  289. *
  290. * @param datetime $releasedAt
  291. */
  292. public function setReleasedAt($releasedAt)
  293. {
  294. $this->releasedAt = $releasedAt;
  295. }
  296. /**
  297. * Get releasedAt
  298. *
  299. * @return datetime $releasedAt
  300. */
  301. public function getReleasedAt()
  302. {
  303. return $this->releasedAt;
  304. }
  305. /**
  306. * Set package
  307. *
  308. * @param Packagist\WebBundle\Entity\Package $package
  309. */
  310. public function setPackage(Package $package)
  311. {
  312. $this->package = $package;
  313. }
  314. /**
  315. * Get package
  316. *
  317. * @return Packagist\WebBundle\Entity\Package $package
  318. */
  319. public function getPackage()
  320. {
  321. return $this->package;
  322. }
  323. /**
  324. * Add tags
  325. *
  326. * @param Packagist\WebBundle\Entity\Tag $tags
  327. */
  328. public function addTags(Tag $tags)
  329. {
  330. $this->tags[] = $tags;
  331. }
  332. /**
  333. * Get tags
  334. *
  335. * @return Doctrine\Common\Collections\Collection $tags
  336. */
  337. public function getTags()
  338. {
  339. return $this->tags;
  340. }
  341. public function setTagsText($text)
  342. {
  343. $tags = array();
  344. if (trim($text)) {
  345. $tags = preg_split('#[\s,]+#', trim($text));
  346. $tags = array_map(function($el) {
  347. return trim(ltrim($el, '#'), '"\'');
  348. }, $tags);
  349. $uniqueTags = array();
  350. foreach ($tags as $tag) {
  351. if ($tag && !isset($uniqueTags[strtolower($tag)])) {
  352. $uniqueTags[strtolower($tag)] = $tag;
  353. }
  354. }
  355. $tags = array_values($uniqueTags);
  356. }
  357. foreach ($this->tags as $k => $tag) {
  358. if (false !== ($idx = array_search($tag->getName(), $tags))) {
  359. unset($tags[$idx]);
  360. } else {
  361. unset($this->tags[$k]);
  362. }
  363. }
  364. foreach ($tags as $tag) {
  365. $this->addTags($this->getTagEntity($tag));
  366. }
  367. }
  368. public function setEntityManager($em)
  369. {
  370. $this->em = $em;
  371. }
  372. protected function getTagEntity($name)
  373. {
  374. return Tag::getByName($this->em, $name, true);
  375. }
  376. public function getTagsText()
  377. {
  378. $tags = array();
  379. foreach ($this->tags as $tag) {
  380. $tags[] = $tag->getName();
  381. }
  382. return implode(', ', $tags);
  383. }
  384. /**
  385. * Set updatedAt
  386. *
  387. * @param datetime $updatedAt
  388. */
  389. public function setUpdatedAt($updatedAt)
  390. {
  391. $this->updatedAt = $updatedAt;
  392. }
  393. /**
  394. * Get updatedAt
  395. *
  396. * @return datetime $updatedAt
  397. */
  398. public function getUpdatedAt()
  399. {
  400. return $this->updatedAt;
  401. }
  402. /**
  403. * Add authors
  404. *
  405. * @param Packagist\WebBundle\Entity\Author $authors
  406. */
  407. public function addAuthors(Author $authors)
  408. {
  409. $this->authors[] = $authors;
  410. }
  411. /**
  412. * Get authors
  413. *
  414. * @return Doctrine\Common\Collections\Collection
  415. */
  416. public function getAuthors()
  417. {
  418. return $this->authors;
  419. }
  420. /**
  421. * Add requirements
  422. *
  423. * @param Packagist\WebBundle\Entity\Requirement $requirements
  424. */
  425. public function addRequirements(Requirement $requirements)
  426. {
  427. $this->requirements[] = $requirements;
  428. }
  429. /**
  430. * Get requirements
  431. *
  432. * @return Doctrine\Common\Collections\Collection
  433. */
  434. public function getRequirements()
  435. {
  436. return $this->requirements;
  437. }
  438. }