Plugin Directory

Changeset 3474833


Ignore:
Timestamp:
03/04/2026 06:15:41 PM (4 weeks ago)
Author:
whiteshadow
Message:

Add more schema factory helper methods and refactor some of the older ones.

Also add more types!

File:
1 edited

Legend:

Unmodified
Added
Removed
  • admin-menu-editor/trunk/customizables/Schemas/SchemaFactory.php

    r3432926 r3474833  
    55use YahnisElsts\AdminMenuEditor\ProCustomizable\Settings\WithSchema\CssPropertySetting;
    66use YahnisElsts\AdminMenuEditor\ProCustomizable\Settings\WithSchema\Font;
     7use YahnisElsts\AdminMenuEditor\ProCustomizable\Settings\WithSchema;
    78
    89class SchemaFactory {
    9     public function string($label = null) {
     10    public function string($label = null): StringSchema {
    1011        return new StringSchema($label);
    1112    }
    1213
    13     public function boolean($label = null) {
     14    public function boolean($label = null): Schema {
    1415        return new Boolean($label);
    1516    }
    1617
    17     public function number($label = null) {
     18    public function number($label = null): Number {
    1819        return new Number($label);
    1920    }
    2021
    21     public function int($label = null) {
     22    public function int($label = null): Number {
    2223        return (new Number($label))->int();
    2324    }
    2425
    25     public function enum(array $values, $label = null) {
     26    public function enum(array $values, $label = null): Enum {
    2627        return (new Enum($label))->values($values);
    2728    }
     
    3132    }
    3233
    33     public function struct(array $fieldSchemas, $label = null) {
     34    public function struct(array $fieldSchemas, $label = null): Struct {
    3435        return new Struct($fieldSchemas, $label);
    3536    }
    3637
    37     public function record(Schema $keySchema, Schema $itemSchema, $label = null) {
     38    public function record(Schema $keySchema, Schema $itemSchema, $label = null): Record {
    3839        return new Record($keySchema, $itemSchema, $label);
    3940    }
     
    6667     * @return IndexedArray
    6768     */
    68     public function arr(Schema $itemSchema, $label = null) {
     69    public function arr(Schema $itemSchema, ?string $label = null): IndexedArray {
    6970        return new IndexedArray($itemSchema, $label);
    7071    }
     
    7576     * @return Union
    7677     */
    77     public function union(array $schemas, $label = null) {
     78    public function union(array $schemas, ?string $label = null): Union {
    7879        return new Union($schemas, $label);
    7980    }
    8081
    81     public function cssColor($label = null) {
    82         return (new Color($label))->orTransparent()->settingClassHint(CssPropertySetting::class);
     82    public function cssColor($label = null, ?string $cssProperty = null): Color {
     83        $params = [];
     84        if ( $cssProperty !== null ) {
     85            $params['cssProperty'] = $cssProperty;
     86        }
     87        return (new Color($label))->orTransparent()->s(CssPropertySetting::class, $params);
    8388    }
    8489
    85     public function cssFont($label = null) {
    86         return (new PlaceholderStruct($label))->settingClassHint(Font::class);
     90    public function cssLength(?string $label = null, ?string $cssProperty = null): Number {
     91        $params = [];
     92        if ( $cssProperty !== null ) {
     93            $params['cssProperty'] = $cssProperty;
     94        }
     95        return (new Number($label))->s(WithSchema\CssLengthSetting::class, $params);
     96    }
     97
     98    public function cssFont(?string $label = null, ?bool $includesLineHeight = null): Struct {
     99        $params = [];
     100        if ( $includesLineHeight !== null ) {
     101            $params['includesLineHeight'] = $includesLineHeight;
     102        }
     103        return Font::createDefaultSchema($this, $label, $params);
     104    }
     105
     106    public function cssPadding(): Struct {
     107        return WithSchema\CssBoxDimensions::createPaddingSchema($this);
     108    }
     109
     110    public function cssMargin(): Struct {
     111        return WithSchema\CssBoxDimensions::createMarginSchema($this);
     112    }
     113
     114    public function cssBorders(): Struct {
     115        return WithSchema\Borders::createDefaultSchema($this)->settingParams(['label' => 'Border']);
     116    }
     117
     118    public function cssBorderStyle(?string $label = 'Border style', bool $nullAllowed = true): Enum {
     119        $allowedValues = ['none', 'solid', 'dashed', 'dotted', 'double', 'groove', 'ridge', 'inset', 'outset'];
     120        if ( $nullAllowed ) {
     121            array_unshift($allowedValues, null);
     122        }
     123
     124        $schema = $this
     125            ->enum($allowedValues, $label)
     126            ->s(CssPropertySetting::class, ['cssProperty' => 'border-style']);
     127
     128        if ( $nullAllowed ) {
     129            $schema->defaultValue(null);
     130        }
     131        return $schema;
    87132    }
    88133
     
    95140     * @return JsonValue
    96141     */
    97     public function json($valueSchema = null, $label = null) {
     142    public function json(?Schema $valueSchema = null, ?string $label = null): JsonValue {
    98143        if ( $valueSchema === null ) {
    99144            $valueSchema = new Anything();
Note: See TracChangeset for help on using the changeset viewer.