Skip to content

Commit

Permalink
🚧 Page Blueprint
Browse files Browse the repository at this point in the history
Signed-off-by: bnomei <[email protected]>
  • Loading branch information
bnomei committed Aug 31, 2023
1 parent 03b49d2 commit d5d71c7
Show file tree
Hide file tree
Showing 14 changed files with 578 additions and 175 deletions.
17 changes: 11 additions & 6 deletions classes/Blueprints/HasBlueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static function registerBlueprintExtension()

// merge with blueprint from yaml file or class
$blueprint = array_merge_recursive(
// self::getBlueprintFromYamlFile(),
self::getBlueprintFromYamlFile(),
self::getBlueprintFromClass(),
);
if (! empty($blueprint)) {
Expand All @@ -41,17 +41,22 @@ public static function registerBlueprintExtension()
}

$rc = new ReflectionClass(self::class);
$pagesSlashModel = 'pages/'.strtolower(str_replace('Page', '', $rc->getShortName()));
$pm = [
$pagesSlashModel => $blueprint,
$typeSlashModel = $rc->getShortName();
if (Str::endsWith($rc->getShortName(), 'Page')) {
$typeSlashModel = 'pages/'.strtolower(str_replace('Page', '', $rc->getShortName()));
} elseif (Str::endsWith($rc->getShortName(), 'User')) {
$typeSlashModel = 'users/'.strtolower(str_replace('User', '', $rc->getShortName()));
}
$b = [
$typeSlashModel => $blueprint,
];

// some might not be cacheable like when they are class based and have dynamic fields
if (! isset(self::$cacheBlueprint) || self::$cacheBlueprint === true) {
BlueprintCache::set(static::class, $pm);
BlueprintCache::set(static::class, $b);
}

return $pm;
return $b;
}

public static function getBlueprintFieldsFromReflection(): array
Expand Down
18 changes: 18 additions & 0 deletions classes/Blueprints/HasFluentSetter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Bnomei\Blueprints;

trait HasFluentSetter
{
public function __call($name, $arguments): self
{
// fluent setter
if (count($arguments) === 1 && property_exists($this, $name)) {
$this->{$name} = $arguments[0];

return $this;
}

return parent::__call($name, $arguments);
}
}
45 changes: 45 additions & 0 deletions classes/Blueprints/HasProperties.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Bnomei\Blueprints;

use ReflectionClass;
use ReflectionProperty;

trait HasProperties
{
public function toArray(): array
{
$data = [];

// use reflection to get all public properties of class with their current value in an array
$rc = new ReflectionClass($this);
foreach ($rc->getProperties(ReflectionProperty::IS_PUBLIC) as $property) {
$data[$property->getName()] = $property->getValue($this);
}

// move all properties to root
if (isset($data['properties'])) {
$data = array_merge($data, $data['properties']);
unset($data['properties']);
}

ksort($data);

// empty() would catch 0 and false which is not what we want
$data = array_filter($data, fn ($value) => $value !== null && $value !== '' && $value !== []);

return $data;
}

public function property(string $key, mixed $value): self
{
$this->properties[$key] = $value;

return $this;
}

public function jsonSerialize(): array
{
return $this->toArray();
}
}
38 changes: 20 additions & 18 deletions classes/Blueprints/Schema/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,41 @@

namespace Bnomei\Blueprints\Schema;

use Bnomei\Blueprints\HasFluentSetter;
use Bnomei\Blueprints\IsArrayable;

/**
* @method width(float|string|null $width): self
* @method sections(Section[] $sections): self
* @method fields(Field[] $fields): self
*/
class Column
{
use HasFluentSetter;
use IsArrayable;

/**
* @param array<Section> $sections
* @param array<Field> $fields
*/
public static function make(
string|float $width = null,
// array $sections = [],
array $fields = [],
): self {
return new self(...func_get_args());
}

/**
* @param array<Section> $sections
* @param array<Field> $fields
*/
public function __construct(
public string|float|null $width = null,
// public array $section = [],
public bool $sticky = false,
public array $sections = [],
public array $fields = [],
) {
}

public function width(float|string|null $width): Column
{
$this->width = $width;

return $this;
/**
* @param array<Section> $sections
* @param array<Field> $fields
*/
public static function make(
string|float $width = null,
bool $sticky = false,
array $sections = [],
array $fields = [],
): static {
return new static(...func_get_args());
}
}
88 changes: 19 additions & 69 deletions classes/Blueprints/Schema/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,83 +2,33 @@

namespace Bnomei\Blueprints\Schema;

class Field implements \JsonSerializable
use Bnomei\Blueprints\HasFluentSetter;
use Bnomei\Blueprints\HasProperties;
use JsonSerializable;

/**
* @method label(array|string|null $label): self
* @method width(float|string|null $width): self
*/
class Field implements JsonSerializable
{
/**
* @param string|FieldTypes $type
* @param string|array<string,string> $label
*/
public static function make(
mixed $type = null,
string|array $label = null,
string|float $width = null,
array $properties = null,
): self {
return new self(...func_get_args());
}
use HasFluentSetter;
use HasProperties;

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

public function toArray(): array
{
$data = [];

// use reflection to get all public properties of class with their current value in an array
$rc = new \ReflectionClass($this);
foreach ($rc->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) {
$data[$property->getName()] = $property->getValue($this);
}

// move all properties to root
if (isset($data['properties'])) {
$data = array_merge($data, $data['properties']);
unset($data['properties']);
}

ksort($data);

// empty() would catch 0 and false which is not what we want
$data = array_filter($data, fn ($value) => $value !== null && $value !== '' && $value !== []);

return $data ?? [];
}

public function jsonSerialize(): array
{
return $this->toArray();
}

public function label(array|string|null $label): Field
{
$this->label = $label;

return $this;
}

public function width(float|string|null $width): Field
{
$this->width = $width;

return $this;
}

public function properties(?array $properties): Field
{
$this->properties = $properties;

return $this;
}

public function property(string $key, mixed $value): Field
{
$this->properties[$key] = $value;

return $this;
public static function make(
mixed $type = null,
string|array $label = null,
array $properties = [],
string|float $width = null,
): static {
return new static(...func_get_args());
}
}
Loading

0 comments on commit d5d71c7

Please sign in to comment.