-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathModelToViewTransformerVisitor.php
More file actions
52 lines (45 loc) · 1.53 KB
/
ModelToViewTransformerVisitor.php
File metadata and controls
52 lines (45 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
namespace DesignPatterns\Behavioral\Visitor\Visitors;
use DesignPatterns\Behavioral\Visitor\FormFields\CheckboxesField;
use DesignPatterns\Behavioral\Visitor\FormFields\EmailField;
use DesignPatterns\Behavioral\Visitor\FormFields\IntegerField;
use DesignPatterns\Behavioral\Visitor\VisitorInterface;
/**
* Visitor that transforms model value to view value.
*
* It corresponds to `ConcreteVisitor` in the Strategy pattern.
*
* @author Vlad Riabchenko <contact@vria.eu>
*/
class ModelToViewTransformerVisitor implements VisitorInterface
{
/**
* @param EmailField $emailField
*/
public function visitEmail(EmailField $emailField)
{
// Set email view value as the model value is.
$emailField->setViewValue($emailField->getValue());
}
/**
* @param IntegerField $integerField
*/
public function visitInteger(IntegerField $integerField)
{
// Convert integer model value to string view value.
$integerField->setViewValue(strval($integerField->getValue()));
}
/**
* @param CheckboxesField $checkboxesField
*/
public function visitCheckboxes(CheckboxesField $checkboxesField)
{
$choices = $checkboxesField->getChoices();
$viewValues = [];
// For each model value find a corresponding view value and add it to the array of view values.
foreach ($checkboxesField->getValue() as $value) {
$viewValues[] = array_search($value, $choices);
}
$checkboxesField->setViewValue($viewValues);
}
}