PHP Object Oriented Form Generator

I am trying to create an object oriented form generator. Please keep in mind that it will only be used by a few people in our company to solve a specific problem.

Currently, I come across two small reservations.

Item Creation Syntax

There are several approaches that I can take.

Setting up everything in the constructor. As a disadvantage, this can lead to inappropriate use of the constructor.

Input::create('text', 'name', array('maxlength' => 10)); 

Constraint constructor for entering and displaying only the most used attributes as methods (keep one method for setting the mass attribute)

 Input::create('text')->name('name')->value('value')->attribute('max_length', 10); 

Set each attribute as a method, either when creating a method for each attribute, or using the magic __call method, which will not lead to support for autocomplete in the IDE. And even now I can save the attribute method.

 Input::create()->type('text')->name('name')->value('value')->max_length(10)->id('id'); //etc. 

At the moment, I think the second approach is the best, because it preserves the “good” things from both worlds. Since it still provides a way to do the abstract part of the work, because, for example, the required method will not only set the required attribute, but also mark this field for the verification object as necessary.

Code duplication with approach 2 and 3

Since there are attributes that can be used by each element, but also attributes that can be used by only 3 or 4 elements, for example. HTML5 form attribute.

Each element can inherit a basic element that has methods for attributes that are universal for each element (for example, name ). Partially used attributes can be solved using interfaces, but this leads to duplication of code, since they cannot contain the body of the method.

Traits would be a solution, but unfortunately I was stuck on PHP 5.3 with no upgradeability. This leaves me with either the implementation of the Mixin or Composition template, which again cannot lead to support for autocomplete. This will be partially mitigated by using the second approach.

So to my actual question:

Which approach might end up as the most appropriate? (Suitable as with minimal code duplication, reuse of solid code and ease of implementation)

I understand that this can very well generate answers based on opinions, so I apologize in advance if this happens.

+5
source share
2 answers

I understand this is an old question, but someone in the comments mentions a project that I created called naomik / htmlgen . I am using some support here since I recently released a new version 2.x which makes the HTML generation pretty enjoyable in PHP.

 use function htmlgen\html as h; echo h('input', ['name'=>'catQty', 'value'=>500]) 

Will output

 <input name="catQty" value="500"> 

However, this example barely scratches the surface in terms of potential

 h('#wrapper', h('h1.title', 'Hello, World'), h('p', h('comment', 'link to project'), h('a', ['href'=>'https://github.com/naomik/htmlgen'], 'See htmlgen on Github') ) ); 

Here's the conclusion (the actual output has no spaces)

 <div id="wrapper"> <h1 class="title">Hello, World</h1> <p> <!-- link to project --> <a href="https://github.com/naomik/htmlgen">See htmlgen on Github</a> </p> </div> 

It is also very useful when visualizing data collections.

 use function htmlgen\html as h; use function htmlgen\map; $links = [ 'home' => '/', 'cats' => '/cats', 'milk' => '/milk', 'honey' => '/honey', 'donuts' => '/donuts', 'bees' => '/bees' ]; echo h('nav', h('ul', map($links, function($href, $text) { return h('li', h('a', ['href'=>$href], $text) ); }) ) ); 

Will be displayed (again, a space here is for display only)

 <nav> <ul> <li><a href="/">home</a></li> <li><a href="/cats">cats</a></li> <li><a href="/milk">milk</a></li> <li><a href="/honey">honey</a></li> <li><a href="/donuts">donuts</a></li> <li><a href="/bees">bees</a></li> </ul> </nav> 

This is all 100% PHP and not a custom, proprietary funny business. It is very expressive and lends itself to excellent compositions. You can break your templates into simple reuse or require .

Check out the examples for more interesting tips.

+1
source

IMO I would choose the second option. Probably because it reminds me of jQuery.

 Input::create('text')->name('name')->value('value')->attribute('max_length', 10); 

Define more general fields, such as "name", "value", "attribute" in the interface class "FormElements" and implement / extend it in the classes "Input", "Select", etc.

Although I personally prefer this second option .. in the words of Paul alien "Sometimes you just need to roll the dice."

0
source

Source: https://habr.com/ru/post/1214072/


All Articles