Skip to content

Commit c25e88f

Browse files
committed
✨ added Ink::* helpers
Signed-off-by: bnomei <[email protected]>
1 parent 1e7acef commit c25e88f

38 files changed

+424
-153
lines changed

README.md

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
[![Maintainability](https://flat.badgen.net/codeclimate/maintainability/bnomei/kirby-blueprints)](https://codeclimate.com/github/bnomei/kirby-blueprints)
88
[![Twitter](https://flat.badgen.net/badge/twitter/bnomei?color=66d9ef)](https://twitter.com/bnomei)
99

10-
PHP Class-based Blueprints for Kirby CMS for better type safety and code completion.
10+
Kirby Ink - PHP Class-based Blueprints for Kirby CMS for better type safety and code completion.
1111

1212
## Install
1313

@@ -473,6 +473,67 @@ Column::make()
473473
]),
474474
```
475475
476+
## Why is it called Ink?
477+
478+
Because on top of all these `*::make()`-helpers it also introduces a new `Ink::*`-helpers to create a blueprint definition from a PageModel. And because it's short and easy to remember.
479+
480+
```php
481+
<?php
482+
483+
use ...;
484+
485+
class ElephantPage extends Page
486+
{
487+
use HasInk;
488+
489+
#[
490+
Label('Left Ear'),
491+
Type(FieldTypes::TEXT),
492+
]
493+
public Field $leftEar;
494+
495+
#[
496+
Label('Right Ear'),
497+
Type(FieldTypes::TAGS),
498+
]
499+
public Field $rightEar;
500+
501+
#[
502+
Blueprint
503+
]
504+
public static function elephantsBlueprint(): array
505+
{
506+
$user = kirby()->user();
507+
508+
return Ink::page(
509+
title: 'Elephant',
510+
columns: [
511+
Ink::column(2/3)->fields([
512+
'leftEar' => true,
513+
Ink::field(FieldTypes::BLOCKS)
514+
->id('trunk')
515+
->label('Trunk Blocks')
516+
->property('empty', '🐘'),
517+
'rightEar' => true,
518+
]),
519+
Ink::column(1/3)->sections([
520+
Ink::section(SectionTypes::FIELDS)->fields([
521+
Ink::field('text', label: 'User')
522+
->property('placeholder', $user?->email().' ('.$user?->role().')'),
523+
]),
524+
Ink::section(SectionTypes::INFO)
525+
->label('Kirby Version')
526+
->theme('info')
527+
->text(kirby()->version()),
528+
Ink::section(SectionTypes::FILES)
529+
->label('Files'),
530+
]),
531+
],
532+
)->toArray();
533+
}
534+
}
535+
```
536+
476537
## Disclaimer
477538

478539
This plugin is provided "as is" with no guarantee. Use it at your own risk and always test it yourself before using it in a production environment. If you find any issues, please [create a new issue](https://github.com/bnomei/kirby-blueprints/issues/new).

classes/Blueprints/Blueprint.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,36 @@ public static function arrayRemoveByValuesRecursive(array $haystack, array $valu
162162

163163
return $haystack;
164164
}
165+
166+
public static function arraySetKeysFromColumns(array $data): array
167+
{
168+
foreach ($data as $key => $value) {
169+
if (in_array($key, ['fields', 'sections', 'columns', 'tabs']) && is_array($value) && count($value)) {
170+
$updated = [];
171+
// if item has column id or label than use that as a key
172+
foreach ($value as $id => $item) {
173+
if (is_numeric($id) === false) {
174+
$updated[$id] = $item; // keep
175+
176+
continue; // do not overwrite those set manually in arrays
177+
}
178+
if (isset($item['id'])) {
179+
$updated[Str::camel($item['id'])] = $item;
180+
} elseif (isset($item['label'])) {
181+
$updated[Str::camel($item['label'])] = $item;
182+
} else {
183+
$updated[Str::random(5)] = $item;
184+
}
185+
}
186+
187+
$value = $updated;
188+
}
189+
190+
if (is_array($value)) {
191+
$data[$key] = static::arraySetKeysFromColumns($value);
192+
}
193+
}
194+
195+
return $data;
196+
}
165197
}

classes/Blueprints/HasFluentSetter.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,16 @@ trait HasFluentSetter
66
{
77
public function __call($name, $arguments): self
88
{
9-
// fluent setter
9+
// fluent setter for properties
1010
if (count($arguments) === 1 && property_exists($this, $name)) {
1111
$this->{$name} = $arguments[0];
1212
}
1313

14+
// fluent setter for dynamic properties
15+
if (count($arguments) === 1 && property_exists($this, 'properties') && is_array($this->properties)) {
16+
$this->properties[$name] = $arguments[0];
17+
}
18+
1419
return $this;
1520
}
1621
}

classes/Blueprints/HasProperties.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ public function property(string $key, mixed $value): self
4040
return $this;
4141
}
4242

43+
/**
44+
* @internal
45+
*/
4346
public function jsonSerialize(): array
4447
{
4548
return $this->toArray();
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Bnomei\Blueprints;
4+
5+
trait HasStaticMake
6+
{
7+
public static function make(...$args): self
8+
{
9+
return new static(...$args);
10+
}
11+
}

classes/Blueprints/IsArrayable.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public function toArray(): array
99
$data = json_decode(json_encode($this), true) ?? [];
1010
ksort($data);
1111

12+
$data = Blueprint::arraySetKeysFromColumns($data);
1213
$data = Blueprint::arrayRemoveByValuesRecursive($data, [null, '', []]);
1314

1415
return $data;

classes/Blueprints/Schema/Column.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
use Bnomei\Blueprints\IsArrayable;
77

88
/**
9-
* @method width(float|string|null $width): self
10-
* @method sections(Section[] $sections): self
11-
* @method fields(Field[] $fields): self
9+
* @method self width(float|string|null $width)
10+
* @method self sections(Section[] $sections)
11+
* @method self id(string $id)
12+
* @method self fields(Field[] $fields)
1213
*/
1314
class Column
1415
{
@@ -22,6 +23,7 @@ class Column
2223
public function __construct(
2324
public string|float|null $width = null,
2425
public bool $sticky = false,
26+
public ?string $id = null,
2527
public array $sections = [],
2628
public array $fields = [],
2729
) {
@@ -34,6 +36,7 @@ public function __construct(
3436
public static function make(
3537
string|float $width = null,
3638
bool $sticky = false,
39+
string $id = null,
3740
array $sections = [],
3841
array $fields = [],
3942
): static {

classes/Blueprints/Schema/Field.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,32 @@
44

55
use Bnomei\Blueprints\HasFluentSetter;
66
use Bnomei\Blueprints\HasProperties;
7+
use Bnomei\Blueprints\HasStaticMake;
78
use JsonSerializable;
89

910
/**
10-
* @method label(array|string|null $label): self
11-
* @method width(float|string|null $width): self
11+
* @method self type(FieldTypes|string $type)
12+
* @method self id(string|null $id)
13+
* @method self label(array|string|null $label)
14+
* @method self width(float|string|null $width)
15+
* @method self property(FieldProperties|string $name, mixed $value)
16+
* @method self properties(array $properties)
1217
*/
1318
class Field implements JsonSerializable
1419
{
1520
use HasFluentSetter;
1621
use HasProperties;
22+
use HasStaticMake;
23+
24+
public mixed $type = null;
1725

1826
public function __construct(
19-
public mixed $type = null,
27+
mixed $type = null,
28+
public ?string $id = null,
2029
public string|array|null $label = null,
2130
public array $properties = [],
2231
public string|float|null $width = null,
2332
) {
24-
}
25-
26-
public static function make(
27-
mixed $type = null,
28-
string|array $label = null,
29-
array $properties = [],
30-
string|float $width = null,
31-
): static {
32-
return new static(...func_get_args());
33+
$this->type ??= $type; // allow override from inheriting class
3334
}
3435
}

classes/Blueprints/Schema/FieldProperties.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@ enum FieldProperties: string
77
case BUTTONS = 'buttons';
88
case MAXLENGTH = 'maxlength';
99
case SPELLCHECK = 'spellcheck';
10+
11+
// TODO: add more
1012
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Bnomei\Blueprints\Schema\Fields;
4+
5+
use Bnomei\Blueprints\HasStaticMake;
6+
use Bnomei\Blueprints\Schema\Field;
7+
8+
/**
9+
* @method label(array|string|null $label): TextField
10+
* @method placeholder(array|string|null $placeholder): TextField
11+
* @method width(float|string|null $width): TextField
12+
*
13+
* @deprecated use Field::make('text') instead, this is just me testing stuff
14+
*/
15+
class TextField extends Field
16+
{
17+
use HasStaticMake;
18+
19+
public mixed $type = 'text';
20+
}

0 commit comments

Comments
 (0)