Skip to content

Commit 02fbb1f

Browse files
committed
Added singleton config for messages overriding and translations.
1 parent 0959e62 commit 02fbb1f

15 files changed

+234
-54
lines changed

config.php

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,59 @@
11
<?php
2+
namespace PHPForm;
23

3-
// Insert your configuration parameters here.
4+
class PHPFormConfig
5+
{
6+
const MESSAGES_FILE_PATH = 'src/messages.php';
7+
const TEMPLATES_FILE_PATH = 'src/templates.php';
8+
9+
private static $config = null;
10+
11+
private $messages = null;
12+
private $templates = null;
13+
14+
private function __construct()
15+
{
16+
$this->messages = include static::MESSAGES_FILE_PATH;
17+
$this->templates = include static::TEMPLATES_FILE_PATH;
18+
}
19+
20+
public static function getInstance()
21+
{
22+
if (is_null(static::$config)) {
23+
static::$config = new PHPFormConfig();
24+
}
25+
26+
return static::$config;
27+
}
28+
29+
public static function getIMessage(string $id)
30+
{
31+
return static::getInstance()->getMessage($id);
32+
}
33+
34+
public function getMessage(string $id)
35+
{
36+
37+
return array_key_exists($id, $this->messages) ? $this->messages[$id] : null;
38+
}
39+
40+
public static function getITemplate(string $id)
41+
{
42+
return static::getInstance()->getTemplate($id);
43+
}
44+
45+
public function getTemplate(string $id)
46+
{
47+
return array_key_exists($id, $this->templates) ? $this->templates[$id] : null;
48+
}
49+
50+
public function setMessages(array $messages)
51+
{
52+
$this->messages = array_merge($this->messages, $messages);
53+
}
54+
55+
public function setTemplates(array $templates)
56+
{
57+
$this->templates = array_merge($this->templates, $templates);
58+
}
59+
}

src/Fields/ChoiceField.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,14 @@
77
use Fleshgrinder\Core\Formatter;
88

99
use PHPForm\Exceptions\ValidationError;
10+
use PHPForm\PHPFormConfig;
1011
use PHPForm\Widgets\Select;
1112

