Skip to content

Commit

Permalink
TRY
Browse files Browse the repository at this point in the history
  • Loading branch information
live627 committed Feb 20, 2025
1 parent 18d5ca9 commit bfca74e
Show file tree
Hide file tree
Showing 14 changed files with 332 additions and 92 deletions.
8 changes: 4 additions & 4 deletions src/CustomForm/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ class Field
*******************/

/**
* @var array The field object based on its type.
* @var FieldInterface|null The field object based on its type.
*/
public array $obj;
public ?FieldInterface $obj;

/****************
* Public methods
Expand Down Expand Up @@ -74,7 +74,7 @@ public function __construct(
);
}

$this->obj = new $class_name((array) $this, $_POST['CustomFormField'][$this->id] ?? '');
$this->obj = new $class_name($this, $_POST['CustomFormField'][$this->id] ?? '');
}

/**
Expand Down Expand Up @@ -147,4 +147,4 @@ public static function fetchManyForAdmin(?array $ids): \Generator
);
}
}
}
}
2 changes: 1 addition & 1 deletion src/CustomForm/FieldInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ interface FieldInterface
* @param array $field The field as returned by {@link total_getCustomForm()}.
* @param string $value Field value.
*/
public function __construct(array $field, string $value);
public function __construct(Field $field, string $value);

