|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Spatie\DataTransferObject; |
|
6
|
|
|
|
|
7
|
|
|
use ReflectionProperty; |
|
8
|
|
|
use ReflectionClass as BaseReflectionClass; |
|
9
|
|
|
|
|
10
|
|
|
class DataTransferObjectDefinition extends BaseReflectionClass |
|
11
|
|
|
{ |
|
12
|
|
|
/** @var array */ |
|
13
|
|
|
protected $uses = []; |
|
14
|
|
|
|
|
15
|
|
|
/** @var \Spatie\DataTransferObject\DataTransferObject */ |
|
16
|
|
|
protected $dataTransferObject; |
|
17
|
|
|
|
|
18
|
|
|
public function __construct(DataTransferObject $dataTransferObject) |
|
19
|
|
|
{ |
|
20
|
|
|
parent::__construct($dataTransferObject); |
|
21
|
|
|
|
|
22
|
|
|
$this->dataTransferObject = $dataTransferObject; |
|
23
|
|
|
|
|
24
|
|
|
$this->resolveUseStatements(); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
protected function resolveUseStatements() |
|
28
|
|
|
{ |
|
29
|
|
|
$handle = fopen($this->getFileName(), 'r'); |
|
30
|
|
|
|
|
31
|
|
|
while ($line = fgets($handle)) { |
|
32
|
|
|
$line = trim($line); |
|
33
|
|
|
|
|
34
|
|
|
if (strpos($line, 'use ') !== 0) { |
|
35
|
|
|
continue; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
$fqcn = str_replace(['use ', ';'], '', $line); |
|
39
|
|
|
|
|
40
|
|
|
$classParts = explode('\\', $fqcn); |
|
41
|
|
|
|
|
42
|
|
|
$alias = end($classParts); |
|
43
|
|
|
|
|
44
|
|
|
$this->uses[$alias] = $fqcn; |
|
45
|
|
|
|
|
46
|
|
|
if (strpos($line, 'class') !== false) { |
|
47
|
|
|
break; |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
fclose($handle); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function hasAlias(string $alias): bool |
|
55
|
|
|
{ |
|
56
|
|
|
return isset($this->uses[$alias]); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function resolveAlias(string $alias): string |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->uses[$alias]; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @return \Spatie\DataTransferObject\DataTransferObjectProperty[] |
|
66
|
|
|
*/ |
|
67
|
|
|
public function getDataTransferObjectProperties(): array |
|
68
|
|
|
{ |
|
69
|
|
|
$properties = []; |
|
70
|
|
|
|
|
71
|
|
|
foreach (parent::getProperties(ReflectionProperty::IS_PUBLIC) as $reflectionProperty) { |
|
|
|
|
|
|
72
|
|
|
$properties[$reflectionProperty->getName()] = DataTransferObjectProperty::fromReflection( |
|
73
|
|
|
$this->dataTransferObject, |
|
74
|
|
|
$this, |
|
75
|
|
|
$reflectionProperty |
|
76
|
|
|
); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return $properties; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()method in theSoncalls the wrong method in the parent class.