Skip to content

Commit

Permalink
Changed building syntax (removed need to pass through builder).
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Robert committed Feb 24, 2022
1 parent 5369059 commit 4ee9fb6
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 25 deletions.
23 changes: 19 additions & 4 deletions Chase/Safari/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ public function __construct(array $request)

public function __set($name, $value)
{
if($name === 'request'){
if(!is_array($value)){
if ($name === 'request') {
if (!is_array($value)) {
throw new \Error("Form attribute 'request' must have an array value.");
}
$request = Utils::cleanGlobal($value);
$this->builder = new ElementBuilder($request);
}else{
} else {
$this->attributes[$name] = $value;
}
}
}

public function __get($name)
Expand All @@ -46,6 +46,21 @@ public function __get($name)
return null;
}

public function __call($name, $arguments)
{
$rc = new \ReflectionClass(ElementBuilder::class);
$methods = array_map(function ($rm) {
return $rm->name;
}, array_filter($rc->getMethods(\ReflectionMethod::IS_PUBLIC), function ($rm) {
return (strpos($rm->name, '_') !== 0);
}));
if (in_array($name, $methods)) {
return $this->builder->$name(...$arguments);
} else {
throw new \Error(sprintf("Unknowned method %s::%s()", ElementBuilder::class, $name));
}
}

/**
* Set an attribute on the form.
*
Expand Down
83 changes: 62 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,19 @@ class LoginForm extends Form
$this->class = "";
}

public function elements(){
public function elements() : array {
return [
$this->builder->input([
$this->input([
'type' => 'text',
'name' => 'username',
'placeholder' => 'Username'
]),
$this->builder->input([
$this->input([
'type' => 'password',
'name' => 'password',
'placeholder'=>'Password'
]),
$this->builder->input([
$this->input([
'type' => 'submit',
'value' => 'SUBMIT'
])
Expand All @@ -76,6 +76,41 @@ $login = new LoginForm($_GET);
echo $login->with("method", "GET")->with("action", "/")->render()
```

## Note
The form building methods used in the `Chase\Safari\Form::elements()` method (`input`, `raw`, e.t.c) are not actually implemented on the class itself. The are methods of the `Chase\Safari\ElementBuilder` class.

Each `Chase\Safari\Form` has an attribute called `builder`, which is an instance of `Chase\Safari\ElementBuilder`.

Using the magic method `Chase\Safari\Form::__call`, all calls to the builder methods are tranfered to it.

TLDR;

Instead of this

```php
//Snippet
public function elements() : array {
return [
$this->input([
// Input parameters
])
];
}
```
You can do this

```php
public function elements() : array {
return [
$this->builder->input([
// Input parameters
])
];
}
```

They are both equivalent.

## Code Sample
```php
<?php
Expand All @@ -97,49 +132,55 @@ class SampleForm extends Form
public function elements(): array
{
return [
$this->builder->raw('<br>'),
$this->raw('<br>'),

$this->builder->input([
$this->input([
'type' => 'text',
'name' => 'username',
'placeholder' => 'Username'
]),

$this->builder->raw('<br>'),
$this->raw('<br>'),

$this->builder->radio([
$this->radio([
'name' => 'radioname',
'value' => 'radiovalue'
]),

$this->builder->raw('<br>'),
$this->raw('<label>Radio display</label>'),

$this->raw('<br>'),

$this->builder->checkbox([
$this->checkbox([
'name' => 'username',
'value' => 'checkvalue'
]),

$this->builder->textarea([
$this->raw('<label>Checkbox display</label>'),

$this->raw('<br>'),

$this->textarea([
'name' => 'textareaname',
]),

$this->builder->raw('<br>'),
$this->raw('<br>'),

$this->builder->select(
// Select attributes
$this->select(

// Select tag attributes.
['name' => 'selectname'],

// Options
['optionValue' => 'displayName', 'anotherOptionValue' => 'displayName'],

// Default option. This is optional.
// Option tags. The array keys are the option values, the array values are the option display.
['inputValue' => 'defaultDisplayName', 'anotherInputValue' => 'anotherDisplayName'],

// Default option. This argument is optional.
'inputValue'
),

$this->builder->raw('<br>'),
$this->raw('<br>'),

$this->builder->input([
$this->input([
'type' => 'submit',
'value' => 'SUBMIT'
])
Expand Down

0 comments on commit 4ee9fb6

Please sign in to comment.