/*
* Sets the input so the user can enter a value.
Expand Down
6 changes: 3 additions & 3 deletions src/CustomForm/FieldTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ trait FieldTrait

protected bool $required = false;

public function __construct(protected readonly array $field, protected readonly string $value)
public function __construct(protected readonly Field $field, protected readonly string $value)
{
$this->exists = $value != '';

$temp = $this->field['type_vars'] != ''
? array_map('trim', explode(',', $this->field['type_vars']))
$temp = $this->field->type_vars != ''
? array_map('trim', explode(',', $this->field->type_vars))
: [];

if ($temp != []) {
Expand Down
4 changes: 2 additions & 2 deletions src/CustomForm/Fields/Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function setHtml(): void
$this->input_html = sprintf(
'<input type="checkbox" name="%s[%d]"%s>',
'CustomFormField',
$this->field['id'],
$this->field->id,
$true ? ' checked' : '',
);
$this->output_html = '{{ ' . $this->getValue() . ' }}';
Expand All @@ -36,7 +36,7 @@ public function setHtml(): void
public function validate(): bool
{
if (!$this->exists && $this->required) {
$this->err = ['customform_invalid_value', $this->field['text']];
$this->err = ['customform_invalid_value', $this->field->text];
}

return $this->err == [];
Expand Down
2 changes: 1 addition & 1 deletion src/CustomForm/Fields/Radio.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function setHtml(): void
$this->input_html .= sprintf(
'<label><input type="radio" name="%s[%d]" value="%4$s"%s> %s</label><br>',
'CustomFormField',
$this->field['id'],
$this->field->id,
(!$this->exists && $this->default == $v) || $this->value == $v
? ' checked="checked"'
: '',
Expand Down
4 changes: 2 additions & 2 deletions src/CustomForm/Fields/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function setHtml(): void
$this->input_html = sprintf(
'<select name="%s[%d]">',
'CustomFormField',
$this->field['id'],
$this->field->id,
);

foreach ($this->type_vars as $v) {
Expand All @@ -49,7 +49,7 @@ public function validate(): bool
$found = isset(array_flip($this->type_vars)[$this->value]) || !empty($this->default);

if (!$found && $this->required) {
$this->err = ['customform_invalid_value', $this->field['text']];
$this->err = ['customform_invalid_value', $this->field->text];
}

return $this->err == [];
Expand Down
8 changes: 4 additions & 4 deletions src/CustomForm/Fields/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function setHtml(): void
$this->input_html = sprintf(
'<input type="text" name="%s[%d]" value="%s">',
'CustomFormField',
$this->field['id'],
$this->field->id,
$this->value,
);
}
Expand All @@ -47,12 +47,12 @@ public function getValue(): string
public function validate(): bool
{
if (!$this->exists && $this->required) {
$this->err = ['customform_invalid_value', $this->field['text']];
$this->err = ['customform_invalid_value', $this->field->text];
}

//~ $class_name = 'CustomFormFieldMask_' . $this->field['mask'];
//~ $class_name = 'CustomFormFieldMask_' . $this->field->mask;
//~ if (!class_exists($class_name))
//~ fatal_error('Mask "' . $this->field['mask'] . '" not found for field "' . $this->field['name'] . '" at ID #' . $this->field['id'] . '.', false);
//~ fatal_error('Mask "' . $this->field->mask . '" not found for field "' . $this->field->name . '" at ID #' . $this->field->id . '.', false);

//~ $mask = new $class_name($this->value, $this->field);
//~ $mask->validate(): bool;
Expand Down
2 changes: 1 addition & 1 deletion src/CustomForm/Fields/Textarea.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function setHtml(): void
$this->input_html = sprintf(
'<textarea name="%s[%d]" %s%s>%s</textarea>',
'CustomFormField',
$this->field['id'],
$this->field->id,
!empty($rows) ? 'rows="' . $rows . '"' : '',
!empty($cols) ? 'cols="' . $cols . '"' : '',
$this->value,
Expand Down
13 changes: 5 additions & 8 deletions src/CustomForm/Output/ForumPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,23 @@

namespace CustomForm\Output;

use CustomForm\Form;
use CustomForm\OutputInterface;
use SMF\Msg;
use SMF\User;
use SMF\Utils;
use CustomForm\{Form, OutputInterface};
use SMF\{Msg, User, Utils};

class ForumPost implements OutputInterface
{
public function send(string $subject, string $output, Form $form_data): void
public function send(string $subject, string $output, Form $form): void
{
$msgOptions = [
'id' => 0,
'subject' => Utils::htmlspecialchars($subject),
'icon' => $form_data['icon'],
'icon' => $form->icon,
'body' => Utils::htmlspecialchars($output),
'smileys_enabled' => true,
];
$topicOptions = [
'id' => 0,
'board' => $form_data['id_board'],
'board' => $form->board_id,
'mark_as_read' => true,
];
$posterOptions = [
Expand Down
2 changes: 1 addition & 1 deletion src/CustomForm/OutputInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@

interface OutputInterface
{
public function send(string $subject, string $output, Form $form_data): void;
public function send(string $subject, string $output, Form $form): void;
}
120 changes: 58 additions & 62 deletions tests/CustomFormTest.php → tests/CustomFormTest.phpe
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ class CustomFormTest extends TestCase
{
public function testCustomFormBaseConstructor(): void
{
$field = [
'id_field' => 1,
'type_vars' => 'size=10,required',
'text' => 'Test Field',
];
$$field = new CustomForm\Field(
id: 1,
type_vars: 'size=10,required',
text: 'Test Field',
);
$value = 'Sample Value';

$form = new CustomForm\Fields\Info($field, $value);
Expand All @@ -23,11 +23,11 @@ public function testCustomFormBaseConstructor(): void

public function testCustomFormInfoSetHtml(): void
{
$field = [
'id_field' => 1,
'type_vars' => '',
'text' => 'Test Info',
];
$$field = new CustomForm\Field(
id: 1,
type_vars: '',
text: 'Test Info',
);
$value = 'Information';

$form = new CustomForm\Fields\Info($field, $value);
Expand All @@ -38,19 +38,15 @@ public function testCustomFormInfoSetHtml(): void

public function testCustomFormCheckValidation(): void
{
$field = [
'id_field' => 2,
'type_vars' => 'required',
'text' => 'Test Checkbox',
];
$$field = new CustomForm\Field(
id: 2,
type_vars: 'required',
text: 'Test Checkbox',
);
$value = ''; // Simulate an unchecked box

$form = new CustomForm\Fields\Check($field, $value);

// Mock the $txt array
$txt = ['yes' => 'Yes', 'no' => 'No'];
$GLOBALS['txt'] = $txt;

$form->setHtml();
$this->assertFalse($form->validate());
$this->assertNotEmpty($form->getError());
Expand All @@ -62,13 +58,13 @@ public function testCustomFormCheckValidation(): void
public function testCheckbox(string $type_vars, string $value, string $expected): void
{
$type = new CustomForm\Fields\Check(
[
'id_field' => '',
'title' => '',
'text' => '',
'type' => '',
'type_vars' => $type_vars,
],
CustomForm\Field(
id: '',
title: '',
text: '',
type: '',
type_vars: $type_vars,
),
$value,
);
$this->assertTrue($type->validate());
Expand All @@ -91,13 +87,13 @@ public function checkboxProvider(): array
public function testselect(string $type_vars, string $value, string $expected): void
{
$type = new CustomForm\Fields\Select(
[
'id_field' => '',
'title' => '',
'text' => '',
'type' => '',
'type_vars' => $type_vars,
],
CustomForm\Field(
id: '',
title: '',
text: '',
type: '',
type_vars: $type_vars,
),
$value,
);
$this->assertTrue($type->validate());
Expand All @@ -119,11 +115,11 @@ public function selectProvider(): array

public function testCustomFormSelectSetHtml(): void
{
$field = [
'id_field' => 3,
'type_vars' => 'Option1,Option2,Option3',
'text' => 'Test Select',
];
$$field = new CustomForm\Field(
id: 3,
type_vars: 'Option1,Option2,Option3',
text: 'Test Select',
);
$value = 'Option2';

$form = new CustomForm\Fields\Select($field, $value);
Expand All @@ -135,11 +131,11 @@ public function testCustomFormSelectSetHtml(): void

public function testCustomFormRadioSetHtml(): void
{
$field = [
'id_field' => 4,
'type_vars' => 'Yes,No,Maybe',
'text' => 'Test Radio',
];
$$field = new CustomForm\Field(
id: 4,
type_vars: 'Yes,No,Maybe',
text: 'Test Radio',
);
$value = 'Yes';

$form = new CustomForm\Fields\Radio($field, $value);
Expand All @@ -151,11 +147,11 @@ public function testCustomFormRadioSetHtml(): void

public function testCustomFormTextSetHtml(): void
{
$field = [
'id_field' => 5,
'type_vars' => 'size=20,default=Sample Text',
'text' => 'Test Text',
];
$$field = new CustomForm\Field(
id: 5,
type_vars: 'size=20,default=Sample Text',
text: 'Test Text',
);
$value = '';

$form = new CustomForm\Fields\Text($field, $value);
Expand All @@ -167,10 +163,10 @@ public function testCustomFormTextSetHtml(): void

public function testCustomFormFieldMaskEmail(): void
{
$field = [
'id_field' => 6,
'text' => 'Test Email',
];
$$field = new CustomForm\Field(
id: 6,
text: 'Test Email',
);
$value = 'invalid-email';

$mask = new CustomForm\Fields\Masks\Email($value, $field);
Expand All @@ -181,11 +177,11 @@ public function testCustomFormFieldMaskEmail(): void

public function testCustomFormFieldMaskRegex(): void
{
$field = [
'id_field' => 7,
'text' => 'Test Regex',
'regex' => '/^[A-Za-z]+$/',
];
$$field = new CustomForm\Field(
id: 7,
text: 'Test Regex',
regex: '/^[A-Za-z]+$/',
);
$value = 'Invalid123';

$mask = new CustomForm\Fields\Masks\Regex($value, $field);
Expand All @@ -196,15 +192,15 @@ public function testCustomFormFieldMaskRegex(): void

public function testCustomFormFieldMaskNumber(): void
{
$field = [
'id_field' => 8,
'text' => 'Test Number',
];
$$field = new CustomForm\Field(
id: 8,
text: 'Test Number',
);
$value = '12345';

$mask = new CustomForm\Fields\Masks\Number($value, $field);

$this->assertTrue($mask->validate());
$this->assertEmpty($mask->getError());
}
}
}
Loading

0 comments on commit bfca74e

Please sign in to comment.