-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathClassSerialize.php
More file actions
49 lines (43 loc) · 1.29 KB
/
ClassSerialize.php
File metadata and controls
49 lines (43 loc) · 1.29 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
<?php
namespace JiraCloud;
trait ClassSerialize
{
/**
* class property to Array.
*
* @param array $ignoreProperties this properties to be (ex|in)cluded from array whether second param is true or false.
* @param bool $excludeMode
*
* @return array
*/
public function toArray(array $ignoreProperties = [], bool $excludeMode = true): array
{
$tmp = get_object_vars($this);
$retAr = null;
foreach ($tmp as $key => $value) {
if ($excludeMode === true) {
if (!in_array($key, $ignoreProperties)) {
$retAr[$key] = $value;
}
} else { // include mode
if (in_array($key, $ignoreProperties)) {
$retAr[$key] = $value;
}
}
}
return $retAr;
}
/**
* class property to String.
*
* @param array $ignoreProperties this properties to be excluded from String.
* @param bool $excludeMode
*
* @return string
*/
public function toString(array $ignoreProperties = [], bool $excludeMode = true): string
{
$ar = $this->toArray($ignoreProperties, $excludeMode);
return json_encode($ar, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
}
}