Repository navigation
Expand file tree
/
Copy pathRepo.php
More file actions
82 lines (63 loc) · 2.22 KB
/
Copy pathRepo.php
File metadata and controls
82 lines (63 loc) · 2.22 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
78
79
80
81
82
<?php
namespace JordanPartridge\GithubClient\ValueObjects;
use InvalidArgumentException;
readonly class Repo
{
private function __construct(
private string $owner,
private string $name,
) {}
public static function fromFullName(string $full_name): self
{
[$owner, $name] = self::validateAndParseRepoName($full_name);
return new self($owner, $name);
}
public static function fromOwnerAndRepo(string $owner, string $repo): self
{
if (empty($owner)) {
throw new InvalidArgumentException('Owner cannot be empty.');
}
if (empty($repo)) {
throw new InvalidArgumentException('Repository name cannot be empty.');
}
// Validate owner and repo names using same regex as validateAndParseRepoName
if (! preg_match('/^[a-zA-Z0-9._-]+$/', $owner)) {
throw new InvalidArgumentException("Invalid characters in owner name '{$owner}'.");
}
if (! preg_match('/^[a-zA-Z0-9._-]+$/', $repo)) {
throw new InvalidArgumentException("Invalid characters in repository name '{$repo}'.");
}
return new self($owner, $repo);
}
private static function validateAndParseRepoName(string $full_name): array
{
$parts = explode('/', $full_name);
if (count($parts) !== 2) {
throw new InvalidArgumentException('Repository must be in format "owner/repo".');
}
[$owner, $name] = $parts;
if (empty($owner) || empty($name)) {
throw new InvalidArgumentException('Owner and repo name cannot be empty.');
}
if (! preg_match('/^[a-zA-Z0-9._-]+$/', $owner) || ! preg_match('/^[a-zA-Z0-9._-]+$/', $name)) {
throw new InvalidArgumentException("Invalid characters in repository name '{$full_name}'.");
}
return [$owner, $name];
}
public static function fromRepo(Repo $repo): self
{
return new self($repo->owner, $repo->name);
}
public function owner(): string
{
return $this->owner;
}
public function name(): string
{
return $this->name;
}
public function fullName(): string
{
return "{$this->owner}/{$this->name}";
}
}