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

Option Classes #2

Open
rugbymauri opened this issue Aug 3, 2023 · 0 comments
Open

Option Classes #2

rugbymauri opened this issue Aug 3, 2023 · 0 comments
Labels
enhancement New feature or request

Comments

@rugbymauri
Copy link
Contributor

rugbymauri commented Aug 3, 2023

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
@rugbymauri rugbymauri added the enhancement New feature or request label Aug 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant