1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of Railt package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace Railt\Reflection; |
11
|
|
|
|
12
|
|
|
use Railt\Io\Readable; |
13
|
|
|
use Railt\Reflection\Common\Serializable; |
14
|
|
|
use Railt\Reflection\Contracts\Definition; |
15
|
|
|
use Railt\Reflection\Contracts\Definition\TypeDefinition; |
16
|
|
|
use Railt\Reflection\Contracts\Document as DocumentInterface; |
17
|
|
|
use Railt\Reflection\Contracts\Type as TypeInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class AbstractDefinition |
21
|
|
|
*/ |
22
|
|
|
abstract class AbstractDefinition implements Definition |
23
|
|
|
{ |
24
|
|
|
use Serializable; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var Document |
28
|
|
|
*/ |
29
|
|
|
protected $document; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var int |
33
|
|
|
*/ |
34
|
|
|
protected $offset = 0; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* AbstractDefinition constructor. |
38
|
|
|
* @param Document $document |
39
|
|
|
*/ |
40
|
4 |
|
public function __construct(Document $document) |
41
|
|
|
{ |
42
|
4 |
|
$this->document = $document; |
43
|
4 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param int $offset |
47
|
|
|
*/ |
48
|
|
|
public function setOffset(int $offset): void |
49
|
|
|
{ |
50
|
|
|
$this->offset = $offset; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param TypeInterface $type |
55
|
|
|
* @return bool |
56
|
|
|
*/ |
57
|
|
|
public static function typeOf(TypeInterface $type): bool |
58
|
|
|
{ |
59
|
|
|
return static::getType()->instanceOf($type); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return DocumentInterface |
64
|
|
|
*/ |
65
|
2 |
|
public function getDocument(): DocumentInterface |
66
|
|
|
{ |
67
|
2 |
|
return $this->document; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return int |
72
|
|
|
*/ |
73
|
|
|
public function getLine(): int |
74
|
|
|
{ |
75
|
|
|
return $this->getFile()->getPosition($this->offset)->getLine(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return Readable |
80
|
|
|
*/ |
81
|
1 |
|
public function getFile(): Readable |
82
|
|
|
{ |
83
|
1 |
|
return $this->document->getFile(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return int |
88
|
|
|
*/ |
89
|
1 |
|
public function getColumn(): int |
90
|
|
|
{ |
91
|
1 |
|
return $this->getFile()->getPosition($this->offset)->getColumn(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
|
|
public function __toString(): string |
98
|
|
|
{ |
99
|
|
|
if (\method_exists($this, 'getName')) { |
100
|
|
|
return \sprintf('%s<%s>', $this->getName(), static::getType()); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return \sprintf('?<%s>', static::getType()); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param string $type |
108
|
|
|
* @return TypeDefinition |
109
|
|
|
*/ |
110
|
|
|
protected function fetch(string $type): TypeDefinition |
111
|
|
|
{ |
112
|
|
|
return $this->document->getReflection()->get($type); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|