Skip to content

Commit

Permalink
refactor!: Require PHP 7.4
Browse files Browse the repository at this point in the history
  • Loading branch information
live627 committed Mar 16, 2021
1 parent 2d38e0a commit 4abc062
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 73 deletions.
3 changes: 3 additions & 0 deletions composer.json
Expand Up @@ -8,5 +8,8 @@
"require-dev": {
"simplemachines/smf2.1": "dev-unittest",
"phpunit/phpunit": "^8.5"
},
"require": {
"php": ">=7.4"
}
}
164 changes: 97 additions & 67 deletions src/Class-CustomForm.php
@@ -1,17 +1,26 @@
<?php

/**
* @package Custom Form mod
* @version 1.0.3
* @author John Rayes <live627@gmail.com>
* @copyright Copyright (c) 2014, John Rayes
* @license http://opensource.org/licenses/MIT MIT
*/

declare(strict_types=1);

interface CustomForm
{
/*
* Constructs the field.
*
* @param array $field The field as returned by {@link total_getCustomForm()}.
* @param string $value Field value.
* @param bool $exists Whether the value exists/is not empty.
*
* @access public
*/
public function __construct($field, $value, $exists);
public function __construct(array $field, string $value);

/*
* Sets the input so the user can enter a value.
Expand All @@ -20,58 +29,55 @@ public function __construct($field, $value, $exists);
* @access public
*/
public function setHtml();
public function validate();

/*
* Gets the value. This method should handle if a specific field type must be sanitized.
*
* @access public
* @return string
*/
public function getValue(): string;
public function validate(): bool;
}

abstract class CustomFormBase implements CustomForm
{
public $input_html;
public $output_html;
protected $field;
protected $err = false;
protected $exists = false;
protected $type_vars = array();
protected $value = '';
protected $size = 0;
protected $default = '';
protected $required = false;

public function __construct($field, $value, $exists)
public string $input_html;
public string $output_html;
protected array $field;
protected array $err = [];
protected bool $exists = false;
protected array $type_vars = array();
protected string $value = '';
protected int $size = 0;
protected string $default = '';
protected bool $required = false;

public function __construct(array $field, string $value)
{
$this->field = $field;
$this->value = $value;
$this->exists = $exists;
$this->exists = !empty($value);
}

/*
* Gets the error generated by the validation method.
*
* @access public
* @return string|false The error string or false for no error.
* @return array
*/
public function getError()
public function getError(): array
{
return $this->err;
}

/*
* Gets the value. This method may be overridden if a specific field type must be sanitized.
*
* @access public
* @return string
*/
public function getValue()
{
return $this->value;
}

/**
* Returns the input so the user can enter a value.
*
* @access public
* @return mixed
* @return string
*/
public function getInputHtml()
public function getInputHtml(): string
{
return $this->input_html;
}
Expand All @@ -80,9 +86,9 @@ public function getInputHtml()
* Returns the output. It's the field's value formatted acccording to its criteria.
*
* @access public
* @return mixed
* @return string
*/
public function getOutputHtml()
public function getOutputHtml(): string
{
return $this->output_html;
}
Expand Down Expand Up @@ -118,52 +124,64 @@ public function setHtml()
{
global $txt;
$true = (!$this->exists && $this->default) || $this->value;
$this->input_html = '<input type="checkbox" name="CustomFormField[' . $this->field['id_field'] . ']"' . ($true ? ' checked' : '') . '>';
$this->output_html = $true ? $txt['yes'] : $txt['no'];
$this->input_html .= sprintf(
'<input type="checkbox" name="%s[$s]"%s>',
'CustomFormField',
$this->field['id_field'],
$true ? ' checked' : ''
);
$this->output_html = $txt[$this->getValue()];
}
public function validate()
public function validate(): bool
{
// Nothing needed here, really. It's just a get out of jail free card. "This card may be kept until needed, or sold."
// Nothing needed here, really. It's just a get out of jail
// free card. "This card may be kept until needed, or sold."
return true;
}
public function getValue()
public function getValue(): string
{
return (!$this->exists && $this->default) || $this->value;
return (!$this->exists && $this->default) || $this->value ? 'yes' : 'no';
}
}

