Skip to content

Option Classes #2

@rugbymauri

Description

@rugbymauri

add Classes for Options

class ContentOptions implements \ArrayAccess
{
    private function __construct(
        private array $data = []
    ) {
    }

    public static function new(array $options = []): self
    {
        return new self($options);
    }

    public function offsetExists(mixed $offset): bool
    {
        return isset($this->options[$offset]);
    }

    public function offsetGet(mixed $offset): mixed
    {
        return $this->options[$offset];
    }

    public function offsetSet(mixed $offset, mixed $value): void
    {
        $this->options[$offset] = $value;
    }

    public function offsetUnset(mixed $offset): void
    {
        unset($this->options[$offset]);
    }

    public function setLabel(string $value): self
    {
        $this->options[Content::OPT_LABEL] = $value;
        return $this;
    }

    public function getLabel(): string
    {
        if (isset($this->options['Content::OPT_LABEL'])) {
            return $this->options['Content::OPT_LABEL'];
        }
        return 'no Label';
    }

    public function setFormatter(string $value): self
    {
        $this->options[Content::OPT_FORMATTER] = $value;
        return $this;
    }

    public function getFormatter(): string
    {
        if (isset($this->options['Content::OPT_FORMATTER'])) {
            return $this->options['Content::OPT_FORMATTER'];
        }
        return null;
    }  
}

usage:

$builder
            ->addBlock('example', null, [
                'label' => 'Example Block',
            ])
            ->addContent('firstname', null, 
               ContentOption::new()
                  ->setLabel('your Name')
           )
            ->addContent('lastname', null, [
                'label' => 'Your Lastname',
            ])
            ->addContent('company', null, [
                'label' => 'Your Company',
            ])
            ->addContent('email', null, 
          ContentOption::new()
->setLabel('Your Company')
->setFormatter(EmailFormatter::class),
            ])
        ;

Pros:

  • type safety
  • code completion (PHP native)
  • compatible to ArrayAnnotation

Cons:

  • Lot of boilerplate code
  • OptionsResolver already implemented, addtional to OptionsResolver

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions