forked from tan-tan-kanarek/github-php-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGitHubObject.php
More file actions
77 lines (67 loc) · 1.52 KB
/
GitHubObject.php
File metadata and controls
77 lines (67 loc) · 1.52 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
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
class GitHubObject
{
/**
* @param array $json
*/
public static function fromArray(array $json, $type)
{
$array = array();
foreach($json as $jsonObject)
$array[] = new $type($jsonObject);
return $array;
}
/**
* @param stdClass $json
*/
public function __construct(stdClass $json)
{
$attributes = $this->getAttributes();
foreach($attributes as $attributeName => $attributeType)
{
if(!isset($json->$attributeName))
continue;
switch ($attributeType)
{
case 'string':
$this->$attributeName = $json->$attributeName;
break;
case 'int':
$this->$attributeName = intval($json->$attributeName);
break;
case 'boolean':
$this->$attributeName = (bool)$json->$attributeName;
break;
default:
$matches = null;
if(preg_match('/^array<([^>]+)>$/', $attributeType, $matches))
{
$attributeType = $matches[1];
$array = array();
if(is_array($json->$attributeName))
{
foreach($json->$attributeName as $value)
{
$array[] = new $attributeType($value);
}
}
$this->$attributeName = $array;
}
else
{
if(!class_exists($attributeType))
throw new GitHubClientException("Github type [$attributeType] not found", GitHubClientException::CLASS_NOT_FOUND);
$this->$attributeName = new $attributeType($json->$attributeName);
}
break;
}
}
}
/**
* @return array
*/
protected function getAttributes()
{
return array();
}
}