Skip to content

Commit

Permalink
added ClassSerialize trait for toArray(), toString() method.
Browse files Browse the repository at this point in the history
  • Loading branch information
lesstif committed Jul 2, 2016
1 parent b333140 commit c3fc47b
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/ClassSerialize.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace JiraRestApi;

trait ClassSerialize
{
/**
* class property to Array.
*
* @param array $ignoreProperties this properties to be excluded from array.
*
* @return array
*/
public function toArray($ignoreProperties = [])
{
$ar = (get_object_vars($this));
foreach ($ar as $key => $value) {
if (in_array($key, $ignoreProperties)) {
unset($ar[$key]);
}
}

return $ar;
}

/**
* class property to String.
*
* @param array $ignoreProperties this properties to be excluded from String.
*
* @return string
*/
public function toString($ignoreProperties = [])
{
$ar = $this->toArray($ignoreProperties);

return json_encode($ar, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
}
}
3 changes: 3 additions & 0 deletions src/Field/Field.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

namespace JiraRestApi\Field;
use JiraRestApi\ClassSerialize;

/**
* Custom filed schema.
Expand Down Expand Up @@ -45,6 +46,8 @@ class Schema
*/
class Field implements \JsonSerializable
{
use ClassSerialize;

/**
* only custom field.
*/
Expand Down
4 changes: 4 additions & 0 deletions src/Issue/IssueField.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

namespace JiraRestApi\Issue;

use JiraRestApi\ClassSerialize;

class IssueField implements \JsonSerializable
{
use ClassSerialize;

public function __construct($updateIssue = false)
{
if ($updateIssue != true) {
Expand Down
3 changes: 3 additions & 0 deletions src/Issue/PaginatedWorklog.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
<?php

namespace JiraRestApi\Issue;
use JiraRestApi\ClassSerialize;

/**
* Class PaginatedWorklog.
*/
class PaginatedWorklog
{
use ClassSerialize;

/**
* @var int Start at position
*/
Expand Down
3 changes: 3 additions & 0 deletions src/Issue/TimeTracking.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
*/

namespace JiraRestApi\Issue;
use JiraRestApi\ClassSerialize;

/**
* Class TimeTracking.
*/
class TimeTracking implements \JsonSerializable
{
use ClassSerialize;

/**
* Original estimate.
*
Expand Down
3 changes: 3 additions & 0 deletions src/Issue/Worklog.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

namespace JiraRestApi\Issue;

use JiraRestApi\ClassSerialize;
use JiraRestApi\JiraException;

/**
* Class Worklog.
*/
class Worklog
{
use ClassSerialize;

/**
* @var int id of worklog
*/
Expand Down
4 changes: 4 additions & 0 deletions src/Project/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

namespace JiraRestApi\Project;

use JiraRestApi\ClassSerialize;

class Project
{
use ClassSerialize;

/**
* return only if Project query by key(not id).
*
Expand Down

0 comments on commit c3fc47b

Please sign in to comment.