class CustomForm_select extends CustomFormBase
{
public function setHtml()
{
$this->input_html = '<select name="CustomFormField[' . $this->field['id_field'] . ']" style="width: 90%;">';
$this->input_html = sprintf(
'<select name="%s[$s]" style="width: 90%;">',
'CustomFormField',
$this->field['id_field']
);
foreach ($this->type_vars as $v)
{
$true = (!$this->exists && $this->default == $v) || $this->value == $v;
$this->input_html .= '<option' . ($true ? ' selected="selected"' : '') . '>' . $v . '</option>';
$this->input_html .= sprintf(
'<option%s> %s</option>',
(!$this->exists && $this->default == $v) || $this->value == $v
? ' checked="checked"'
: '',
$v
);
if ($true)
$this->output_html = $v;
}

$this->input_html .= '</select>';
}
public function validate()
public function validate(): bool
{
$found = false;
$opts = array_flip($this->type_vars);
if (isset($this->value, $opts[$this->value]))
$found = true;
$found = isset(array_flip($this->type_vars)[$this->value]);

if (!$found && $this->required)
$this->err = array('pf_invalid_value', $this->field['name']);

return $found;
}
public function getValue()
public function getValue(): string
{
$value = $this->default;
$opts = array_flip($this->type_vars);
if (isset($this->value, $opts[$this->value]))
$value = $this->value;

return $value;
return isset(array_flip($this->type_vars)[$this->value])
? $this->value
: $this->default;
}
}

Expand All @@ -174,8 +192,15 @@ public function setHtml()
$this->input_html = '<fieldset>';
foreach ($this->type_vars as $v)
{
$true = (!$this->exists && $this->default == $v) || $this->value == $v;
$this->input_html .= '<label><input type="radio" name="CustomFormField[' . $this->field['id_field'] . ']"' . ($true ? ' checked="checked"' : '') . '> ' . $v . '</label><br>';
$this->input_html .= sprintf(
'<label><input type="radio" name="%s[$s]"%s> %s</label><br>',
'CustomFormField',
$this->field['id_field'],
(!$this->exists && $this->default == $v) || $this->value == $v
? ' checked="checked"'
: '',
$v
);
if ($true)
$this->output_html = $v;
}
Expand All @@ -188,9 +213,14 @@ class CustomForm_text extends CustomFormBase
public function setHtml()
{
$this->output_html = $this->value;
$this->input_html = '<input type="text" name="CustomFormField[' . $this->field['id_field'] . ']" style="width: 90%;" value="' . $this->value . '">';
$this->input_html = sprintf(
'<input type="text" name="%s[$s]" style="width: 90%;" value="%s">',
'CustomFormField',
$this->field['id_field'],
$this->value
);
}
public function getValue()
public function getValue(): string
{
global $smcFunc;
if (!empty($this->size))
Expand All @@ -201,7 +231,7 @@ public function getValue()

return $this->value;
}
public function validate()
public function validate(): bool
{
if ($this->exists && $this->required)
$this->err = array('pf_invalid_value', $this->field['name']);
Expand All @@ -211,7 +241,7 @@ public function validate()
//~ fatal_error('Mask "' . $this->field['mask'] . '" not found for field "' . $this->field['name'] . '" at ID #' . $this->field['id_field'] . '.', false);

//~ $mask = new $class_name($this->value, $this->field);
//~ $mask->validate();
//~ $mask->validate(): bool;
//~ if (false !== ($err = $mask->getError()))
//~ $this->err = $err;
}
Expand All @@ -223,14 +253,14 @@ public function setHtml()
{
$this->output_html = $this->value;
@list ($rows, $cols) = @explode(',', $this->default);
$this->input_html = '<textarea name="CustomFormField[' . $this->field['id_field'] . ']" ' . (!empty($rows) ? 'rows="' . $rows . '"' : '') . ' ' . (!empty($cols) ? 'cols="' . $cols . '"' : '') . '>' . $this->value . '</textarea>';
$this->input_html = '<textarea name="%s[$s]" ' . (!empty($rows) ? 'rows="' . $rows . '"' : '') . ' ' . (!empty($cols) ? 'cols="' . $cols . '"' : '') . '>' . $this->value . '</textarea>';
}
}

