Skip to content

Commit d131183

Browse files
author
Matt Humphrey
committed
Immutable models, model type hints. #61
1 parent 787bde9 commit d131183

21 files changed

+320
-58
lines changed

lib/Gitlab/Model/AbstractModel.php

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,32 +55,39 @@ public function api($api)
5555
* @param array $data
5656
* @return $this
5757
*/
58-
public function hydrate(array $data = array())
58+
protected function hydrate(array $data = array())
5959
{
6060
if (!empty($data)) {
61-
foreach ($data as $k => $v) {
62-
if (in_array($k, static::$properties)) {
63-
$this->$k = $v;
64-
}
61+
foreach ($data as $field => $value) {
62+
$this->setData($field, $value);
6563
}
6664
}
6765

6866
return $this;
6967
}
7068

7169
/**
72-
* @param string $property
70+
* @param string $field
7371
* @param mixed $value
72+
* @return $this
7473
*/
75-
public function __set($property, $value)
74+
protected function setData($field, $value)
7675
{
77-
if (!in_array($property, static::$properties)) {
78-
throw new RuntimeException(sprintf(
79-
'Property "%s" does not exist for %s object', $property, get_called_class()
80-
));
76+
if (in_array($field, static::$properties)) {
77+
$this->data[$field] = $value;
8178
}
8279

83-
$this->data[$property] = $value;
80+
return $this;
81+
}
82+
83+
/**
84+
* @param string $property
85+
* @param mixed $value
86+
* @throws RuntimeException
87+
*/
88+
public function __set($property, $value)
89+
{
90+
throw new RuntimeException('Model properties are immutable');
8491
}
8592

8693
/**
@@ -102,4 +109,13 @@ public function __get($property)
102109

103110
return null;
104111
}
112+
113+
/**
114+
* @param string $property
115+
* @return bool
116+
*/
117+
public function __isset($property)
118+
{
119+
return isset($this->data[$property]);
120+
}
105121
}

lib/Gitlab/Model/Branch.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33
use Gitlab\Client;
44
use Gitlab\Api\AbstractApi as Api;
55

6+
/**
7+
* Class Branch
8+
*
9+
* @property-read string $name
10+
* @property-read bool $protected
11+
* @property-read Commit $commit
12+
* @property-read Project $project
13+
*/
614
class Branch extends AbstractModel
715
{
816
/**
@@ -40,9 +48,8 @@ public static function fromArray(Client $client, Project $project, array $data)
4048
public function __construct(Project $project, $name = null, Client $client = null)
4149
{
4250
$this->setClient($client);
43-
44-
$this->project = $project;
45-
$this->name = $name;
51+
$this->setData('project', $project);
52+
$this->setData('name', $name);
4653
}
4754

4855
/**

lib/Gitlab/Model/Commit.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,24 @@
22

33
use Gitlab\Client;
44

5+
/**
6+
* Class Commit
7+
*
8+
* @property-read string $id
9+
* @property-read string $short_id
10+
* @property-read string $title
11+
* @property-read string $message
12+
* @property-read string $author_name
13+
* @property-read string $author_email
14+
* @property-read string $authored_date
15+
* @property-read string $committed_date
16+
* @property-read string $created_at
17+
* @property-read Commit[] $parents
18+
* @property-read Node[] $tree
19+
* @property-read User $committer
20+
* @property-read User $author
21+
* @property-read Project $project
22+
*/
523
class Commit extends AbstractModel
624
{
725
/**
@@ -62,8 +80,7 @@ public static function fromArray(Client $client, Project $project, array $data)
6280
public function __construct(Project $project, $id = null, Client $client = null)
6381
{
6482
$this->setClient($client);
65-
66-
$this->project = $project;
67-
$this->id = $id;
83+
$this->setData('project', $project);
84+
$this->setData('id', $id);
6885
}
6986
}

lib/Gitlab/Model/CommitNote.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22

33
use Gitlab\Client;
44

5+
/**
6+
* Class CommitNote
7+
*
8+
* @property-read string $note
9+
* @property-read string $path
10+
* @property-read string $line
11+
* @property-read string $line_type
12+
* @property-read User $author
13+
*/
514
class CommitNote extends AbstractModel
615
{
716
/**

lib/Gitlab/Model/Event.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,21 @@
22

33
use Gitlab\Client;
44

5+
/**
6+
* Class Event
7+
*
8+
* @property-read string $title
9+
* @property-read int $id
10+
* @property-read string $action_name
11+
* @property-read string $data
12+
* @property-read int $target_id
13+
* @property-read string $target_type
14+
* @property-read string $target_title
15+
* @property-read int $author_id
16+
* @property-read string $author_username
17+
* @property-read User $author
18+
* @property-read Project $project
19+
*/
520
class Event extends AbstractModel
621
{
722
/**
@@ -45,7 +60,6 @@ public static function fromArray(Client $client, Project $project, array $data)
4560
public function __construct(Project $project, Client $client = null)
4661
{
4762
$this->setClient($client);
48-
49-
$this->project = $project;
63+
$this->setData('project', $project);
5064
}
5165
}

lib/Gitlab/Model/File.php

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

33
use Gitlab\Client;
44

5+
/**
6+
* Class File
7+
*
8+
* @property-read string $file_path
9+
* @property-read string $branch_name
10+
* @property-read Project $project
11+
*/
512
class File extends AbstractModel
613
{
714
/**
@@ -34,8 +41,7 @@ public static function fromArray(Client $client, Project $project, array $data)
3441
public function __construct(Project $project, $file_path = null, Client $client = null)
3542
{
3643
$this->setClient($client);
37-
38-
$this->project = $project;
39-
$this->file_path = $file_path;
44+
$this->setData('project', $project);
45+
$this->setData('file_path', $file_path);
4046
}
4147
}

lib/Gitlab/Model/Group.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22

33
use Gitlab\Client;
44

5+
/**
6+
* Class Group
7+
*
8+
* @property-read int $id
9+
* @property-read string $name
10+
* @property-read string $path
11+
* @property-read int $owner_id
12+
* @property-read Project[]
13+
*/
514
class Group extends AbstractModel
615
{
716
/**
@@ -55,8 +64,7 @@ public static function create(Client $client, $name, $path)
5564
public function __construct($id, Client $client = null)
5665
{
5766
$this->setClient($client);
58-
59-
$this->id = $id;
67+
$this->setData('id', $id);
6068
}
6169

6270
/**

lib/Gitlab/Model/Hook.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
use Gitlab\Client;
44

5+
/**
6+
* Class Hook
7+
*
8+
* @property-read int $id
9+
* @property-read string $url
10+
* @property-read string $created_at
11+
*/
512
class Hook extends AbstractModel
613
{
714
/**
@@ -44,8 +51,7 @@ public static function create(Client $client, $url)
4451
public function __construct($id, Client $client = null)
4552
{
4653
$this->setClient($client);
47-
48-
$this->id = $id;
54+
$this->setData('id', $id);
4955
}
5056

5157
/**

lib/Gitlab/Model/Issue.php

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,24 @@
22

33
use Gitlab\Client;
44

5+
/**
6+
* Class Issue
7+
*
8+
* @property-read int $id
9+
* @property-read int $iid
10+
* @property-read int $project_id,
11+
* @property-read string $title
12+
* @property-read string $description
13+
* @property-read array $labels
14+
* @property-read User $assignee
15+
* @property-read User $author
16+
* @property-read bool $closed
17+
* @property-read string $updated_at
18+
* @property-read string $created_at
19+
* @property-read string $state
20+
* @property-read Milestone $milestone
21+
* @property-read Project $project
22+
*/
523
class Issue extends AbstractModel
624
{
725
/**
@@ -21,8 +39,7 @@ class Issue extends AbstractModel
2139
'updated_at',
2240
'created_at',
2341
'project',
24-
'state',
25-
'labels'
42+
'state'
2643
);
2744

2845
/**
@@ -54,9 +71,8 @@ public static function fromArray(Client $client, Project $project, array $data)
5471
public function __construct(Project $project, $id = null, Client $client = null)
5572
{
5673
$this->setClient($client);
57-
58-
$this->project = $project;
59-
$this->id = $id;
74+
$this->setData('project', $project);
75+
$this->setData('id', $id);
6076
}
6177

6278
/**

lib/Gitlab/Model/Key.php

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

33
use Gitlab\Client;
44

5+
/**
6+
* Class Key
7+
*
8+
* @property-read int $id
9+
* @property-read string $title
10+
* @property-read string $key
11+
* @property-read string $created_at
12+
*/
513
class Key extends AbstractModel
614
{
715
/**

0 commit comments

Comments
 (0)