FOSSology  4.5.1
Open Source License Compliance by Open Source Software
License.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2021 HH Partners
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
12 namespace Fossology\UI\Api\Models;
13 
19 class License
20 {
25  const ALLOWED_KEYS = ['shortName', 'fullName', 'text', 'url', 'risk',
26  'isCandidate', 'mergeRequest'];
31  private $id;
36  private $shortName;
41  private $fullName;
46  private $text;
51  private $url;
56  private $obligations;
61  private $risk;
66  private $isCandidate;
71  private $mergeRequest;
72 
85  public function __construct(
86  $id,
87  $shortName = "",
88  $fullName = "",
89  $text = "",
90  $url = "",
91  $obligations = null,
92  $risk = null,
93  $isCandidate = false
94  )
95  {
96  $this->id = intval($id);
97  $this->setShortName($shortName);
98  $this->setFullName($fullName);
99  $this->setText($text);
100  $this->setUrl($url);
102  $this->setRisk($risk);
104  $this->mergeRequest = false;
105  }
106 
111  public function getJSON()
112  {
113  return json_encode($this->getArray());
114  }
115 
120  public function getArray()
121  {
122  $data = [
123  'id' => $this->getId(),
124  'shortName' => $this->getShortName(),
125  'fullName' => $this->getFullName(),
126  'text' => $this->getText(),
127  'url' => $this->getUrl(),
128  'risk' => $this->getRisk(),
129  'isCandidate' => $this->getIsCandidate()
130  ];
131  if ($this->obligations !== null) {
132  $data['obligations'] = $this->getObligations();
133  }
134  return $data;
135  }
136 
141  public function getId()
142  {
143  return $this->id;
144  }
145 
150  public function getShortName()
151  {
152  if ($this->shortName === null) {
153  return "";
154  }
155  return $this->shortName;
156  }
157 
162  public function getFullName()
163  {
164  if ($this->fullName === null) {
165  return "";
166  }
167  return $this->fullName;
168  }
169 
174  public function getText()
175  {
176  if ($this->text === null) {
177  return "";
178  }
179  return $this->text;
180  }
181 
186  public function getUrl()
187  {
188  if ($this->url === null) {
189  return "";
190  }
191  return $this->url;
192  }
193 
198  public function getRisk()
199  {
200  return $this->risk;
201  }
202 
207  public function getIsCandidate()
208  {
209  return $this->isCandidate;
210  }
211 
216  public function getObligations()
217  {
218  if ($this->obligations === null) {
219  return null;
220  }
221 
222  $obligationList = [];
223  foreach ($this->obligations as $obligation) {
224  $obligationList[] = $obligation->getArray();
225  }
226  return $obligationList;
227  }
228 
233  public function getMergeRequest()
234  {
235  return $this->mergeRequest;
236  }
237 
242  public function setShortName($shortName)
243  {
244  $this->shortName = convertToUTF8($shortName, false);
245  }
246 
251  public function setFullName($fullName)
252  {
253  $this->fullName = convertToUTF8($fullName, false);
254  }
255 
260  public function setText($text)
261  {
262  $this->text = convertToUTF8($text, false);
263  }
264 
269  public function setUrl($url)
270  {
271  $this->url = convertToUTF8($url, false);
272  }
273 
278  public function setRisk($risk)
279  {
280  // invtval returns 0 for null, so check for nullness to preserve the
281  // difference in the response.
282  if (!is_null($risk)) {
283  $this->risk = intval($risk);
284  } else {
285  $this->risk = $risk;
286  }
287  }
288 
293  public function setIsCandidate($isCandidate)
294  {
295  $this->isCandidate = filter_var($isCandidate, FILTER_VALIDATE_BOOLEAN);
296  }
297 
302  public function setObligations($obligations)
303  {
304  if (is_array($obligations)) {
305  $this->obligations = [];
306  } elseif ($obligations === null) {
307  $this->obligations = null;
308  return;
309  }
310  foreach ($obligations as $obligation) {
311  $this->addObligation($obligation);
312  }
313  }
314 
319  public function addObligation($obligation)
320  {
321  if ($this->obligations === null) {
322  $this->obligations = [];
323  }
324  $this->obligations[] = $obligation;
325  }
326 
332  {
333  $this->mergeRequest = filter_var($mergeRequest, FILTER_VALIDATE_BOOLEAN);
334  }
335 
342  public static function parseFromArray($inputLicense)
343  {
344  $inputKeys = array_keys($inputLicense);
345  $intersectKeys = array_intersect($inputKeys, self::ALLOWED_KEYS);
346  if (count($inputKeys) > 0 && count($intersectKeys) != count($inputKeys)) {
347  return -1;
348  }
349  if (array_search('shortName', $inputKeys) === false) {
350  return -2;
351  }
352  $newLicense = new License(0);
353  if (array_key_exists('shortName', $inputLicense)) {
354  $newLicense->setShortName($inputLicense['shortName']);
355  }
356  if (array_key_exists('fullName', $inputLicense)) {
357  $newLicense->setFullName($inputLicense['fullName']);
358  }
359  if (array_key_exists('text', $inputLicense)) {
360  $newLicense->setText($inputLicense['text']);
361  }
362  if (array_key_exists('url', $inputLicense)) {
363  $newLicense->setUrl($inputLicense['url']);
364  }
365  if (array_key_exists('risk', $inputLicense)) {
366  $newLicense->setRisk($inputLicense['risk']);
367  }
368  if (array_key_exists('isCandidate', $inputLicense)) {
369  $newLicense->setIsCandidate($inputLicense['isCandidate']);
370  }
371  if (array_key_exists('mergeRequest', $inputLicense)) {
372  $newLicense->setMergeRequest($inputLicense['mergeRequest']);
373  }
374  return $newLicense;
375  }
376 }
setMergeRequest($mergeRequest)
Definition: License.php:331
static parseFromArray($inputLicense)
Definition: License.php:342
__construct( $id, $shortName="", $fullName="", $text="", $url="", $obligations=null, $risk=null, $isCandidate=false)
Definition: License.php:85
if(! defined('ENT_SUBSTITUTE')) convertToUTF8($content, $toHTML=true)