Skip to content

Commit

Permalink
✨ added Ink::* helpers
Browse files Browse the repository at this point in the history
Signed-off-by: bnomei <[email protected]>
  • Loading branch information
bnomei committed Nov 26, 2023
1 parent 1e7acef commit c25e88f
Show file tree
Hide file tree
Showing 38 changed files with 424 additions and 153 deletions.
63 changes: 62 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
[![Maintainability](https://flat.badgen.net/codeclimate/maintainability/bnomei/kirby-blueprints)](https://codeclimate.com/github/bnomei/kirby-blueprints)
[![Twitter](https://flat.badgen.net/badge/twitter/bnomei?color=66d9ef)](https://twitter.com/bnomei)

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

## Install

Expand Down Expand Up @@ -473,6 +473,67 @@ Column::make()
]),
```
## Why is it called Ink?
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.

```php
<?php

use ...;

class ElephantPage extends Page
{
use HasInk;

#[
Label('Left Ear'),
Type(FieldTypes::TEXT),
]
public Field $leftEar;

#[
Label('Right Ear'),
Type(FieldTypes::TAGS),
]
public Field $rightEar;

#[
Blueprint
]
public static function elephantsBlueprint(): array
{
$user = kirby()->user();

return Ink::page(
title: 'Elephant',
columns: [
Ink::column(2/3)->fields([
'leftEar' => true,
Ink::field(FieldTypes::BLOCKS)
->id('trunk')
->label('Trunk Blocks')
->property('empty', '🐘'),
'rightEar' => true,
]),
Ink::column(1/3)->sections([
Ink::section(SectionTypes::FIELDS)->fields([
Ink::field('text', label: 'User')
->property('placeholder', $user?->email().' ('.$user?->role().')'),
]),
Ink::section(SectionTypes::INFO)
->label('Kirby Version')
->theme('info')
->text(kirby()->version()),
Ink::section(SectionTypes::FILES)
->label('Files'),
]),
],
)->toArray();
}
}
```

## Disclaimer

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).
Expand Down
32 changes: 32 additions & 0 deletions classes/Blueprints/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,36 @@ public static function arrayRemoveByValuesRecursive(array $haystack, array $valu

return $haystack;
}

public static function arraySetKeysFromColumns(array $data): array
{
foreach ($data as $key => $value) {
if (in_array($key, ['fields', 'sections', 'columns', 'tabs']) && is_array($value) && count($value)) {
$updated = [];
// if item has column id or label than use that as a key
foreach ($value as $id => $item) {
if (is_numeric($id) === false) {
$updated[$id] = $item; // keep

continue; // do not overwrite those set manually in arrays
}
if (isset($item['id'])) {
$updated[Str::camel($item['id'])] = $item;
} elseif (isset($item['label'])) {
$updated[Str::camel($item['label'])] = $item;
} else {
$updated[Str::random(5)] = $item;
}
}

$value = $updated;
}

if (is_array($value)) {
$data[$key] = static::arraySetKeysFromColumns($value);
}
}

return $data;
}
}
7 changes: 6 additions & 1 deletion classes/Blueprints/HasFluentSetter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@ trait HasFluentSetter
{
public function __call($name, $arguments): self
{
// fluent setter
// fluent setter for properties
if (count($arguments) === 1 && property_exists($this, $name)) {
$this->{$name} = $arguments[0];
}

// fluent setter for dynamic properties
if (count($arguments) === 1 && property_exists($this, 'properties') && is_array($this->properties)) {
$this->properties[$name] = $arguments[0];
}

return $this;
}
}
3 changes: 3 additions & 0 deletions classes/Blueprints/HasProperties.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public function property(string $key, mixed $value): self
return $this;
}

/**
* @internal
*/
public function jsonSerialize(): array
{
return $this->toArray();
Expand Down
11 changes: 11 additions & 0 deletions classes/Blueprints/HasStaticMake.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Bnomei\Blueprints;

trait HasStaticMake
{
public static function make(...$args): self
{
return new static(...$args);
}
}
1 change: 1 addition & 0 deletions classes/Blueprints/IsArrayable.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public function toArray(): array
$data = json_decode(json_encode($this), true) ?? [];
ksort($data);

$data = Blueprint::arraySetKeysFromColumns($data);
$data = Blueprint::arrayRemoveByValuesRecursive($data, [null, '', []]);

return $data;
Expand Down
9 changes: 6 additions & 3 deletions classes/Blueprints/Schema/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
use Bnomei\Blueprints\IsArrayable;

/**
* @method width(float|string|null $width): self
* @method sections(Section[] $sections): self
* @method fields(Field[] $fields): self
* @method self width(float|string|null $width)
* @method self sections(Section[] $sections)
* @method self id(string $id)
* @method self fields(Field[] $fields)
*/
class Column
{
Expand All @@ -22,6 +23,7 @@ class Column
public function __construct(
public string|float|null $width = null,
public bool $sticky = false,
public ?string $id = null,
public array $sections = [],
public array $fields = [],
) {
Expand All @@ -34,6 +36,7 @@ public function __construct(
public static function make(
string|float $width = null,
bool $sticky = false,
string $id = null,
array $sections = [],
array $fields = [],
): static {
Expand Down
25 changes: 13 additions & 12 deletions classes/Blueprints/Schema/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,32 @@

use Bnomei\Blueprints\HasFluentSetter;
use Bnomei\Blueprints\HasProperties;
use Bnomei\Blueprints\HasStaticMake;
use JsonSerializable;

/**
* @method label(array|string|null $label): self
* @method width(float|string|null $width): self
* @method self type(FieldTypes|string $type)
* @method self id(string|null $id)
* @method self label(array|string|null $label)
* @method self width(float|string|null $width)
* @method self property(FieldProperties|string $name, mixed $value)
* @method self properties(array $properties)
*/
class Field implements JsonSerializable
{
use HasFluentSetter;
use HasProperties;
use HasStaticMake;

public mixed $type = null;

public function __construct(
public mixed $type = null,
mixed $type = null,
public ?string $id = null,
public string|array|null $label = null,
public array $properties = [],
public string|float|null $width = null,
) {
}

public static function make(
mixed $type = null,
string|array $label = null,
array $properties = [],
string|float $width = null,
): static {
return new static(...func_get_args());
$this->type ??= $type; // allow override from inheriting class
}
}
2 changes: 2 additions & 0 deletions classes/Blueprints/Schema/FieldProperties.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ enum FieldProperties: string
case BUTTONS = 'buttons';
case MAXLENGTH = 'maxlength';
case SPELLCHECK = 'spellcheck';

// TODO: add more
}
20 changes: 20 additions & 0 deletions classes/Blueprints/Schema/Fields/TextField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Bnomei\Blueprints\Schema\Fields;

use Bnomei\Blueprints\HasStaticMake;
use Bnomei\Blueprints\Schema\Field;

/**
* @method label(array|string|null $label): TextField
* @method placeholder(array|string|null $placeholder): TextField
* @method width(float|string|null $width): TextField
*
* @deprecated use Field::make('text') instead, this is just me testing stuff
*/
class TextField extends Field
{
use HasStaticMake;

public mixed $type = 'text';
}
6 changes: 3 additions & 3 deletions classes/Blueprints/Schema/Fields/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
use Bnomei\Blueprints\IsArrayable;
use JetBrains\PhpStorm\Deprecated;

#[
Deprecated
]
/**
* @deprecated use Field::make('url') instead, this is just me testing stuff
*/
class Url
{
use IsArrayable;
Expand Down
8 changes: 4 additions & 4 deletions classes/Blueprints/Schema/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
use Bnomei\Blueprints\IsArrayable;

/**
* @method tabs(Tab[] $tabs): self
* @method sections(Section[] $sections): self
* @method columns(Column[] $columns): self
* @method fields(Field[] $fields): self
* @method self tabs(Tab[] $tabs)
* @method self sections(Section[] $sections)
* @method self columns(Column[] $columns)
* @method self fields(Field[] $fields)
*/
class File
{
Expand Down
30 changes: 15 additions & 15 deletions classes/Blueprints/Schema/FileAccept.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@
use Bnomei\Blueprints\HasProperties;

/**
* @method changeName(bool|array $changeName = true): self
* @method replace(bool|array $replace = true): self
* @method delete(bool|array $delete = true): self
* @method read(bool|array $read = true): self
* @method update(bool|array $update = true): self
* @method extension(null|string|array $extension = null): self
* @method mime(null|string|array $mime = null): self
* @method maxHeight(null|int $maxheight = null): self
* @method maxSize(null|int $maxsize = null): self
* @method maxWidth(null|int $maxwidth = null): self
* @method minHeight(null|int $minheight = null): self
* @method minSize(null|int $minsize = null): self
* @method minWidth(null|int $minwidth = null): self
* @method orientation(null|string $orientation = null): self
* @method type(null|string|array $type = null): self
* @method self changeName(bool|array $changeName = true)
* @method self replace(bool|array $replace = true)
* @method self delete(bool|array $delete = true)
* @method self read(bool|array $read = true)
* @method self update(bool|array $update = true)
* @method self extension(null|string|array $extension = null)
* @method self mime(null|string|array $mime = null)
* @method self maxHeight(null|int $maxheight = null)
* @method self maxSize(null|int $maxsize = null)
* @method self maxWidth(null|int $maxwidth = null)
* @method self minHeight(null|int $minheight = null)
* @method self minSize(null|int $minsize = null)
* @method self minWidth(null|int $minwidth = null)
* @method self orientation(null|string $orientation = null)
* @method self type(null|string|array $type = null)
*/
class FileAccept implements \JsonSerializable
{
Expand Down
10 changes: 5 additions & 5 deletions classes/Blueprints/Schema/FileOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
use Bnomei\Blueprints\HasProperties;

/**
* @method changeName(bool|array $changeName = true): self
* @method replace(bool|array $replace = true): self
* @method delete(bool|array $delete = true): self
* @method read(bool|array $read = true): self
* @method update(bool|array $update = true): self
* @method self changeName(bool|array $changeName = true)
* @method self replace(bool|array $replace = true)
* @method self delete(bool|array $delete = true)
* @method self read(bool|array $read = true)
* @method self update(bool|array $update = true)
*/
class FileOptions implements \JsonSerializable
{
Expand Down
8 changes: 4 additions & 4 deletions classes/Blueprints/Schema/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
use Bnomei\Blueprints\IsArrayable;

/**
* @method tabs(Tab[] $tabs): self
* @method sections(Section[] $sections): self
* @method columns(Column[] $columns): self
* @method fields(Field[] $fields): self
* @method self tabs(Tab[] $tabs)
* @method self sections(Section[] $sections)
* @method self columns(Column[] $columns)
* @method self fields(Field[] $fields)
*/
class Page
{
Expand Down
8 changes: 4 additions & 4 deletions classes/Blueprints/Schema/PageImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
use Bnomei\Blueprints\HasFluentSetter;

/**
* @method back(string $back): self
* @method color(string $color): self
* @method icon(string $icon): self
* @method query(string $query): self
* @method self back(string $back)
* @method self color(string $color)
* @method self icon(string $icon)
* @method self query(string $query)
*/
class PageImage
{
Expand Down
6 changes: 3 additions & 3 deletions classes/Blueprints/Schema/PageNavigation.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
use Bnomei\Blueprints\HasFluentSetter;

/**
* @method status(string|array $status): self
* @method template(string|array $template): self
* @method sortBy(string $sortBy): self
* @method self status(string|array $status)
* @method self template(string|array $template)
* @method self sortBy(string $sortBy)
*/
class PageNavigation
{
Expand Down
Loading

0 comments on commit c25e88f

Please sign in to comment.