Skip to content

Commit

Permalink
Refactoring src/Screen/Layouts
Browse files Browse the repository at this point in the history
  • Loading branch information
bald-cat committed Jan 25, 2025
1 parent b8e3452 commit b5fb1d8
Show file tree
Hide file tree
Showing 39 changed files with 233 additions and 386 deletions.
8 changes: 2 additions & 6 deletions src/Platform/Http/Layouts/NotificationTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@

class NotificationTable extends Table
{
/**
* Data source.
*
* @var string
*/
public $target = 'notifications';

public string $target = 'notifications';

/**
* @return TD[]
Expand Down
44 changes: 13 additions & 31 deletions src/Screen/Layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Orchid\Screen;

use Illuminate\Support\Arr;
use Illuminate\View\View;
use JsonSerializable;

/**
Expand All @@ -18,42 +19,29 @@ abstract class Layout implements JsonSerializable
* The Main template to display the layer
* Represents the view() argument.
*
* @var string
*/
protected $template;
protected string $template;

/**
* Nested layers that should be
* displayed along with it.
*
* @var Layout[]
*/
protected $layouts = [];
protected array $layouts = [];

/**
* @var array
*/
protected $variables = [];
protected array $variables = [];

/**
* @var Repository
*/
protected $query;
protected Repository $query;

/**
* @return mixed
*/
abstract public function build(Repository $repository);
abstract public function build(Repository $repository): View|string|null;

/**
* @return mixed
*/
protected function buildAsDeep(Repository $repository)
protected function buildAsDeep(Repository $repository): ?View
{
$this->query = $repository;

if (! $this->isSee()) {
return;
return null;
}

$build = collect($this->layouts)
Expand All @@ -71,12 +59,12 @@ protected function buildAsDeep(Repository $repository)
}

/**
* @param array $layouts
* @param array $layouts
* @param int|string $key
*
* @param Repository $repository
* @return array
*/
protected function buildChild(iterable $layouts, $key, Repository $repository)
protected function buildChild(iterable $layouts, int|string $key, Repository $repository): array
{
return collect($layouts)
->flatten()
Expand All @@ -98,10 +86,7 @@ public function getSlug(): string
return sha1(json_encode($this));
}

/**
* @return Layout|null
*/
public function findBySlug(string $slug)
public function findBySlug(string $slug): ?Layout
{
if ($slug === $this->getSlug()) {
return $this;
Expand All @@ -122,10 +107,7 @@ public function findBySlug(string $slug)
->first();
}

/**
* @return Layout|null
*/
public function findByType(string $type)
public function findByType(string $type): ?Layout
{
if (is_subclass_of($this, $type)) {
return $this;
Expand Down
31 changes: 5 additions & 26 deletions src/Screen/Layouts/Accordion.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,19 @@

namespace Orchid\Screen\Layouts;

use Illuminate\Support\Arr;
use Orchid\Screen\Layout;
use Orchid\Screen\Repository;
use Illuminate\View\View;

/**
* Class Accordion.
*/
abstract class Accordion extends Layout
{
/**
* @var string
*/
protected $template = 'platform::layouts.accordion';

/**
* @var array
*/
protected $variables = [
protected string $template = 'platform::layouts.accordion';

protected array $variables = [
'stayOpen' => false,
'open' => [],
];
Expand All @@ -37,10 +32,7 @@ public function __construct(array $layouts = [])
$this->variables['open'] = [array_key_first($this->layouts)];
}

/**
* @return mixed
*/
public function build(Repository $repository)
public function build(Repository $repository): ?View
{
return $this->buildAsDeep($repository);
}
Expand All @@ -59,17 +51,4 @@ public function stayOpen(bool $stayOpen = true): self
return $this;
}

/**
* Set active accordion(s).
*
* @param string|array $activeAccordion
*
* @return $this
*/
public function open(string|array $activeAccordion): self
{
$this->variables['open'] = Arr::wrap($activeAccordion);

return $this;
}
}
11 changes: 3 additions & 8 deletions src/Screen/Layouts/Blank.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@
*/
abstract class Blank extends Layout
{
/**
* @var string
*/
protected $template = 'platform::layouts.blank';

protected string $template = 'platform::layouts.blank';

/**
* Layout constructor.
Expand All @@ -27,10 +25,7 @@ public function __construct(array $layouts = [])
$this->layouts = $layouts;
}

/**
* @return \Illuminate\View\View|mixed
*/
public function build(Repository $repository)
public function build(Repository $repository): \Illuminate\View\View
{
return $this->buildAsDeep($repository);
}
Expand Down
35 changes: 12 additions & 23 deletions src/Screen/Layouts/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Orchid\Screen\Commander;
use Orchid\Screen\Layout;
use Orchid\Screen\Repository;
use Illuminate\View\View;

/**
* Class Block.
Expand All @@ -17,24 +18,13 @@ abstract class Block extends Layout
{
use Commander;

/**
* @var string
*/
protected $template = 'platform::layouts.block';
protected string $template = 'platform::layouts.block';

/**
* @var false[]
*/
protected $variables = [
protected array $variables = [
'vertical' => false,
];

/**
* Button commands.
*
* @var array
*/
protected $commandBar = [];
protected array $commandBar = [];

/**
* Layout constructor.
Expand All @@ -54,10 +44,7 @@ protected function commandBar(): array
return $this->commandBar;
}

/**
* @return mixed
*/
public function build(Repository $repository)
public function build(Repository $repository): ?View
{
$this->variables['commandBar'] = $this->buildCommandBar($repository);

Expand All @@ -77,9 +64,8 @@ public function title(string $title): self
/**
* Used to create the description of a group of form elements.
*
* @param string|\Illuminate\View\View description
*/
public function description($description): self
public function description(string|View $description): self
{
$this->variables['description'] = $description;

Expand All @@ -90,21 +76,24 @@ public function description($description): self
* Used to define block orientation.
*
* @param bool $vertical
* @return Block
*/
public function vertical($vertical = true): self
public function vertical(bool $vertical = true): self
{
$this->variables['vertical'] = $vertical;

return $this;
}

/**
* @param Action|Action[] description
* @param Action|Action[] $commands
*/
public function commands($commands): self
public function commands(Action|array $commands): self
{
$this->commandBar = Arr::wrap($commands);

return $this;
}


}
Loading

0 comments on commit b5fb1d8

Please sign in to comment.