Skip to content

Commit 77a43e3

Browse files
authored
Merge pull request #4 from dipcode-software/feat/messages
Moved messages.php to class based and made if configurable on Config
2 parents 2c9dd7b + 4001105 commit 77a43e3

28 files changed

+140
-213
lines changed

CHANGELOG.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,22 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1515
- Added template pack `DefaultTemplatePack`. Defined as default template pack;
1616
- Added template pack `Bootstrap4TemplatePack` that integrates Bootstrap v4.0.0-beta.2.
1717
- `Config` singleton class allowing:
18-
- Configure custom renderers;
19-
- Configure custom template packs.
18+
- Configure custom template packs;
19+
- Configure custom messages;
20+
- Configure custom renderers.
2021
- Added extra arg `label` to method `getContext` of `Widget` class.
2122

2223
### Changed
2324
- `BoundField` attribute name `choices` changed to `options`;
2425
- `BoundField` attribute `options` now return an array instead of formated string;
2526
- `Widgets`, `labelTag` and `ErrorList` now render through default renderer instead of formatter `fleshgrinder/format`;
2627
- `CheckboxSelectMultiple` and `RadioSelect` widget wrapped in an unordered list tag instead of previous `div`;
27-
- Method name `getSubWidgets` to `getOptions` in `Widgets` class and now returns an array instead of formated string.
28+
- Method name `getSubWidgets` to `getOptions` in `Widgets` class and now returns an array instead of formated string;
29+
- `messages.php` to class based definition.
2830

2931
### Removed:
3032
- `PHPFormConfig` class. Use new `Config` class instead to configure `PHPForm`;
31-
- `\Utils\Attributes` class. All static methods, except `flatattr` which is no longer used, where migrated to `helpers.php`;
33+
- `Attribute` class. All static methods, except `flatattr` which is no longer used, where migrated to `helpers.php`;
3234
- Method `asUL` from `ErrorList` class.
3335

