for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace Rudolf\Component\Forms;
use Rudolf\Component\Alerts;
abstract class Form
{
/**
* @var array
*/
protected $data;
protected $fields;
public function __construct()
$this->validator = new Validator();
validator
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
class MyClass { } $x = new MyClass(); $x->foo = true;
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:
class MyClass { public $foo; } $x = new MyClass(); $x->foo = true;
}
* Request handler.
*
* @param array $request Request array ($_POST)
public function handle(array $request)
$this->data = $request;
$this->check();
* Check is any errors.
* @return bool
public function isValid()
return !$this->validator->isErrors();
* Check is fields values valid.
abstract protected function check();
* Get data to display in add form.
* @param array $mergeWith
* @return array
public function getDataToDisplay($mergeWith = [])
if (empty($this->data)) {
$this->data = [];
if (!empty($mergeWith)) {
$this->data = array_merge($mergeWith, $this->data);
return array_map(function ($a) {
return htmlspecialchars(trim($a));
}, $this->data);
public function displayAlerts()
foreach ($this->validator->getAlerts() as $key => $value) {
Alerts\AlertsCollection::add(new Alerts\Alert(
'error',
$value
));
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: