Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove option validator #10

Merged
merged 17 commits into from
Sep 28, 2024
Merged
5 changes: 2 additions & 3 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

## Website Variables
SITE_IMAGE=ghcr.io/syntatis/wp:php7.4-fpm
SITE_PORT=7004
SITE_PORT=7006
SITE_HOST=localhost
SITE_TITLE=syntatis/codex
SITE_ADMIN_USER=admin
Expand All @@ -16,8 +16,7 @@ DB_IMAGE=mariadb:10.11.6

### Database Variables (Test)
DB_TEST_HOST=db_test
DB_TEST_PORT=3306
DB_TEST_PORT_PUBLISHED=8004
DB_TEST_PORT_PUBLISHED=8006
DB_TEST_NAME=wp_test
DB_TEST_USER=wp_test
DB_TEST_PASS=wp_test
Expand Down
18 changes: 0 additions & 18 deletions app/Contracts/Enqueueable.php

This file was deleted.

26 changes: 12 additions & 14 deletions app/Foundation/Settings/Registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,20 @@

namespace Codex\Foundation\Settings;

use Codex\Contracts\Hookable;
use Codex\Foundation\Hooks\Hook;
use Codex\Foundation\Settings\Support\SettingRegistrar;
use InvalidArgumentException;
use Syntatis\Utils\Val;

use function count;

class Registry implements Hookable
class Registry
{
private Hook $hook;

private string $prefix = '';

/** @phpstan-var non-empty-string $settingGroup */
private string $settingGroup;

/** @var array<Setting> */
/** @var array<string,Setting> */
private array $settings = [];

/** @var array<string,SettingRegistrar> */
Expand All @@ -36,27 +32,23 @@ public function __construct(string $settingGroup)
$this->settingGroup = $settingGroup;
}

public function hook(Hook $hook): void
{
$this->hook = $hook;
}

public function setPrefix(string $prefix): void
{
$this->prefix = $prefix;
}

public function addSettings(Setting ...$settings): void
{
$this->settings = [...$this->settings, ...$settings];
foreach ($settings as $key => $setting) {
$this->settings[$setting->getName()] = $setting;
}
}

public function register(): void
{
foreach ($this->settings as $setting) {
$registry = new SettingRegistrar($setting, $this->settingGroup);
$registry->setPrefix($this->prefix);
$registry->hook($this->hook);
$registry->register();

$this->registered[$registry->getName()] = $registry;
Expand All @@ -73,8 +65,14 @@ public function getSettingGroup(): string
return $this->settingGroup;
}

/** @return array<string,Setting> */
public function getSettings(): array
{
return $this->settings;
}

/** @return array<string,SettingRegistrar> */
public function getRegistered(): array
public function getRegisteredSettings(): array
{
return $this->registered;
}
Expand Down
56 changes: 26 additions & 30 deletions app/Foundation/Settings/Setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use function array_merge;

/**
* @phpstan-type Constraints callable|null
* @phpstan-type ValueDefault bool|float|int|string|array<array-key, bool|float|int|string|array<array-key, mixed>>|null
* @phpstan-type ValueFormat 'date-time'|'uri'|'email'|'ip'|'uuid'|'hex-color'
* @phpstan-type ValueType 'string'|'boolean'|'integer'|'number'|'array'|'object'
Expand All @@ -22,28 +21,25 @@
*/
class Setting
{
private string $name;
protected string $name;

/** @phpstan-var ValueType */
private string $type = 'string';
protected string $type = 'string';

/** @phpstan-var ValueDefault */
private $default = null;
protected $default = null;

/**
* The priority determines the order in which the `option_` related hooks
* are executed.
*/
private int $priority = 73;

/** @phpstan-var array<Constraints> */
private $constraints = [];
protected int $priority = 73;

/**
* @var array<string, mixed>
* @phpstan-var SettingVars
*/
private array $settingVars = ['show_in_rest' => true];
protected array $settingVars = ['show_in_rest' => true];

/** @phpstan-param ValueType $type */
public function __construct(string $name, string $type = 'string')
Expand All @@ -64,33 +60,40 @@ public function getName(): string
/**
* @param array|bool|float|int|string $value
* @phpstan-param ValueDefault $value
*
* @return static
*/
public function withDefault($value): self
public function withDefault($value)
{
$self = clone $this;
$self->default = $value;

return $self;
}

/** @phpstan-return ValueDefault */
public function getDefault()
/** @return static */
public function withLabel(string $label)
{
return $this->default;
$self = clone $this;
$self->settingVars['label'] = $label;

return $self;
}

public function withLabel(string $label): self
/** @return static */
public function withDescription(string $value)
{
$self = clone $this;
$self->settingVars['label'] = $label;
$self->settingVars['description'] = $value;

return $self;
}

public function withDescription(string $value): self
/** @return static */
public function withPriority(int $priority)
{
$self = clone $this;
$self->settingVars['description'] = $value;
$self->priority = $priority;

return $self;
}
Expand All @@ -99,8 +102,10 @@ public function withDescription(string $value): self
* Whether to show the option on WordPress REST API endpoint, `/wp/v2/settings`.
*
* @phpstan-param APISchema $schema
*
* @return static
*/
public function apiSchema(array $schema): self
public function apiSchema(array $schema)
{
$self = clone $this;
$self->settingVars['show_in_rest'] = [
Expand All @@ -111,19 +116,10 @@ public function apiSchema(array $schema): self
return $self;
}

/** @phpstan-param Constraints ...$constraints */
public function withConstraints(...$constraints): self
{
$self = clone $this;
$self->constraints = $constraints;

return $self;
}

/** @phpstan-return array<Constraints> */
public function getConstraints(): array
/** @phpstan-return ValueDefault */
public function getDefault()
{
return $this->constraints;
return $this->default;
}

public function getPriority(): int
Expand Down
63 changes: 0 additions & 63 deletions app/Foundation/Settings/Support/InputValidator.php

This file was deleted.

Loading