FOSSology  4.5.1
Open Source License Compliance by Open Source Software
spdxutils.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2016 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
8 namespace Fossology\Spdx;
9 
12 
17 class SpdxUtils
18 {
29  static public function preWorkOnArgsFlp($args,$key1,$key2)
30  {
31  $needle = ' --'.$key2.'=';
32  if (is_array($args) &&
33  array_key_exists($key1, $args) &&
34  strpos($args[$key1],$needle) !== false) {
35  $exploded = explode($needle,$args[$key1]);
36  $args[$key1] = trim($exploded[0]);
37  $args[$key2] = trim($exploded[1]);
38  }
39  return $args;
40  }
41 
47  static public function addPrefixOnDemand($license)
48  {
49  if (empty($license) || $license === "NOASSERTION") {
50  return "NOASSERTION";
51  }
52 
53  if (strpos($license, " OR ") !== false) {
54  return "(" . $license . ")";
55  }
56 
57  $license = preg_replace('/[^a-zA-Z0-9\-\_\.\+]/','-',$license);
58  if (strpos($license, LicenseRef::SPDXREF_PREFIX) !== false) {
59  // License ref can not end with a '+'
60  $license = preg_replace('/\+$/', '-or-later', $license);
61  }
62  return preg_replace('/\+(?!$)/','-',$license);
63  }
64 
70  static public function addPrefixOnDemandKeys($licenses)
71  {
72  $ret = array();
73  foreach ($licenses as $license=>$text) {
74  $ret[self::addPrefixOnDemand($license)] = $text;
75  }
76  return $ret;
77  }
78 
84  static public function addPrefixOnDemandList($licenses)
85  {
86  return array_map(function ($license)
87  {
88  return SpdxUtils::addPrefixOnDemand($license);
89  },$licenses);
90  }
91 
97  static public function implodeLicenses($licenses)
98  {
99  if (!$licenses || !is_array($licenses) || sizeof($licenses) == 0) {
100  return "";
101  }
102 
103  $licenses = self::addPrefixOnDemandList($licenses);
104  sort($licenses, SORT_NATURAL | SORT_FLAG_CASE);
105 
106  if (count($licenses) == 3) {
107  foreach ($licenses as $index => $lic) {
108  if (StringOperation::stringStartsWith($lic, "Dual-license") ||
109  StringOperation::stringStartsWith($lic, LicenseRef::SPDXREF_PREFIX . "Dual-license") ||
110  StringOperation::stringStartsWith($lic, LicenseRef::SPDXREF_PREFIX_FOSSOLOGY . "Dual-license")) {
111  return $licenses[$index===0?1:0] . " OR " . $licenses[$index===2?1:2];
112  }
113  }
114  }
115  return implode(" AND ", $licenses);
116  }
117 
124  static public function cleanTextArray($texts): array
125  {
126  if (!$texts || !is_array($texts) || sizeof($texts) == 0) {
127  return [];
128  }
129 
130  sort($texts, SORT_NATURAL | SORT_FLAG_CASE);
131 
132  $cleanArray = [];
133  foreach ($texts as $text) {
134  $text = trim($text);
135  if (empty($text)) {
136  continue;
137  }
138  $cleanArray[] = $text;
139  }
140  return $cleanArray;
141  }
142 
148  public static function removeEmptyLicenses($licenses): array
149  {
150  $newList = [];
151  foreach ($licenses as $license) {
152  if (empty($license) || $license === "NOASSERTION") {
153  continue;
154  }
155  $newList[] = $license;
156  }
157  return $newList;
158  }
159 }
static stringStartsWith($haystack, $needle)
Utilities for SPDX2.
Definition: spdxutils.php:18
static preWorkOnArgsFlp($args, $key1, $key2)
For a given set of arguments assign $args[$key1] and $args[$key2].
Definition: spdxutils.php:29
static addPrefixOnDemandList($licenses)
Add prefix to license list.
Definition: spdxutils.php:84
static implodeLicenses($licenses)
Implode licenses with "AND" or "OR".
Definition: spdxutils.php:97
static addPrefixOnDemandKeys($licenses)
Add prefix to license keys.
Definition: spdxutils.php:70
static addPrefixOnDemand($license)
Add prefix to the license based on SPDX2 standards.
Definition: spdxutils.php:47
static removeEmptyLicenses($licenses)
Definition: spdxutils.php:148
static cleanTextArray($texts)
Definition: spdxutils.php:124
char * trim(char *ptext)
Trimming whitespace.
Definition: fossconfig.c:690
Namespace used by SPDX2 agent.