1213
class ChoiceField extends Field
1314
{
1415
protected $widget = Select::class;
1516
protected $choices = array();
1617

17-
protected $error_messages = array(
18-
'invalid' => 'Select a valid choice. "{choice}" is not one of the available choices.'
19-
);
20-
2118
public function __construct(array $args = array())
2219
{
2320
parent::__construct($args);
@@ -48,7 +45,8 @@ public function validate($value)
4845
parent::validate($value);
4946

5047
if ($this->isEmpty($value) || !array_key_exists($value, $this->choices)) {
51-
$message = Formatter::format($this->error_messages['invalid'], array(
48+
$error_message = PHPFormConfig::getIMessage("INVALID_CHOICE");
49+
$message = Formatter::format($error_message, array(
5250
'choice' => $value
5351
));
5452

src/Fields/DateField.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55
namespace PHPForm\Fields;
66

7+
use PHPForm\PHPFormConfig;
78
use PHPForm\Widgets\DateInput;
89

910
class DateField extends TemporalField
@@ -12,7 +13,10 @@ class DateField extends TemporalField
1213

1314
protected $widget = DateInput::class;
1415

15-
protected $error_messages = array(
16-
'invalid' => 'Enter a valid date.'
17-
);
16+
public function __construct(array $args = array())
17+
{
18+
parent::__construct($args);
19+
20+
$this->error_messages['invalid'] = PHPFormConfig::getIMessage("INVALID_DATE");
21+
}
1822
}

src/Fields/DateTimeField.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55
namespace PHPForm\Fields;
66

7+
use PHPForm\PHPFormConfig;
78
use PHPForm\Widgets\DateTimeInput;
89

910
class DateTimeField extends TemporalField
@@ -12,7 +13,10 @@ class DateTimeField extends TemporalField
1213

1314
protected $widget = DateTimeInput::class;
1415

15-
protected $error_messages = array(
16-
'invalid' => 'Enter a valid date/time.'
17-
);
16+
public function __construct(array $args = array())
17+
{
18+
parent::__construct($args);
19+
20+
$this->error_messages['invalid'] = PHPFormConfig::getIMessage("INVALID_DATETIME");
21+
}
1822
}

src/Fields/Field.php

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use InvalidArgumentException;
88

99
use PHPForm\Exceptions\ValidationError;
10+
use PHPForm\PHPFormConfig;
1011
use PHPForm\Utils\Attributes;
1112

1213
abstract class Field
@@ -55,9 +56,7 @@ abstract class Field
5556
/**
5657
* @var array Array of message errors.
5758
*/
58-
protected $error_messages = array(
59-
'required' => 'This field is required.'
60-
);
59+
protected $error_messages = array();
6160

6261
/**
6362
* Instantiates a field.
@@ -84,6 +83,12 @@ public function __construct(array $args = array())
8483
$this->error_messages = array_key_exists('error_messages', $args) ?
8584
$args['error_messages'] : $this->error_messages;
8685

86+
$default_error_messages = array(
87+
'required' => PHPFormConfig::getIMessage("REQUIRED"),
88+
);
89+
90+
$this->error_messages = array_merge($default_error_messages, $this->error_messages);
91+
8792
if (!is_null($this->widget)) {
8893
// instantiate widget class if string is passed like so: Widget::class
8994
if (is_string($this->widget)) {
@@ -92,8 +97,6 @@ public function __construct(array $args = array())
9297

9398
$this->widget->setAttrs($this->widgetAttrs($this->widget));
9499
}
95-
96-
$this->error_messages = array_merge($this->getErrorMessages(), $this->error_messages);
97100
}
98101

99102
/**
@@ -245,27 +248,4 @@ public function widgetAttrs($widget)
245248
{
246249
return $this->widget_attrs;
247250
}
248-
249-
/**
250-
* Returns the combined inherited error messages for the field.
251-
*
252-
* @param string $class The class name to retrieve error message from.
253-
*
254-
* @return array
255-
*/
256-
private function getErrorMessages($class = null)
257-
{
258-
if (is_null($class)) {
259-
$class = get_class($this);
260-
}
261-
262-
$error_messages = array();
263-
264-
do {
265-
$class_vars = get_class_vars($class);
266-
$error_messages = array_merge($class_vars['error_messages'], $error_messages);
267-
} while ($class = get_parent_class($class));
268-
269-
return $error_messages;
270-
}
271251
}

src/Fields/IntegerField.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace PHPForm\Fields;
66

77
use PHPForm\Exceptions\ValidationError;
8+
use PHPForm\PHPFormConfig;
89
use PHPForm\Validators\MinValueValidator;
910
use PHPForm\Validators\MaxValueValidator;
1011
use PHPForm\Widgets\NumberInput;
@@ -13,11 +14,6 @@ class IntegerField extends Field
1314
{
1415
protected $widget = NumberInput::class;
1516

16-
protected $error_messages = array(
17-
'invalid' => 'Enter a whole number.'
18-
);
19-
20-
2117
public function __construct(array $args = array())
2218
{
2319
$this->min_value = array_key_exists('min_value', $args) ? $args['min_value'] : null;
@@ -40,7 +36,8 @@ public function toNative($value)
4036
if (is_numeric($value)) {
4137
return intval($value);
4238
} else {
43-
throw new ValidationError($this->error_messages['invalid'], 'invalid');
39+
$error_messages = PHPFormConfig::getIMessage("INVALID_NUMBER");
40+
throw new ValidationError($error_messages, 'invalid');
4441
}
4542
}
4643

src/Validators/EmailValidator.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,23 @@
44
*/
55
namespace PHPForm\Validators;
66

7-
use PHPForm\Validators\Validator;
87
use PHPForm\Exceptions\ValidationError;
8+
use PHPForm\PHPFormConfig;
9+
use PHPForm\Validators\Validator;
910

1011
class EmailValidator extends Validator
1112
{
12-
protected $message = "Enter a valid email address.";
1313
protected $code = "invalid";
1414

15+
public function __construct($message = null)
16+
{
17+
if (is_null($message)) {
18+
$message = PHPFormConfig::getIMessage("INVALID_EMAIL");
19+
}
20+
21+
parent::__construct($message);
22+
}
23+
1524
public function __invoke($value)
1625
{
1726
if (!filter_var($value, FILTER_VALIDATE_EMAIL)) {

src/Validators/MaxLengthValidator.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,23 @@
66

77
use Fleshgrinder\Core\Formatter;
88

9-
use PHPForm\Validators\BaseValidator;
109
use PHPForm\Exceptions\ValidationError;
10+
use PHPForm\PHPFormConfig;
11+
use PHPForm\Validators\BaseValidator;
1112

1213
class MaxLengthValidator extends BaseValidator
1314
{
14-
protected $message = "Ensure this value has at most {limit} character (it has {value}).";
1515
protected $code = "max_length";
1616

17+
public function __construct(int $value, $message = null)
18+
{
19+
if (is_null($message)) {
20+
$message = PHPFormConfig::getIMessage("INVALID_MAX_LENGTH");
21+
}
22+
23+
parent::__construct($value, $message);
24+
}
25+
1726
protected function cleanValue($value)
1827
{
1928
return strlen($value);

src/Validators/MaxValueValidator.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,22 @@
44
*/
55
namespace PHPForm\Validators;
66

7+
use PHPForm\PHPFormConfig;
78
use PHPForm\Validators\BaseValidator;
89

910
class MaxValueValidator extends BaseValidator
1011
{
11-
protected $message = "Ensure this value is less than or equal to {limit}.";
1212
protected $code = "min_value";
1313

14+
public function __construct(int $value, $message = null)
15+
{
16+
if (is_null($message)) {
17+
$message = PHPFormConfig::getIMessage("INVALID_MAX_VALUE");
18+
}
19+
20+
parent::__construct($value, $message);
21+
}
22+
1423
protected function compare($a, $b)
1524
{
1625
return $a > $b;

src/Validators/MinLengthValidator.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,22 @@
44
*/
55
namespace PHPForm\Validators;
66

7+
use PHPForm\PHPFormConfig;
78
use PHPForm\Validators\BaseValidator;
89

910
class MinLengthValidator extends BaseValidator
1011
{
11-
protected $message = "Ensure this value has at least {limit} character (it has {value}).";
1212
protected $code = "min_length";
1313

14+
public function __construct(int $value, $message = null)
15+
{
16+
if (is_null($message)) {
17+
$message = PHPFormConfig::getIMessage("INVALID_MIN_LENGTH");
18+
}
19+
20+
parent::__construct($value, $message);
21+
}
22+
1423
protected function cleanValue($value)
1524
{
1625
return strlen($value);

0 commit comments

Comments
 (0)