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