Skip to content

Commit 08ab1e7

Browse files
feat: New Kirby\Reflection\Field class to extract info about fields
feat: Reflect parent props
1 parent 032228e commit 08ab1e7

1 file changed

Lines changed: 74 additions & 0 deletions

File tree

src/Reflection/Field.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
namespace Kirby\Reflection;
4+
5+
use Kirby\Form\FieldClass;
6+
use ReflectionClass;
7+
8+
class Field
9+
{
10+
protected Constructor $constructor;
11+
protected ReflectionClass $class;
12+
13+
public function __construct(
14+
protected FieldClass|string $field
15+
) {
16+
$this->class = new ReflectionClass($field);
17+
$this->constructor = new Constructor($field);
18+
}
19+
20+
protected function parentClass(): ReflectionClass
21+
{
22+
return $this->class->getParentClass();
23+
}
24+
25+
protected function parentProps(): array
26+
{
27+
return $this->parentReflection()->props();
28+
}
29+
30+
protected function parentReflection(): static
31+
{
32+
return new static($this->parentClass()->getName());
33+
}
34+
35+
/**
36+
* Returns field properties based on the constructor signature.
37+
*/
38+
public function props(): array
39+
{
40+
$props = [];
41+
$ignore = ['model', 'siblings'];
42+
43+
foreach ($this->constructor->getParameters() as $parameter) {
44+
$name = $parameter->getName();
45+
46+
if (in_array($name, $ignore) === true) {
47+
continue;
48+
}
49+
50+
// resolve ... by getting props from the parent class
51+
if ($parameter->isVariadic()) {
52+
$props = [
53+
...$this->parentProps(),
54+
...$props,
55+
];
56+
continue;
57+
}
58+
59+
$property = $this->class->hasProperty($name) ? $this->class->getProperty($name) : null;
60+
$comment = DocComment::from($property);
61+
62+
$props[$name] = [
63+
'name' => $name,
64+
'type' => (string)$parameter->getType(),
65+
'default' => $parameter->getDefaultValue(),
66+
'description' => $comment->description()
67+
];
68+
}
69+
70+
ksort($props);
71+
72+
return $props;
73+
}
74+
}

0 commit comments

Comments
 (0)