1<?php 2 3/* 4 * This file is part of the Fxp Composer Asset Plugin package. 5 * 6 * (c) François Pluchino <francois.pluchino@gmail.com> 7 * 8 * For the full copyright and license information, please view the LICENSE 9 * file that was distributed with this source code. 10 */ 11 12namespace Fxp\Composer\AssetPlugin\Converter; 13 14/** 15 * Utils for NPM package converter. 16 * 17 * @author François Pluchino <francois.pluchino@gmail.com> 18 */ 19abstract class NpmPackageUtil 20{ 21 /** 22 * Convert the npm package name. 23 * 24 * @param string $name The npm package name 25 * 26 * @return string 27 */ 28 public static function convertName($name) 29 { 30 if (0 === strpos($name, '@') && false !== strpos($name, '/')) { 31 $name = ltrim(str_replace('/', '--', $name), '@'); 32 } 33 34 return $name; 35 } 36 37 /** 38 * Revert the npm package name from composer package name. 39 * 40 * @param string $name The npm package name 41 * 42 * @return string 43 */ 44 public static function revertName($name) 45 { 46 if (false !== strpos($name, '--')) { 47 $name = '@'.str_replace('--', '/', $name); 48 } 49 50 return $name; 51 } 52 53 /** 54 * Convert the npm licenses list. 55 * 56 * @param array|string $licenses The npm package licenses list 57 * 58 * @return array|string 59 */ 60 public static function convertLicenses($licenses) 61 { 62 if (!\is_array($licenses)) { 63 return $licenses; 64 } 65 66 $result = array(); 67 foreach ($licenses as $license) { 68 if (\is_array($license)) { 69 if (!empty($license['type'])) { 70 $result[] = $license['type']; 71 } elseif (!empty($license['name'])) { 72 $result[] = $license['name']; 73 } 74 } else { 75 $result[] = $license; 76 } 77 } 78 79 return $result; 80 } 81 82 /** 83 * Convert the author section. 84 * 85 * @param null|string $value The current value 86 * 87 * @return array 88 */ 89 public static function convertAuthor($value) 90 { 91 if (null !== $value) { 92 $value = array($value); 93 } 94 95 return $value; 96 } 97 98 /** 99 * Convert the contributors section. 100 * 101 * @param null|string $value The current value 102 * @param null|string $prevValue The previous value 103 * 104 * @return array 105 */ 106 public static function convertContributors($value, $prevValue) 107 { 108 $mergeValue = \is_array($prevValue) ? $prevValue : array(); 109 $mergeValue = array_merge($mergeValue, \is_array($value) ? $value : array()); 110 111 if (\count($mergeValue) > 0) { 112 $value = $mergeValue; 113 } 114 115 return $value; 116 } 117 118 /** 119 * Convert the dist section. 120 * 121 * @param null|string $value The current value 122 * 123 * @return array 124 */ 125 public static function convertDist($value) 126 { 127 if (\is_array($value)) { 128 $data = (array) $value; 129 $value = array(); 130 131 foreach ($data as $type => $url) { 132 if (\is_string($url)) { 133 self::convertDistEntry($value, $type, $url); 134 } 135 } 136 } 137 138 return $value; 139 } 140 141 /** 142 * Convert the each entry of dist section. 143 * 144 * @param array $value The result 145 * @param string $type The dist type 146 * @param string $url The dist url 147 */ 148 private static function convertDistEntry(array &$value, $type, $url) 149 { 150 $httpPrefix = 'http://'; 151 152 if (0 === strpos($url, $httpPrefix)) { 153 $url = 'https://'.substr($url, \strlen($httpPrefix)); 154 } 155 156 if ('shasum' === $type) { 157 $value[$type] = $url; 158 } elseif ('tarball' === $type) { 159 $value['type'] = 'tar'; 160 $value['url'] = $url; 161 } elseif (\in_array($type, self::getDownloaderTypes(), true)) { 162 $value['type'] = $type; 163 $value['url'] = $url; 164 } 165 } 166 167 /** 168 * Get downloader types in Composer. 169 * 170 * @return array 171 */ 172 private static function getDownloaderTypes() 173 { 174 return array('git', 'svn', 'fossil', 'hg', 'perforce', 'zip', 'rar', 'tar', 'gzip', 'xz', 'phar', 'file', 'path'); 175 } 176} 177