Skip to content

Commit

Permalink
✨ Stats Section, Color Field, Props-based inline make, added missing @…
Browse files Browse the repository at this point in the history
…methods declarations

🐛 properties[] got set twice
✅ added some tests for Ink
  • Loading branch information
bnomei committed Oct 11, 2024
1 parent ade7dfc commit ec7513d
Show file tree
Hide file tree
Showing 27 changed files with 739 additions and 326 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ You will need to add two traits to your PageModel.
Without defining a YAML blueprint for the `article` template Kirby would have an empty blueprint definition. But with the attributes defined in the PageModel Kirby will know what fields we want to have in the blueprint (registered by the *autoloader helper*).

**site/plugins/example/models/ArticlePage.php**

```php
<?php
Expand Down Expand Up @@ -259,7 +260,7 @@ Most likely you will want a more complex blueprint definition than just fields a

You can find all available PHP attributes in the [classes/Blueprints/Attributes folder](https://github.com/bnomei/kirby-blueprints/tree/main/classes/Blueprints/Attributes) of this repository. They reflect the properties you would set for a given field in a YAML blueprint. For some properties I created variants since different fields use the same property name but with different meanings (like `max` in a `number` field vs. `max` in a `date` field) and I wanted to keep them unambiguous in PHP.

`Accept, After, Api, Autocomplete, Autofocus, Before, Blueprint, Buttons, Calendar, Columns, ColumnsCount, Converter, Counter, CustomType, DefaultValue, Disabled, Display, EmptyValue, ExtendsField, Fieldsets, Fields, Files, Font, Format, GenericAttribute, Grow, Help, Icon, Image, Info, Inline, Label, Layout, LayoutSettings, Layouts, Link, Marks, Max, MaxDate, MaxRange, MaxTime, MaxLength, Min, MinDate, MinLength, MinRange, MinTime, Multiple, Nodes, Numbered, Options, Path, Pattern, Placeholder, Prepend, Property, Query, Range, Required, Reset, Search, Separator, Size, SortBy, Sortable, Spellcheck, Step, Store, Subpages, Sync, Text, Theme, Time, TimeNotation, Tooltip, Translate, Type, Uploads, When, Width, Wizard`
`Alpha, Accept, After, Api, Autocomplete, Autofocus, Before, Blueprint, Buttons, Calendar, Columns, ColumnsCount, Converter, Counter, CustomType, DefaultValue, Disabled, Display, EmptyValue, ExtendsField, Fieldsets, Fields, Files, Font, Format, GenericAttribute, Grow, Help, Icon, Image, Info, Inline, Label, Layout, LayoutSettings, Layouts, Link, Marks, Max, MaxDate, MaxRange, MaxTime, MaxLength, Min, MinDate, MinLength, MinRange, MinTime, Mode, Multiple, Nodes, Numbered, Options, Path, Pattern, Placeholder, Prepend, Property, Query, Range, Required, Reset, Search, Separator, Size, SortBy, Sortable, Spellcheck, Step, Store, Subpages, Sync, Text, Theme, Time, TimeNotation, Tooltip, Translate, Type, Uploads, When, Width, Wizard`

### Benefits of using this plugin

Expand Down Expand Up @@ -371,6 +372,7 @@ You will use the `*::make()`-helpers to create a blueprint definition in a PHP b
This will also give you the benefit of not having any blueprint files at all.

**site/plugins/example/models/ArticlePage.php**

```php
<?php

Expand All @@ -388,13 +390,13 @@ class ProductPage extends \Kirby\Cms\Page
CustomType('qrcode'),
Property('Custom key', 'custom data'),
]
public Kirby\Content\Field $qrcode;
public \Kirby\Content\Field $qrcode;
#[
Type(FieldTypes::EMAIL),
Placeholder('Email Field from Property')
]
public Kirby\Content\Field $email;
public \Kirby\Content\Field $email;
#[
Blueprint
Expand Down
16 changes: 16 additions & 0 deletions classes/Blueprints/Attributes/Alpha.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Bnomei\Blueprints\Attributes;

use Attribute;