3436
## [1.0.1] - 2017-12-07

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@
2525
},
2626
"autoload": {
2727
"files": [
28-
"src/helpers.php",
29-
"config.php"
28+
"src/helpers.php"
3029
],
3130
"psr-4": {
3231
"PHPForm\\" : "src/"

config.php

Lines changed: 0 additions & 67 deletions
This file was deleted.

src/Config.php

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
use PHPForm\Renderers\TwigRenderer;
55
use PHPForm\TemplatePacks\DefaultTemplatePack;
6+
use PHPForm\Messages;
67

78
class Config extends Singleton
89
{
@@ -14,6 +15,11 @@ class Config extends Singleton
1415
DefaultTemplatePack::class,
1516
);
1617

18+
/**
19+
* @var string Renderer class used to render html content.
20+
*/
21+
protected $messages_class = Messages::class;
22+
1723
/**
1824
* @var string Renderer class used to render html content.
1925
*/
@@ -25,17 +31,33 @@ class Config extends Singleton
2531
private $renderer;
2632

2733
/**
28-
* Return renderer class instantiated
34+
* Set template pack on top level.
2935
*
30-
* @return PHPForm\Renderers\Renderer
36+
* @param string Class name of TemplatePack.
3137
*/
32-
public function getRenderer()
38+
public function setTemplatePack(string $template_pack)
3339
{
34-
if (is_null($this->renderer)) {
35-
$this->renderer = new $this->renderer_class($this->getTemplatesDirs());
36-
}
40+
$this->template_packs = array_unshift($this->template_packs, $template_pack);
41+
}
3742

38-
return $this->renderer;
43+
/**
44+
* Set messages class.
45+
*
46+
* @param string Class name of Renderer.
47+
*/
48+
public function setMessages(string $messages_class)
49+
{
50+
$this->messages_class = $messages_class;
51+
}
52+
53+
/**
54+
* Get messages class.
55+
*
56+
* @param Messages Messages class.
57+
*/
58+
public function getMessages()
59+
{
60+
return $this->messages_class;
3961
}
4062

4163
/**
@@ -49,13 +71,17 @@ public function setRenderer(string $renderer_class)
4971
}
5072

5173
/**
52-
* Set template pack on top level.
74+
* Return renderer class instantiated
5375
*
54-
* @param string Class name of TemplatePack.
76+
* @return PHPForm\Renderers\Renderer
5577
*/
56-
public function setTemplatePack(string $template_pack)
78+
public function getRenderer()
5779
{
58-
$this->template_packs = array_unshift($this->template_packs, $template_pack);
80+
if (is_null($this->renderer)) {
81+
$this->renderer = new $this->renderer_class($this->getTemplatesDirs());
82+
}
83+
84+
return $this->renderer;
5985
}
6086

6187
/**

src/Fields/ChoiceField.php

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

7-
use Fleshgrinder\Core\Formatter;
8-
97
use PHPForm\Exceptions\ValidationError;
10-
use PHPForm\PHPFormConfig;
118
use PHPForm\Widgets\Select;
129

1310
class ChoiceField extends Field
@@ -45,14 +42,12 @@ public function validate($value)
4542
parent::validate($value);
4643

4744
if (!$this->validValue($value)) {
48-
$error_message = PHPFormConfig::getIMessage("INVALID_CHOICE");
49-
5045
if (is_array($value)) {
5146
$value_diff = array_diff($value, array_keys($this->choices));
5247
$value = implode(', ', $value_diff);
5348
}
5449

55-
$message = Formatter::format($error_message, array(
50+
$message = msg("INVALID_CHOICE", array(
5651
'choice' => $value,
5752
));
5853

src/Fields/DateField.php

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

7-
use PHPForm\PHPFormConfig;
87
use PHPForm\Widgets\DateInput;
98

109
class DateField extends TemporalField
@@ -17,6 +16,6 @@ public function __construct(array $args = array())
1716
{
1817
parent::__construct($args);
1918

20-
$this->error_messages['invalid'] = PHPFormConfig::getIMessage("INVALID_DATE");
19+
$this->error_messages['invalid'] = msg("INVALID_DATE");
2120
}
2221
}

src/Fields/DateTimeField.php

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

7-
use PHPForm\PHPFormConfig;
87
use PHPForm\Widgets\DateTimeInput;
98

109
class DateTimeField extends TemporalField
@@ -17,6 +16,6 @@ public function __construct(array $args = array())
1716
{
1817
parent::__construct($args);
1918

20-
$this->error_messages['invalid'] = PHPFormConfig::getIMessage("INVALID_DATETIME");
19+
$this->error_messages['invalid'] = msg("INVALID_DATETIME");
2120
}
2221
}

src/Fields/Field.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use InvalidArgumentException;
88

99
use PHPForm\Exceptions\ValidationError;
10-
use PHPForm\PHPFormConfig;
1110

1211
abstract class Field
1312
{
@@ -83,7 +82,7 @@ public function __construct(array $args = array())
8382
$args['error_messages'] : $this->error_messages;
8483

8584
$default_error_messages = array(
86-
'required' => PHPFormConfig::getIMessage("REQUIRED"),
85+
'required' => msg("REQUIRED"),
8786
);
8887

8988
$this->error_messages = array_merge($default_error_messages, $this->error_messages);

src/Fields/FileField.php

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

7-
use Fleshgrinder\Core\Formatter;
8-
97
use PHPForm\Exceptions\ValidationError;
10-
use PHPForm\PHPFormConfig;
118
use PHPForm\Validators\FileTypeValidator;
129
use PHPForm\Widgets\FileInput;
1310

@@ -39,14 +36,11 @@ public function validate($value)
3936
}
4037

4138
if (0 == $value->size) {
42-
$errormsg = PHPFormConfig::getIMessage("EMPTY_FILE");
43-
throw new ValidationError($errormsg, 'empty_file');
39+
throw new ValidationError(msg("EMPTY_FILE"), 'empty_file');
4440
}
4541

4642
if (!is_null($this->max_size) && $value->size >= $this->max_size) {
47-
$errormsg = PHPFormConfig::getIMessage("INVALID_FILE_MAX_SIZE");
48-
49-
$message = Formatter::format($errormsg, array(
43+
$message = msg("INVALID_FILE_MAX_SIZE", array(
5044
"limit" => $this->max_size,
5145
"value" => $value->size
5246
));
@@ -58,7 +52,7 @@ public function validate($value)
5852
public function toNative($value)
5953
{
6054
if (!is_array($value)) {
61-
throw new ValidationError(PHPFormConfig::getIMessage("INVALID_FILE"), 'invalid');
55+
throw new ValidationError(msg("INVALID_FILE"), 'invalid');
6256
}
6357

6458
return (object) $value;

src/Fields/IntegerField.php

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

77
use PHPForm\Exceptions\ValidationError;
8-
use PHPForm\PHPFormConfig;
98
use PHPForm\Validators\MinValueValidator;
109
use PHPForm\Validators\MaxValueValidator;
1110
use PHPForm\Widgets\NumberInput;
@@ -36,8 +35,7 @@ public function toNative($value)
3635
if (is_numeric($value)) {
3736
return intval($value);
3837
} else {
39-
$error_messages = PHPFormConfig::getIMessage("INVALID_NUMBER");
40-
throw new ValidationError($error_messages, 'invalid');
38+
throw new ValidationError(msg("INVALID_NUMBER"), 'invalid');
4139
}
4240
}
4341

0 commit comments

Comments
 (0)