patternphpMinor
Email text validator
Viewed 0 times
textvalidatoremail
Problem
I'm using this pattern for the first time and wanted to check if this is the correct implementation.
class.validator.strategy.php
class.text.validator.php
class.number.validator.php
class.validator.php
```
include('validator.strategy.php');
include('class.text.validator.php');
include('class.number.validator.php');
include('class.email.validator.php');
class Validator
{
//holds strategy object
protected $validatorStrategy = array();
//holds form field
protected $fields = array();
public function __construct()
{
$this->validatorStrategy[ 'text' ] = new TextValidator();
$this->validatorStrategy[ 'number' ] = new NumberValidator();
$this->validatorStrategy[ 'email' ] = new EmailValidator();
}
public function ruleForTextField( $name, $value )
{
$this->fields[ $name ][ 'value' ] = $value;
$this->fields[ $name ][ 'type' ] = 'text';
}
public function ruleForNumbertField( $name, $value )
{
$this->fields[ $name ][ 'value' ] = $value;
$this->fields[ $name ][ 'type' ] = 'number';
}
public function ruleForEmailField( $name, $value )
{
$this->fields[ $name ][ 'value' ] = $value;
$this->fields[ $name ][ 'type' ] = 'email';
}
public function isValidate()
{
$status = 0;
foreach ( $this->fields as $key => $val )
{
if ( !$this->validatorStrategy[ $val[ 'type' ] ]->check( $key, $val ) )
{
$status++;
}
if ( $status == 0 )
{
return true;
}
else
class.validator.strategy.php
abstract class ValidatorStrategy
{
abstract public function check( $name, $val );
}class.text.validator.php
class TextValidator extends ValidatorStrategy
{
public function check( $name, $val )
{
//logic here
}
}class.number.validator.php
class NumberValidator extends ValidatorStrategy
{
public function check( $name, $val )
{
//logic here
}
}class.validator.php
```
include('validator.strategy.php');
include('class.text.validator.php');
include('class.number.validator.php');
include('class.email.validator.php');
class Validator
{
//holds strategy object
protected $validatorStrategy = array();
//holds form field
protected $fields = array();
public function __construct()
{
$this->validatorStrategy[ 'text' ] = new TextValidator();
$this->validatorStrategy[ 'number' ] = new NumberValidator();
$this->validatorStrategy[ 'email' ] = new EmailValidator();
}
public function ruleForTextField( $name, $value )
{
$this->fields[ $name ][ 'value' ] = $value;
$this->fields[ $name ][ 'type' ] = 'text';
}
public function ruleForNumbertField( $name, $value )
{
$this->fields[ $name ][ 'value' ] = $value;
$this->fields[ $name ][ 'type' ] = 'number';
}
public function ruleForEmailField( $name, $value )
{
$this->fields[ $name ][ 'value' ] = $value;
$this->fields[ $name ][ 'type' ] = 'email';
}
public function isValidate()
{
$status = 0;
foreach ( $this->fields as $key => $val )
{
if ( !$this->validatorStrategy[ $val[ 'type' ] ]->check( $key, $val ) )
{
$status++;
}
if ( $status == 0 )
{
return true;
}
else
Solution
No, strategy pattern relies on an interface to allow composition to determine functionality at run time. Notice that it is easier to extend with new speaking functionality; and can be used to execute at run time. Here you are just using the array of the interface instantiations to do multiple validations.
I.E.
Interface for the pattern
Implementations of the pattern
Class that uses the strategy
Example usage
I.E.
Interface for the pattern
public interface Speak {
public void execute();
}Implementations of the pattern
public class Quack extends Speak {
public void execute() { /* Quack */ }
}
public class Bark extends Speak {
public void execute() { /* bark */ }
}Class that uses the strategy
public Animal {
private Speak _speak;
public Animal(Speak s) { _speak = s; }
}Example usage
main() {
new Animal(new Bark()); /* creates animal that barks */
new Animal(new Quack()); /* creates animal that quacks */
}Code Snippets
public interface Speak {
public void execute();
}public class Quack extends Speak {
public void execute() { /* Quack */ }
}
public class Bark extends Speak {
public void execute() { /* bark */ }
}public Animal {
private Speak _speak;
public Animal(Speak s) { _speak = s; }
}main() {
new Animal(new Bark()); /* creates animal that barks */
new Animal(new Quack()); /* creates animal that quacks */
}Context
StackExchange Code Review Q#3021, answer score: 4
Revisions (0)
No revisions yet.