#[Attribute(Attribute::TARGET_PROPERTY)]
class Alpha extends GenericAttribute
{
/**
* Use the alpha option (default: false) to activate alpha transparency support
*/
public function __construct(
public bool $alpha = false
) {}
}
18 changes: 18 additions & 0 deletions classes/Blueprints/Attributes/Mode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Bnomei\Blueprints\Attributes;

use Attribute;

#[Attribute(Attribute::TARGET_PROPERTY)]
class Mode extends GenericAttribute
{
/**
* With the mode option you control which elements of the color field are available. Possible values: picker, input, options.
*
* @param string $mode
*/
public function __construct(
public string $mode
) {}
}
16 changes: 16 additions & 0 deletions classes/Blueprints/HasFluentGetter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Bnomei\Blueprints;

trait HasFluentGetter
{
public function __get(string $name): mixed
{
// fluent getter for dynamic properties
if (property_exists($this, 'properties') && is_array($this->properties) && array_key_exists($name, $this->properties)) {
return $this->properties[$name];
}

return null;
}
}
1 change: 1 addition & 0 deletions classes/Blueprints/HasFluentSetter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

trait HasFluentSetter
{
// TODO: refactor to __set
public function __call(string $name, array $arguments = []): self
{
// fluent setter for properties
Expand Down
7 changes: 7 additions & 0 deletions classes/Blueprints/HasProperties.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ public function toArray(): array
return $data;
}

public function properties(array $merge): self
{
$this->properties = array_merge($this->properties, $merge);

return $this;
}

public function property(string $key, mixed $value): self
{
$this->properties[$key] = $value;
Expand Down
1 change: 1 addition & 0 deletions classes/Blueprints/Schema/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* @method self sections(Section[] $sections)
* @method self id(string $id)
* @method self fields(Field[] $fields)
* @method self sticky(bool $sticky)
*/
class Column
{
Expand Down
4 changes: 4 additions & 0 deletions classes/Blueprints/Schema/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Bnomei\Blueprints\Schema;

use Bnomei\Blueprints\HasFluentGetter;
use Bnomei\Blueprints\HasFluentSetter;
use Bnomei\Blueprints\HasProperties;
use Bnomei\Blueprints\HasStaticMake;
Expand All @@ -12,11 +13,13 @@
* @method self id(string|null $id)
* @method self label(array|string|null $label)
* @method self width(float|string|null $width)
* @method self extends(string|null $extends)
* @method self property(string $name, mixed $value)
* @method self properties(array $properties)
*/
class Field implements JsonSerializable
{
use HasFluentGetter;
use HasFluentSetter;
use HasProperties;
use HasStaticMake;
Expand All @@ -29,6 +32,7 @@ public function __construct(
public string|array|null $label = null,
public array $properties = [],
public string|float|null $width = null,
public ?string $extends = null,
) {
$this->type ??= $type; // allow override from inheriting class
}
Expand Down
42 changes: 42 additions & 0 deletions classes/Blueprints/Schema/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,46 @@ public static function make(
): self {
return new self(...func_get_args()); // @phpstan-ignore-line
}

public function accept(
string|array|null $extension = null,
string|array|null $mime = null,
?int $maxheight = null,
?int $maxsize = null,
?int $maxwidth = null,
?int $minheight = null,
?int $minsize = null,
?int $minwidth = null,
?string $orientation = null, // landscape/square/portrait
string|array|null $type = null,
array $properties = [],
): self {
$this->accept = FileAccept::make(...func_get_args()); // @phpstan-ignore-line;

return $this;
}

public function image(
?string $back = null,
?string $color = null,
?string $icon = null,
?string $query = null,
): self {
$this->image = FileImage::make(...func_get_args()); // @phpstan-ignore-line;

return $this;
}

public function options(
bool|array $changeName = true,
bool|array $replace = true,
bool|array $delete = true,
bool|array $read = true,
bool|array $update = true,
array $properties = [],
): self {
$this->options = FileOptions::make(...func_get_args()); // @phpstan-ignore-line

return $this;
}
}
57 changes: 55 additions & 2 deletions classes/Blueprints/Schema/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@
* @method self sections(Section[] $sections)
* @method self columns(Column[] $columns)
* @method self fields(Field[] $fields)
* @method self title(string $title)
* @method self num(string $num)
* @method self icon(string|Icon $icon)
*/
class Page
{
use HasFluentSetter;
use IsArrayable;

public function __construct(
public string $title, // TODO: should be an string OR array of languages
public mixed $title,
public ?string $num = null,
public mixed $status = null,
public mixed $icon = null,
Expand All @@ -42,7 +45,7 @@ public function __construct(
* @param array<Field> $fields
*/
public static function make(
string $title,
mixed $title,
?string $num = null,
mixed $status = null,
mixed $icon = null,
Expand All @@ -56,4 +59,54 @@ public static function make(
): self {
return new self(...func_get_args()); // @phpstan-ignore-line

Check failure on line 60 in classes/Blueprints/Schema/Page.php

View workflow job for this annotation

GitHub Actions / phpstan

No error to ignore is reported on line 60.

Check failure on line 60 in classes/Blueprints/Schema/Page.php

View workflow job for this annotation

GitHub Actions / phpstan

No error to ignore is reported on line 60.
}

public function status(
string|array $draft,
string|array $unlisted,
string|array $listed,
): self {
$this->status = PageStatus::make(...func_get_args()); // @phpstan-ignore-line;

return $this;
}

public function image(
?string $back = null,
?string $color = null,
?string $icon = null,
?string $query = null,
): self {
$this->image = PageImage::make(...func_get_args()); // @phpstan-ignore-line;

return $this;
}

public function options(
bool|array $changeSlug = true,
bool|array $changeStatus = true,
bool|array $changeTemplate = true,
bool|array $changeTitle = true,
bool|array $create = true,
bool|array $delete = true,
bool|array $duplicate = true,
bool|string $preview = true,
bool|array $read = true,
bool|array $sort = true,
bool|array $update = true,
array $properties = [],
): self {
$this->options = PageOptions::make(...func_get_args()); // @phpstan-ignore-line

return $this;
}

public function navigation(
string|array $status = 'all',
string|array $template = 'all',
string $sortBy = 'title asc',
): self {
$this->navigation = PageNavigation::make(...func_get_args()); // @phpstan-ignore-line

return $this;
}
}
4 changes: 2 additions & 2 deletions classes/Blueprints/Schema/Section.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function __construct(
public mixed $info = null,
public ?string $id = null,
public mixed $label = null,
public ?string $layout = 'list',
public ?string $layout = null,
public ?int $limit = null,
public ?int $max = null,
public ?int $min = null,
Expand Down Expand Up @@ -88,7 +88,7 @@ public static function make(
mixed $info = null,
?string $id = null,
mixed $label = null,
?string $layout = 'list',
?string $layout = null,
?int $limit = null,
?int $max = null,
?int $min = null,
Expand Down
10 changes: 10 additions & 0 deletions classes/Blueprints/Schema/Site.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,14 @@ public static function make(
): self {
return new self(...func_get_args()); // @phpstan-ignore-line
}

public function options(
bool|array $changeTitle = true,
bool|array $update = true,
array $properties = [],
): self {
$this->options = SiteOptions::make(...func_get_args()); // @phpstan-ignore-line;

return $this;
}
}
19 changes: 19 additions & 0 deletions classes/Blueprints/Schema/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
* @method self sections(Section[] $sections)
* @method self columns(Column[] $columns)
* @method self fields(Field[] $fields)
* @method self name(string $name)
* @method self title(string $title)
* @method self description(string $description)
* @method self home(string $home)
* @method self image(UserImage $image)
*/
class User
{
Expand Down Expand Up @@ -51,4 +56,18 @@ public static function make(
): self {
return new self(...func_get_args()); // @phpstan-ignore-line
}

public function permissions(
bool|array $access = true,
bool|array $files = true,
bool|array $pages = true,
bool|array $site = true,
bool|array $user = true,
bool|array $users = true,
array $properties = []
): self {
$this->permissions = UserPermissions::make(...func_get_args()); // @phpstan-ignore-line;

return $this;
}
}
Loading

0 comments on commit ec7513d

Please sign in to comment.