-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsonValidate.php
More file actions
64 lines (56 loc) · 1.61 KB
/
jsonValidate.php
File metadata and controls
64 lines (56 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
require_once "bitmap.php";
class Validator
{
public static function GetObjFromInput(string $JSON)
{
try
{
$JSON = htmlentities($JSON, ENT_NOQUOTES, "UTF-8");
$obj = json_decode($JSON);
if (!isset($obj->addrArr)) throw new Exception("Address array is not set");
else $obj->addrArr = Validator::AddrArr($obj);
if (!isset($obj->bitRate)) throw new Exception("Bitrate is not set");
else $obj->bitRate = Validator::BitRate($obj->bitRate);
if (!isset($obj->author)) $obj->author = "Anonymous";
if (!isset($obj->description)) $obj->description = "Nic";
$ip = Connector::getUserIP();
return new Bitmap($obj->addrArr, $obj->bitRate, $obj->rows, $obj->author, $obj->description, $ip);
}
catch (Exception $e)
{
return $e->getMessage();
}
}
static function AddrArr($obj)
{
$addrArr = $obj->addrArr;
if (is_countable($addrArr))
{
$obj->rows = count($addrArr);
if ($obj->rows < 1)
{
throw new Exception("Address array is empty");
}
else
{
return $addrArr;
}
}
else
{
throw new Exception("Address array is not array");
}
}
static function BitRate(int $bitRate)
{
if ($bitRate == 32 || $bitRate == 128)
{
return $bitRate;
}
else
{
throw new Exception("Bitrate is not valid");
}
}
}