Skip to content

Commit c3fc47b

Browse files
committed
added ClassSerialize trait for toArray(), toString() method.
1 parent b333140 commit c3fc47b

File tree

7 files changed

+59
-0
lines changed

7 files changed

+59
-0
lines changed

src/ClassSerialize.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace JiraRestApi;
4+
5+
trait ClassSerialize
6+
{
7+
/**
8+
* class property to Array.
9+
*
10+
* @param array $ignoreProperties this properties to be excluded from array.
11+
*
12+
* @return array
13+
*/
14+
public function toArray($ignoreProperties = [])
15+
{
16+
$ar = (get_object_vars($this));
17+
foreach ($ar as $key => $value) {
18+
if (in_array($key, $ignoreProperties)) {
19+
unset($ar[$key]);
20+
}
21+
}
22+
23+
return $ar;
24+
}
25+
26+
/**
27+
* class property to String.
28+
*
29+
* @param array $ignoreProperties this properties to be excluded from String.
30+
*
31+
* @return string
32+
*/
33+
public function toString($ignoreProperties = [])
34+
{
35+
$ar = $this->toArray($ignoreProperties);
36+
37+
return json_encode($ar, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
38+
}
39+
}

src/Field/Field.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
namespace JiraRestApi\Field;
4+
use JiraRestApi\ClassSerialize;
45

56
/**
67
* Custom filed schema.
@@ -45,6 +46,8 @@ class Schema
4546
*/
4647
class Field implements \JsonSerializable
4748
{
49+
use ClassSerialize;
50+
4851
/**
4952
* only custom field.
5053
*/

src/Issue/IssueField.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22

33
namespace JiraRestApi\Issue;
44

5+
use JiraRestApi\ClassSerialize;
6+
57
class IssueField implements \JsonSerializable
68
{
9+
use ClassSerialize;
10+
711
public function __construct($updateIssue = false)
812
{
913
if ($updateIssue != true) {

src/Issue/PaginatedWorklog.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
<?php
22

33
namespace JiraRestApi\Issue;
4+
use JiraRestApi\ClassSerialize;
45

56
/**
67
* Class PaginatedWorklog.
78
*/
89
class PaginatedWorklog
910
{
11+
use ClassSerialize;
12+
1013
/**
1114
* @var int Start at position
1215
*/

src/Issue/TimeTracking.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@
77
*/
88

99
namespace JiraRestApi\Issue;
10+
use JiraRestApi\ClassSerialize;
1011

1112
/**
1213
* Class TimeTracking.
1314
*/
1415
class TimeTracking implements \JsonSerializable
1516
{
17+
use ClassSerialize;
18+
1619
/**
1720
* Original estimate.
1821
*

src/Issue/Worklog.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22

33
namespace JiraRestApi\Issue;
44

5+
use JiraRestApi\ClassSerialize;
56
use JiraRestApi\JiraException;
67

78
/**
89
* Class Worklog.
910
*/
1011
class Worklog
1112
{
13+
use ClassSerialize;
14+
1215
/**
1316
* @var int id of worklog
1417
*/

src/Project/Project.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22

33
namespace JiraRestApi\Project;
44

5+
use JiraRestApi\ClassSerialize;
6+
57
class Project
68
{
9+
use ClassSerialize;
10+
711
/**
812
* return only if Project query by key(not id).
913
*

0 commit comments

Comments
 (0)