Skip to content

Commit

Permalink
PSR-12 Compliant
Browse files Browse the repository at this point in the history
  • Loading branch information
sam committed Sep 2, 2021
1 parent 02be038 commit 8407ab1
Show file tree
Hide file tree
Showing 3 changed files with 430 additions and 299 deletions.
96 changes: 51 additions & 45 deletions Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,52 +4,58 @@

class Element
{
const VOID_ELEMENTS = ['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'param', 'source', 'track', 'wbr'];

protected $tagName = '';
protected $attributeList = [];
protected $classList = [];
protected $innerContent = '';

public function is_void()
{
return in_array($this->tagName, self::VOID_ELEMENTS);
}

public function __construct($tagName, $innerContent=null, $attributeList = [])
{
$this->tagName = $tagName;
$this->attributeList = $attributeList;
$this->innerContent = $innerContent ?? '';
}

private function format_attributes()
{
$ret = '';

foreach($this->attributeList as $k => $v)
if(!is_null($v) && $v !== '' && !is_array($v))
$ret .= is_int($k) ? " $v" : sprintf(' %s="%s"', $k, $v);

return $ret;
}

public function __toString()
{
$flattributes = $this->format_attributes();

$ret = '';
if($this->is_void())
$ret = sprintf('<%s%s/>', $this->tagName, $flattributes);
else
const VOID_ELEMENTS = [
'area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input',
'link', 'meta', 'param', 'source', 'track', 'wbr'
];

protected $tagName = '';
protected $attributeList = [];
protected $classList = [];
protected $innerContent = '';

public function isVoid()
{
return in_array($this->tagName, self::VOID_ELEMENTS);
}

public function __construct($tagName, $innerContent = null, $attributeList = [])
{
$this->tagName = $tagName;
$this->attributeList = $attributeList;
$this->innerContent = $innerContent ?? '';
}

private function formatAttributes()
{
$ret = sprintf('<%s%s>%s</%s>',
$this->tagName,
$flattributes,
$this->innerContent,
$this->tagName);
$ret = '';

foreach ($this->attributeList as $k => $v) {
if (!is_null($v) && $v !== '' && !is_array($v)) {
$ret .= is_int($k) ? " $v" : sprintf(' %s="%s"', $k, $v);
}
}

return $ret;
}

return $ret;
}
public function __toString()
{
$flattributes = $this->formatAttributes();

$ret = '';
if ($this->isVoid()) {
$ret = sprintf('<%s%s/>', $this->tagName, $flattributes);
} else {
$ret = sprintf(
'<%s%s>%s</%s>',
$this->tagName,
$flattributes,
$this->innerContent,
$this->tagName
);
}

return $ret;
}
}
181 changes: 89 additions & 92 deletions Form.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace HexMakina\Marker;

/**
Expand All @@ -7,115 +8,111 @@
class Form
{

public static function __callStatic($element_type, $arguments)
{
// arguments [name, value, [attributes], [errors]]
$i=0;
$field_name= $arguments[$i++] ?? null;
$field_value= $arguments[$i++] ?? null;
$attributes= $arguments[$i++] ?? [];
$errors= $arguments[$i++] ?? [];


$attributes['type'] = $element_type;
$attributes['name'] = $attributes['name'] ?? $field_name;
$attributes['value'] = $attributes['value'] ?? $field_value;

switch($attributes['type'])
public static function __callStatic($element_type, $arguments)
{
case 'datetime':
$attributes['type'] = 'datetime-local';
break;

case 'password':
$attributes['value'] = '';
break;
// arguments [name, value, [attributes], [errors]]
$i = 0;
$field_name = $arguments[$i++] ?? null;
$field_value = $arguments[$i++] ?? null;
$attributes = $arguments[$i++] ?? [];
$errors = $arguments[$i++] ?? [];


$attributes['type'] = $element_type;
$attributes['name'] = $attributes['name'] ?? $field_name;
$attributes['value'] = $attributes['value'] ?? $field_value;

switch ($attributes['type']) {
case 'datetime':
$attributes['type'] = 'datetime-local';
break;

case 'password':
$attributes['value'] = '';
break;
}
return self::input($field_name, $field_value, $attributes, $errors);
}
return self::input($field_name, $field_value, $attributes, $errors);

}

public static function input($field_name, $field_value=null, $attributes=[], $errors=[])
{

if(isset($attributes['disabled']) || !isset($attributes['type']) || in_array('disabled', $attributes, true))
$attributes['type'] = 'text';
else
$attributes['type'] = $attributes['type'] ?? 'text';

$attributes['name'] = $attributes['name'] ?? $field_name;
$attributes['value'] = $attributes['value'] ?? $field_value;
public static function input($field_name, $field_value = null, $attributes = [], $errors = [])
{

return self::element_with_errors('input', null, $attributes, isset($errors[$field_name]));
}
if (isset($attributes['disabled']) || !isset($attributes['type']) || in_array('disabled', $attributes, true)) {
$attributes['type'] = 'text';
} else {
$attributes['type'] = $attributes['type'] ?? 'text';
}

public static function textarea($field_name, $field_value=null, $attributes=[], $errors=[])
{
$attributes['name'] = $attributes['name'] ?? $field_name;
return self::element_with_errors('textarea', $field_value, $attributes, isset($errors[$field_name]));
}
$attributes['name'] = $attributes['name'] ?? $field_name;
$attributes['value'] = $attributes['value'] ?? $field_value;

public static function select($field_name, $option_list, $selected = null, $attributes=[], $errors=[])
{
$attributes['name'] = $attributes['name'] ?? $field_name;
return self::elementWithErrors('input', null, $attributes, isset($errors[$field_name]));
}

$options = '';
foreach($option_list as $value => $label)
public static function textarea($field_name, $field_value = null, $attributes = [], $errors = [])
{
$option_attributes = ['value' => $value];
if($selected == $value)
$option_attributes['selected'] = 'selected';

$options .= new Element('option', $label, $option_attributes);
$attributes['name'] = $attributes['name'] ?? $field_name;
return self::elementWithErrors('textarea', $field_value, $attributes, isset($errors[$field_name]));
}
return self::element_with_errors('select', $options, $attributes, isset($errors[$field_name]));
}

public static function legend($label, $attributes=[])
{
return new Element('legend', $label, $attributes);
}

public static function label($field_for, $field_label, $attributes=[], $errors=[])
{
$attributes['for'] = $field_for;
return self::element_with_errors('label', $field_label, $attributes, isset($errors[$field_for]));
}

public static function submit($field_id, $field_label, $attributes=[])
{
$attributes['id'] = $attributes['id'] ?? $field_id;
unset($attributes['name']);
$attributes['type'] = 'submit';
if(isset($attributes['tag']) && $attributes['tag'] === 'input')
return new Element('input', '', $attributes);
else

public static function select($field_name, $option_list, $selected = null, $attributes = [], $errors = [])
{
unset($attributes['tag']);
unset($attributes['value']);
return new Element('button', $field_label, $attributes);
$attributes['name'] = $attributes['name'] ?? $field_name;

$options = '';
foreach ($option_list as $value => $label) {
$option_attributes = ['value' => $value];
if ($selected == $value) {
$option_attributes['selected'] = 'selected';
}

$options .= new Element('option', $label, $option_attributes);
}
return self::elementWithErrors('select', $options, $attributes, isset($errors[$field_name]));
}
}

private static function element_with_errors($tag, $content, $attributes=[], $errors=false)
{

$attributes['id'] = $attributes['id'] ?? $attributes['name'] ?? '';
public static function legend($label, $attributes = [])
{
return new Element('legend', $label, $attributes);
}

if($errors === true)
public static function label($field_for, $field_label, $attributes = [], $errors = [])
{
$attributes['class'] = $attributes['class'] ?? '';
$attributes['class'] .= ' error';
$attributes['for'] = $field_for;
return self::elementWithErrors('label', $field_label, $attributes, isset($errors[$field_for]));
}

$label = '';
if(isset($attributes['label']))
public static function submit($field_id, $field_label, $attributes = [])
{
$label = self::label($attributes['id'], $attributes['label'], [], $errors);
unset($attributes['label']);
$attributes['id'] = $attributes['id'] ?? $field_id;
unset($attributes['name']);
$attributes['type'] = 'submit';
if (isset($attributes['tag']) && $attributes['tag'] === 'input') {
return new Element('input', '', $attributes);
} else {
unset($attributes['tag']);
unset($attributes['value']);
return new Element('button', $field_label, $attributes);
}
}

// vd($attributes, $tag);
return $label.(new Element($tag, $content, $attributes));
}
private static function elementWithErrors($tag, $content, $attributes = [], $errors = false)
{

$attributes['id'] = $attributes['id'] ?? $attributes['name'] ?? '';

if ($errors === true) {
$attributes['class'] = $attributes['class'] ?? '';
$attributes['class'] .= ' error';
}

$label = '';
if (isset($attributes['label'])) {
$label = self::label($attributes['id'], $attributes['label'], [], $errors);
unset($attributes['label']);
}

// vd($attributes, $tag);
return $label . (new Element($tag, $content, $attributes));
}
}
Loading

0 comments on commit 8407ab1

Please sign in to comment.