FOSSology  4.4.0
Open Source License Compliance by Open Source Software
Hash.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2020 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
11 namespace Fossology\UI\Api\Models;
12 
17 class Hash
18 {
19 
24  const ALLOWED_KEYS = ['sha1', 'sha256', 'md5', 'size'];
25 
30  private $sha1;
31 
36  private $sha256;
37 
42  private $md5;
43 
48  private $size;
49 
58  public function __construct($sha1 = null, $md5 = null, $sha256 = null,
59  $size = null)
60  {
61  $this->sha1 = $sha1;
62  $this->md5 = $md5;
63  $this->sha256 = $sha256;
64  if ($size === null) {
65  $this->size = $size;
66  } else {
67  $this->size = intval($size);
68  }
69  }
70 
74  public function getSha1()
75  {
76  return $this->sha1;
77  }
78 
82  public function getSha256()
83  {
84  return $this->sha256;
85  }
86 
90  public function getMd5()
91  {
92  return $this->md5;
93  }
94 
98  public function getSize()
99  {
100  return $this->size;
101  }
102 
108  public function getArray()
109  {
110  return [
111  'sha1' => $this->getSha1(),
112  'md5' => $this->getMd5(),
113  'sha256' => $this->getSha256(),
114  'size' => $this->getSize()
115  ];
116  }
117 
125  public static function createFromArray($inputArray)
126  {
127  $sha1 = null;
128  $md5 = null;
129  $sha256 = null;
130  $size = null;
131  $inputKeys = array_keys($inputArray);
132  $intersectKeys = array_intersect($inputKeys, self::ALLOWED_KEYS);
133  if (count($inputKeys) > 0 && count($intersectKeys) != count($inputKeys)) {
134  return null;
135  }
136  if (array_key_exists('sha1', $inputArray)) {
137  $sha1 = $inputArray['sha1'];
138  }
139  if (array_key_exists('md5', $inputArray)) {
140  $md5 = $inputArray['md5'];
141  }
142  if (array_key_exists('sha256', $inputArray)) {
143  $sha256 = $inputArray['sha256'];
144  }
145  if (array_key_exists('size', $inputArray)) {
146  $size = $inputArray['size'];
147  }
148  return new Hash($sha1, $md5, $sha256, $size);
149  }
150 }
Hash model holding information about file like checksums and size.
Definition: Hash.php:18
static createFromArray($inputArray)
Definition: Hash.php:125
__construct($sha1=null, $md5=null, $sha256=null, $size=null)
Definition: Hash.php:58