Skip to content

Commit 88bf897

Browse files
committed
First release
1 parent 48bc6ad commit 88bf897

File tree

7 files changed

+174
-0
lines changed

7 files changed

+174
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/vendor/

composer.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "dekyfin/php-array-forms",
3+
"description": "A package to create HTML FORMS from PHP Array",
4+
"type": "library",
5+
"authors": [
6+
{
7+
"name": "Desmond Kyeremeh",
8+
"email": "dekyfin@gmail.com"
9+
}
10+
],
11+
"minimum-stability": "dev",
12+
"require": {
13+
"php": ">=5.3.0"
14+
},
15+
"autoload": {
16+
"psr-0": {
17+
"DF": "src/"
18+
}
19+
}
20+
}

composer.lock

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/formsList.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/**
4+
* This files contains the list of forms defined
5+
*
6+
*/
7+
8+
// Renderable array with a form elements.
9+
$contact_form = array(
10+
'name' => array(
11+
'title' => 'Name',
12+
'type' => 'text',
13+
'validations' => array('not_empty'),
14+
),
15+
'email' => array(
16+
'title' => 'Email',
17+
'type' => 'email',
18+
'validations' => array('not_empty', 'is_valid_email'),
19+
),
20+
'comment' => array(
21+
'title' => 'Comments',
22+
'type' => 'textarea',
23+
'validations' => array('not_empty'),
24+
),
25+
'submit' => array(
26+
'title' => 'Submit me!',
27+
'type' => 'submit',
28+
),
29+
);

example/index.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
require_once 'formsList.php';
4+
require_once 'classes/Form.class.php';
5+
6+
$form = new Form($contact_form);
7+
$form_html = $form->build();
8+
?>
9+
10+
<!DOCTYPE html>
11+
<html>
12+
<head>
13+
<title>The index file</title>
14+
</head>
15+
<body>
16+
<?php echo $form_html; ?>
17+
</body>
18+
</html>
19+

src/DF/ArrayForm.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
/**
4+
* ArrayForm Class
5+
*
6+
* Responsible for building forms
7+
*
8+
* @param array $elements renderable array containing form elements
9+
*
10+
* @return void
11+
*/
12+
namespace DF;
13+
14+
class ArrayForm {
15+
16+
public $elements;
17+
public $form_number = 1;
18+
19+
public function __construct( $elements ){
20+
$this->elements = $elements;
21+
}
22+
23+
/**
24+
* Form class method to dump object elements
25+
*
26+
* The method just dumps the elements of the form passed to the instantiation.
27+
*
28+
* @return void
29+
*
30+
*/
31+
public function dumpData() {
32+
var_dump($this->elements);
33+
}
34+
35+
/**
36+
* Form class method to build a form from an array
37+
*
38+
*
39+
* @return string $output contains the form as HTML
40+
*
41+
*/
42+
function build() {
43+
$output = '';
44+
45+
// For multiple forms, create a counter.
46+
$this->form_number++;
47+
48+
// Loop through each form element and render it.
49+
foreach ($this->elements as $name => $elements) {
50+
$label = '<label>' . $elements['title'] . '</label>';
51+
switch ($elements['type']) {
52+
case 'textarea':
53+
$input = '<textarea name="' . $name . '" ></textarea>';
54+
break;
55+
case 'submit':
56+
$input = '<input type="submit" name="' . $name . '" value="' . $elements['title'] . '">';
57+
$label = '';
58+
break;
59+
default:
60+
$input = '<input type="' . $elements['type'] . '" name="' . $name . '" />';
61+
break;
62+
}
63+
$output .= $label . '<p>' . $input . '</p>';
64+
}
65+
66+
// Wrap a form around the inputs.
67+
$output = '
68+
<form action="' . $_SERVER['PHP_SELF'] . '" method="post">
69+
<input type="hidden" name="action" value="submit_' . $this->form_number . '" />
70+
' . $output . '
71+
</form>';
72+
73+
// Return the form.
74+
return $output;
75+
}
76+
}

tests/test.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
require_once __DIR__ . '/../vendor/autoload.php'; // Autoload files using Composer autoload
4+
5+
6+
$f = new DF\ArrayForm( ["a"=>3] );
7+
8+
$f->dumpData();
9+

0 commit comments

Comments
 (0)