interface CustomFormFieldMask
{
public function __construct($value, $field);
public function validate();
public function validate(): bool;
}

abstract class CustomFormFieldMaskBase implements CustomFormFieldMask
Expand All @@ -253,7 +283,7 @@ public function getError()

class CustomFormFieldMask_email extends CustomFormFieldMaskBase
{
public function validate()
public function validate(): bool
{
if (!preg_match('~^[0-9A-Za-z=_+\-/][0-9A-Za-z=_\'+\-/\.]*@[\w\-]+(\.[\w\-]+)*(\.[\w]{2,6})$~', $this->value))
$this->err = array('pf_invalid_value', $this->field['name']);
Expand All @@ -262,7 +292,7 @@ public function validate()

class CustomFormFieldMask_regex extends CustomFormFieldMaskBase
{
public function validate()
public function validate(): bool
{
if (!preg_match($this->field['regex'], $this->value))
if (!empty($this->field['err']))
Expand All @@ -274,7 +304,7 @@ public function validate()

class CustomFormFieldMask_number extends CustomFormFieldMaskBase
{
public function validate()
public function validate(): bool
{
if (!preg_match('/^\s*([0-9]+)\s*$/', $this->value))
$this->err = array('pf_invalid_value', $this->field['name']);
Expand All @@ -283,7 +313,7 @@ public function validate()

class CustomFormFieldMask_float extends CustomFormFieldMaskBase
{
public function validate()
public function validate(): bool
{
if (!preg_match('/^\s*([0-9]+(\.[0-9]+)?)\s*$/', $this->value))
$this->err = array('pf_invalid_value', $this->field['name']);
Expand All @@ -292,7 +322,7 @@ public function validate()

class CustomFormFieldMask_nohtml extends CustomFormFieldMaskBase
{
public function validate()
public function validate(): bool
{
if (strip_tags($this->value) != $this->value)
$this->err = array('pf_invalid_value', $this->field['name']);
Expand Down
5 changes: 2 additions & 3 deletions src/CustomForm.php
Expand Up @@ -114,10 +114,9 @@ function CustomForm()

$type = new $class_name($field, $value, !empty($value));
$type->setOptions();
$type->validate();
if (false !== ($err = $type->getError()))
if (!$type->validate())
{
$post_errors[] = $err;
$post_errors[] = $type->getError();
// Do the 'fail form/field' stuff.
$data[$i]['failed'] = true;
$fail_submit = true;
Expand Down
8 changes: 5 additions & 3 deletions src/Subs-CustomForm.php
Expand Up @@ -8,20 +8,22 @@
* @license http://opensource.org/licenses/MIT MIT
*/

function customform_actions(&$action_array)
declare(strict_types=1);

function customform_actions(array &$action_array)
{
$action_array['form'] = array('CustomForm.php', 'CustomForm');
}

function customform_admin_areas(&$admin_areas)
function customform_admin_areas(array &$admin_areas)
{
global $txt;

loadLanguage('CustomForm');
$admin_areas['config']['areas']['modsettings']['subsections']['customform'] = array($txt['customform_tabheader']);
}

function customform_modify_modifications(&$sub_actions)
function customform_modify_modifications(array &$sub_actions)
{
$sub_actions['customform'] = 'ModifyCustomFormSettings';
}
Expand Down

0 comments on commit 4abc062

Please sign in to comment.