Repository navigation
Expand file tree
/
Copy pathInterfaceType.php
More file actions
94 lines (76 loc) 路 2.38 KB
/
Copy pathInterfaceType.php
File metadata and controls
94 lines (76 loc) 路 2.38 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
83
84
85
86
87
88
89
90
91
92
93
94
<?php declare(strict_types=1);
/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/
namespace Nette\PhpGenerator;
use Nette;
/**
* Definition of an interface with properties, methods and constants.
*/
final class InterfaceType extends ClassLike
{
use Traits\ConstantsAware;
use Traits\MethodsAware;
use Traits\PropertiesAware;
/** @var list<string> */
private array $extends = [];
/** @param string|list<string> $names */
public function setExtends(string|array $names): static
{
$names = (array) $names;
$this->validateNames($names);
$this->extends = $names;
return $this;
}
/** @return list<string> */
public function getExtends(): array
{
return $this->extends;
}
/**
* Adds a parent interface.
*/
public function addExtend(string $name): static
{
$this->validateNames([$name]);
$this->extends[] = $name;
return $this;
}
/**
* Adds a member to the interface.
* @throws Nette\InvalidStateException if the member already exists and $overwrite is false
*/
public function addMember(Method|Constant|Property $member, bool $overwrite = false): static
{
$name = $member->getName();
[$type, $n] = match (true) {
$member instanceof Constant => ['consts', $name],
$member instanceof Method => ['methods', strtolower($name)],
$member instanceof Property => ['properties', $name],
};
if (!$overwrite && isset($this->$type[$n])) {
throw new Nette\InvalidStateException("Cannot add member '$name', because it already exists.");
}
$this->$type[$n] = $member;
return $this;
}
/** @throws Nette\InvalidStateException */
public function validate(): void
{
foreach ($this->getProperties() as $property) {
if ($property->isInitialized()) {
throw new Nette\InvalidStateException("Property {$this->getName()}::\${$property->getName()}: Interface cannot have initialized properties.");
} elseif (!$property->getHooks()) {
throw new Nette\InvalidStateException("Property {$this->getName()}::\${$property->getName()}: Interface cannot have properties without hooks.");
}
}
}
public function __clone(): void
{
parent::__clone();
$this->consts = array_map(fn(Constant $c) => clone $c, $this->consts);
$this->methods = array_map(fn(Method $m) => clone $m, $this->methods);
$this->properties = array_map(fn(Property $p) => clone $p, $this->properties);
}
}