FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
spdx2utils.php
1 <?php
2 /*
3  * Copyright (C) 2016, Siemens AG
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * version 2 as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18 
19 namespace Fossology\SpdxTwo;
20 
26 {
27  static public $prefix = "LicenseRef-";
28 
39  static public function preWorkOnArgsFlp($args,$key1,$key2)
40  {
41  $needle = ' --'.$key2.'=';
42  if (is_array($args) &&
43  array_key_exists($key1, $args) &&
44  strpos($args[$key1],$needle) !== false) {
45  $exploded = explode($needle,$args[$key1]);
46  $args[$key1] = trim($exploded[0]);
47  $args[$key2] = trim($exploded[1]);
48  }
49  return $args;
50  }
51 
58  static public function addPrefixOnDemand($license, $spdxValidityChecker = null)
59  {
60  if (empty($license) || $license === "NOASSERTION") {
61  return "NOASSERTION";
62  }
63 
64  if (strpos($license, " OR ") !== false) {
65  return "(" . $license . ")";
66  }
67 
68  if ($spdxValidityChecker === null ||
69  (is_callable($spdxValidityChecker) &&
70  call_user_func($spdxValidityChecker, $license))) {
71  return $license;
72  }
73 
74  // if we get here, we're using a non-standard SPDX license
75  // make sure our license text conforms to the SPDX specifications
76  $license = preg_replace('/[^a-zA-Z0-9\-\_\.\+]/','-',$license);
77  $license = preg_replace('/\+(?!$)/','-',$license);
78  return self::$prefix . $license;
79  }
80 
87  static public function addPrefixOnDemandKeys($licenses, $spdxValidityChecker = null)
88  {
89  $ret = array();
90  foreach ($licenses as $license=>$text) {
91  $ret[self::addPrefixOnDemand($license, $spdxValidityChecker)] = $text;
92  }
93  return $ret;
94  }
95 
102  static public function addPrefixOnDemandList($licenses, $spdxValidityChecker = null)
103  {
104  return array_map(function ($license) use ($spdxValidityChecker)
105  {
106  return SpdxTwoUtils::addPrefixOnDemand($license, $spdxValidityChecker);
107  },$licenses);
108  }
109 
116  static public function implodeLicenses($licenses, $spdxValidityChecker = null)
117  {
118  if (!$licenses || !is_array($licenses) || sizeof($licenses) == 0) {
119  return "";
120  }
121 
122  $licenses = self::addPrefixOnDemandList($licenses, $spdxValidityChecker);
123  sort($licenses, SORT_NATURAL | SORT_FLAG_CASE);
124 
125  if (count($licenses) == 3 &&
126  ($index = array_search("Dual-license",$licenses)) !== false) {
127  return $licenses[$index===0?1:0] . " OR " . $licenses[$index===2?1:2];
128  } elseif (count($licenses) == 3 &&
129  ($index = array_search(self::$prefix . "Dual-license",$licenses)) !== false) {
130  return $licenses[$index===0?1:0] . " OR " . $licenses[$index===2?1:2];
131  } else {
132  // Add prefixes where needed, enclose statments containing ' OR ' with parantheses
133  return implode(" AND ", $licenses);
134  }
135  }
136 }
static preWorkOnArgsFlp($args, $key1, $key2)
For a given set of arguments assign $args[$key1] and $args[$key2].
Definition: spdx2utils.php:39
static addPrefixOnDemand($license, $spdxValidityChecker=null)
Add prefix to the license based on SPDX2 standards.
Definition: spdx2utils.php:58
Namespace used by SPDX2 agent.
static addPrefixOnDemandKeys($licenses, $spdxValidityChecker=null)
Add prefix to license keys.
Definition: spdx2utils.php:87
static addPrefixOnDemandList($licenses, $spdxValidityChecker=null)
Add prefix to license list.
Definition: spdx2utils.php:102
Utilities for SPDX2.
Definition: spdx2utils.php:25
static $prefix
Prefix to be used.
Definition: spdx2utils.php:27
static implodeLicenses($licenses, $spdxValidityChecker=null)
Implode licenses with "AND" or "OR".
Definition: spdx2utils.php:116
char * trim(char *ptext)
Trimming whitespace.
Definition: fossconfig.c:695