FOSSology  4.7.1
Open Source License Compliance by Open Source Software
CycloneDxSettings.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2026 Siemens AG
4  Author: Shaheem Azmal M MD <shaheem.azmal@gmail.com>
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 
9 namespace Fossology\Lib\Data\Report;
10 
20 final class CycloneDxSettings
21 {
22  const SUPPORTED_VERSIONS = ['1.4', '1.5', '1.6', '1.7'];
23  const DEFAULT_VERSION = '1.7';
24  const DEFAULT_NAMESPACE = 'fossology:';
25  const MAX_NAMESPACE_LENGTH = 64;
27  const NAMESPACE_PATTERN = '/^[A-Za-z0-9._-]+(:[A-Za-z0-9._-]+)*:$/';
28 
30  private $licenseComment;
32  private $ignoreFilesWithoutInfo;
34  private $osselotExport;
36  private $tagNamespace;
38  private $specVersion;
39 
43  public function __construct(bool $licenseComment = false,
44  bool $ignoreFilesWithoutInfo = false, bool $osselotExport = false,
45  ?string $tagNamespace = null, ?string $specVersion = null)
46  {
47  $this->licenseComment = $licenseComment;
48  $this->ignoreFilesWithoutInfo = $ignoreFilesWithoutInfo;
49  $this->osselotExport = $osselotExport;
50  $this->tagNamespace = self::normaliseNamespace($tagNamespace) ?? self::DEFAULT_NAMESPACE;
51  $this->specVersion = self::normaliseSpecVersion($specVersion) ?? self::DEFAULT_VERSION;
52  }
53 
58  public static function normaliseNamespace(?string $namespace): ?string
59  {
60  $namespace = trim((string) $namespace);
61  if ($namespace === '') {
62  return null;
63  }
64  if (substr($namespace, -1) !== ':') {
65  $namespace .= ':';
66  }
67  if (strlen($namespace) > self::MAX_NAMESPACE_LENGTH ||
68  !preg_match(self::NAMESPACE_PATTERN, $namespace)) {
69  return null;
70  }
71  return $namespace;
72  }
73 
78  public static function normaliseSpecVersion(?string $version): ?string
79  {
80  return in_array($version, self::SUPPORTED_VERSIONS, true) ? $version : null;
81  }
82 
87  public static function fromUserSettings(?string $csv): self
88  {
89  $fields = array_pad(explode(',', (string) $csv), 5, null);
90  return new self($fields[1] === 'checked', $fields[2] === 'checked',
91  $fields[0] === 'checked', $fields[3], $fields[4]);
92  }
93 
97  public function toUserSettings(): string
98  {
99  return implode(',', [self::flag($this->osselotExport),
100  self::flag($this->licenseComment), self::flag($this->ignoreFilesWithoutInfo),
101  $this->tagNamespace, $this->specVersion]);
102  }
103 
108  public static function fromReportInfo(?string $json): ?self
109  {
110  $data = json_decode((string) $json, true);
111  if (!is_array($data)) {
112  return null;
113  }
114  return new self(!empty($data['licenseComment']),
115  !empty($data['ignoreFilesWithoutInfo']), !empty($data['osselotExport']),
116  isset($data['tagNamespace']) ? (string) $data['tagNamespace'] : null,
117  isset($data['specVersion']) ? (string) $data['specVersion'] : null);
118  }
119 
123  public function toReportInfo(): string
124  {
125  return json_encode([
126  'licenseComment' => $this->licenseComment,
127  'ignoreFilesWithoutInfo' => $this->ignoreFilesWithoutInfo,
128  'osselotExport' => $this->osselotExport,
129  'tagNamespace' => $this->tagNamespace,
130  'specVersion' => $this->specVersion
131  ]);
132  }
133 
138  public function isLicenseCommentEnabled(): bool
139  {
140  return $this->licenseComment || $this->osselotExport;
141  }
142 
146  public function isLicenseCommentChecked(): bool
147  {
148  return $this->licenseComment;
149  }
150 
151  public function isIgnoreFilesWithoutInfo(): bool
152  {
153  return $this->ignoreFilesWithoutInfo;
154  }
155 
156  public function isOsselotExport(): bool
157  {
158  return $this->osselotExport;
159  }
160 
161  public function getTagNamespace(): string
162  {
163  return $this->tagNamespace;
164  }
165 
166  public function getSpecVersion(): string
167  {
168  return $this->specVersion;
169  }
170 
171  private static function flag(bool $value): string
172  {
173  return $value ? 'checked' : 'unchecked';
174  }
175 }
CycloneDX export settings of a user (defaults) or of an upload.
__construct(bool $licenseComment=false, bool $ignoreFilesWithoutInfo=false, bool $osselotExport=false, ?string $tagNamespace=null, ?string $specVersion=null)
char * trim(char *ptext)
Trimming whitespace.
Definition: fossconfig.c:690