Skip to content

Commit 4af1cc4

Browse files
first steps
first steps
1 parent 11d2441 commit 4af1cc4

File tree

3 files changed

+129
-13
lines changed

3 files changed

+129
-13
lines changed

src/Form/Field/BlocksField.php

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Kirby\Data\Json;
1212
use Kirby\Exception\InvalidArgumentException;
1313
use Kirby\Exception\NotFoundException;
14+
use Kirby\Form\Fields;
1415
use Kirby\Form\FieldClass;
1516
use Kirby\Form\Form;
1617
use Kirby\Form\Mixin\EmptyState;
@@ -25,29 +26,60 @@ class BlocksField extends FieldClass
2526
use Max;
2627
use Min;
2728

29+
/**
30+
* Defines the allowed block types in the blocks field. See below.
31+
*/
2832
protected Fieldsets $fieldsets;
2933
protected array $forms;
34+
35+
/**
36+
* Group name to identify all block fields that can share blocks via drag & drop
37+
*/
3038
protected string|null $group;
39+
40+
/**
41+
* Saves pretty printed JSON in text files
42+
*/
3143
protected bool $pretty;
3244
protected mixed $value = [];
3345

3446
public function __construct(
35-
string|array|null $empty = null,
36-
string|array|null $fieldsets = null,
47+
bool $autofocus = false,
48+
array $default = [],
49+
bool $disabled = false,
50+
array|string|null $empty = null,
51+
array|string|null $fieldsets = null,
52+
array|string|null $help = null,
3753
string|null $group = null,
54+
array|string|null $label = null,
55+
ModelWithContent|null $model = null,
56+
string|null $name = null,
3857
int|null $max = null,
3958
int|null $min = null,
4059
bool $pretty = false,
41-
...$props
60+
bool $required = false,
61+
Fields|null $siblings = null,
62+
bool $translate = true,
63+
array $when = [],
64+
string|null $width = '1/1',
4265
) {
43-
$this->setFieldsets(
44-
$fieldsets,
45-
$props['model'] ?? App::instance()->site()
66+
parent::__construct(
67+
autofocus: $autofocus,
68+
default: $default,
69+
disabled: $disabled,
70+
help: $help,
71+
label: $label,
72+
model: $model,
73+
name: $name,
74+
required: $required,
75+
siblings: $siblings,
76+
translate: $translate,
77+
when: $when,
78+
width: $width
4679
);
4780

48-
parent::__construct(...$props);
49-
5081
$this->setEmpty($empty);
82+
$this->setFieldsets($fieldsets);
5183
$this->setGroup($group);
5284
$this->setMax($max);
5385
$this->setMin($min);
@@ -262,17 +294,14 @@ protected function setDefault(mixed $default = null): void
262294
parent::setDefault($default);
263295
}
264296

265-
protected function setFieldsets(
266-
string|array|null $fieldsets,
267-
ModelWithContent $model
268-
): void {
297+
protected function setFieldsets(string|array|null $fieldsets): void {
269298
if (is_string($fieldsets) === true) {
270299
$fieldsets = [];
271300
}
272301

273302
$this->fieldsets = Fieldsets::factory(
274303
$fieldsets,
275-
['parent' => $model]
304+
['parent' => $this->model]
276305
);
277306
}
278307

src/Reflection/DocComment.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Kirby\Reflection;
4+
5+
use ReflectionClass;
6+
use ReflectionFunction;
7+
use ReflectionMethod;
8+
use ReflectionProperty;
9+
10+
class DocComment
11+
{
12+
public function __construct(
13+
protected string $comment,
14+
) {
15+
}
16+
17+
public static function from(
18+
ReflectionClass|ReflectionFunction|ReflectionMethod|ReflectionProperty|null $object
19+
) {
20+
return new static($object?->getDocComment() ?? '');
21+
}
22+
23+
/**
24+
* Returns the cleaned docblock text of the given property.
25+
*/
26+
public function description(): string|null
27+
{
28+
$comment = preg_replace(['#^/\*\*#', '#\*/$#'], '', $this->comment);
29+
$lines = preg_split('/\R/', (string)$comment) ?: [];
30+
$lines = array_map(static fn(string $line): string => ltrim(trim($line), "* \t"), $lines);
31+
$lines = array_filter(
32+
$lines,
33+
static fn(string $line): bool => $line !== '' && str_starts_with($line, '@') === false
34+
);
35+
36+
return implode(' ', $lines);
37+
}
38+
}

src/Reflection/Field.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace Kirby\Reflection;
4+
5+
use Kirby\Form\FieldClass;
6+
use ReflectionClass;
7+
use ReflectionProperty;
8+
9+
class Field
10+
{
11+
protected Constructor $constructor;
12+
protected ReflectionClass $class;
13+
14+
public function __construct(
15+
protected FieldClass $field
16+
) {
17+
$this->class = new ReflectionClass($field);
18+
$this->constructor = new Constructor($field);
19+
}
20+
21+
/**
22+
* Returns field properties based on the constructor signature.
23+
*/
24+
public function props(): array
25+
{
26+
$props = [];
27+
$ignore = ['model', 'siblings', 'props'];
28+
29+
foreach ($this->constructor->getParameters() as $parameter) {
30+
$name = $parameter->getName();
31+
32+
if (in_array($name, $ignore) === true) {
33+
continue;
34+
}
35+
36+
$property = $this->class->hasProperty($name) ? $this->class->getProperty($name) : null;
37+
$comment = DocComment::from($property);
38+
39+
$props[$name] = [
40+
'name' => $name,
41+
'type' => (string)$parameter->getType(),
42+
'default' => $parameter->getDefaultValue(),
43+
'description' => $comment->description()
44+
];
45+
}
46+
47+
return $props;
48+
}
49+
}

0 commit comments

Comments
 (0)