FOSSology  4.4.0
Open Source License Compliance by Open Source Software
LicenseRef.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2014 Siemens AG
4  Author: Johannes Najjar
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 
9 namespace Fossology\Lib\Data;
10 
13 
15 {
17  private $id;
18 
20  private $shortName;
21 
23  private $fullName;
24 
26  private $spdxId;
27 
32  const SPDXREF_PREFIX = "LicenseRef-";
37  const SPDXREF_PREFIX_FOSSOLOGY = "LicenseRef-fossology-";
38 
45  function __construct($licenseId, $licenseShortName, $licenseName, $spdxId)
46  {
47  $this->id = $licenseId;
48  $this->shortName = $licenseShortName;
49  $this->fullName = $licenseName ? : $licenseShortName;
50  $this->spdxId = self::convertToSpdxId($this->shortName, $spdxId);
51  }
52 
56  public function getId()
57  {
58  return $this->id;
59  }
60 
64  public function getFullName()
65  {
66  return $this->fullName;
67  }
68 
72  public function getShortName()
73  {
74  return $this->shortName;
75  }
76 
80  public function getSpdxId()
81  {
82  return $this->spdxId;
83  }
84 
85  public function __toString()
86  {
87  return 'LicenseRef('
88  .$this->id
89  .", ".$this->spdxId
90  .", ".$this->shortName
91  .", ".$this->fullName
92  .')';
93  }
94 
106  public static function convertToSpdxId($shortname, $spdxId): string
107  {
108  if (strcasecmp($shortname, LicenseDao::NO_LICENSE_FOUND) === 0 ||
109  strcasecmp($shortname, LicenseDao::VOID_LICENSE) === 0) {
110  $spdxLicense = $shortname;
111  } elseif (empty($spdxId)) {
112  $spdxLicense = $shortname;
113  if (! StringOperation::stringStartsWith($shortname, self::SPDXREF_PREFIX)) {
114  $spdxLicense = self::SPDXREF_PREFIX_FOSSOLOGY . $shortname;
115  }
116  } else {
117  $spdxLicense = $spdxId;
118  }
119  if (StringOperation::stringStartsWith($spdxLicense, self::SPDXREF_PREFIX)) {
120  // License ref can not end with a '+'
121  $spdxLicense = preg_replace('/\+$/', '-or-later', $spdxLicense);
122  }
123  return self::replaceSpaces($spdxLicense);
124  }
125 
132  public static function replaceSpaces($licenseName): string
133  {
134  $licenseName = str_replace(' ', '-', $licenseName);
135  return preg_replace('/-(OR|AND|WITH)-(?!later)/i', ' $1 ', $licenseName);
136  }
137 }
static convertToSpdxId($shortname, $spdxId)
Given a license's shortname and spdx id, give out spdx id to use in reports.
Definition: LicenseRef.php:106
static replaceSpaces($licenseName)
Definition: LicenseRef.php:132
__construct($licenseId, $licenseShortName, $licenseName, $spdxId)
Definition: LicenseRef.php:45
static stringStartsWith($haystack, $needle)