FOSSology  4.4.0
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  $obligationList = [];
219  foreach ($this->obligations as $obligation) {
220  $obligationList[] = $obligation->getArray();
221  }
222  return $obligationList;
223  }
224 
229  public function getMergeRequest()
230  {
231  return $this->mergeRequest;
232  }
233 
238  public function setShortName($shortName)
239  {
240  $this->shortName = convertToUTF8($shortName, false);
241  }
242 
247  public function setFullName($fullName)
248  {
249  $this->fullName = convertToUTF8($fullName, false);
250  }
251 
256  public function setText($text)
257  {
258  $this->text = convertToUTF8($text, false);
259  }
260 
265  public function setUrl($url)
266  {
267  $this->url = convertToUTF8($url, false);
268  }
269 
274  public function setRisk($risk)
275  {
276  // invtval returns 0 for null, so check for nullness to preserve the
277  // difference in the response.
278  if (!is_null($risk)) {
279  $this->risk = intval($risk);
280  } else {
281  $this->risk = $risk;
282  }
283  }
284 
289  public function setIsCandidate($isCandidate)
290  {
291  $this->isCandidate = filter_var($isCandidate, FILTER_VALIDATE_BOOLEAN);
292  }
293 
298  public function setObligations($obligations)
299  {
300  if (is_array($obligations)) {
301  $this->obligations = [];
302  } elseif ($obligations === null) {
303  $this->obligations = null;
304  return;
305  }
306  foreach ($obligations as $obligation) {
307  $this->addObligation($obligation);
308  }
309  }
310 
315  public function addObligation($obligation)
316  {
317  if ($this->obligations === null) {
318  $this->obligations = [];
319  }
320  $this->obligations[] = $obligation;
321  }
322 
328  {
329  $this->mergeRequest = filter_var($mergeRequest, FILTER_VALIDATE_BOOLEAN);
330  }
331 
338  public static function parseFromArray($inputLicense)
339  {
340  $inputKeys = array_keys($inputLicense);
341  $intersectKeys = array_intersect($inputKeys, self::ALLOWED_KEYS);
342  if (count($inputKeys) > 0 && count($intersectKeys) != count($inputKeys)) {
343  return -1;
344  }
345  if (array_search('shortName', $inputKeys) === false) {
346  return -2;
347  }
348  $newLicense = new License(0);
349  if (array_key_exists('shortName', $inputLicense)) {
350  $newLicense->setShortName($inputLicense['shortName']);
351  }
352  if (array_key_exists('fullName', $inputLicense)) {
353  $newLicense->setFullName($inputLicense['fullName']);
354  }
355  if (array_key_exists('text', $inputLicense)) {
356  $newLicense->setText($inputLicense['text']);
357  }
358  if (array_key_exists('url', $inputLicense)) {
359  $newLicense->setUrl($inputLicense['url']);
360  }
361  if (array_key_exists('risk', $inputLicense)) {
362  $newLicense->setRisk($inputLicense['risk']);
363  }
364  if (array_key_exists('isCandidate', $inputLicense)) {
365  $newLicense->setIsCandidate($inputLicense['isCandidate']);
366  }
367  if (array_key_exists('mergeRequest', $inputLicense)) {
368  $newLicense->setMergeRequest($inputLicense['mergeRequest']);
369  }
370  return $newLicense;
371  }
372 }
setMergeRequest($mergeRequest)
Definition: License.php:327
static parseFromArray($inputLicense)
Definition: License.php:338
__construct( $id, $shortName="", $fullName="", $text="", $url="", $obligations=null, $risk=null, $isCandidate=false)
Definition: License.php:85
if(! defined('ENT_SUBSTITUTE')) convertToUTF8($content, $toHTML=true)