CodeQ

Sabtu, 12 Desember 2009

Specification Pattern untuk Validasi

A specification pattern outlines a unit of business logic that is combinable with other business logic units. In this pattern, a unit of business logic inherits its functionality from the abstract aggregate Composite Specification class. The Composite Specification class has one function called IsSatisfiedBy that returns a boolean value. After instantiation, the specification is "chained" with other specifications, making new specifications easily maintainable, yet highly customizable business logic. Furthermore upon instantiation the business logic may, through method invocation or inversion of control, have its state altered in order to become a delegate of other classes such as a persistence repository.
Specification Interface

Setiap specification harus mengimplementasikan interface ini

interface Mox_Util_Specification_Interface
{
 public function isSatisfied();
 public function getMessage();
 public function setMessage($message);
}
Specification Base Class

Base class ini menyediakan default implementasi untuk method terkait dengan message

abstract class Mox_Util_Specification_Base implements Mox_Util_Specification_Interface
{
 private $message;
 
 public function __construct($message = '')
 {
  $this->message = $message;
 }
 
 public function getMessage()
 {
  return $this->message;
 }
 
 public function setMessage($message)
 {
  $this->message = $message;
 }
}
Composite Specification

Kelas ini mendukung composite specification

class Mox_Util_Specification_Composite extends Mox_Util_Specification_Base
{
 private $specs;
 
 public function __construct()
 {
  parent::__construct();
  $this->specs = array();
 }
 
 public function addSpecification($spec)
 {
  array_push($this->specs, $spec);
 }
 
 public function clear()
 {
  $this->specs = array();
 }
 
 public function isSatisfied()
 {
  foreach ($this->specs as $spec) {
   if (! $spec->isSatisfied()) {
    $this->setMessage($spec->getMessage());
    return false; 
   }
  }
  
  return true;
 }
}
Domain Specification

Spesifikasi ini akan terpenuhi jika $target ada di dalam array $domain

class Mox_Util_Specification_Domain extends Mox_Util_Specification_Base
{
 private $target;
 private $domain;
 
 public function __construct($target, $domain, $message = 'violating domain rule')
 {
  parent::__construct($message);
  $this->target =  $target;
  $this->domain = $domain;
 }
 
 
 public function isSatisfied()
 {  
  return in_array($this->target, $this->domain); 
 }
}
Match Specification

Spesifikasi ini akan terpenuhi jika $left sama dengan $right

class Mox_Util_Specification_Match extends Mox_Util_Specification_Base
{
 private $left;
 
 private $right;
 
 public function __construct($left, $right, $message = 'violating match rule')
 {
  parent::__construct($message);
  $this->left = $left;
  $this->right = $right;
 }
 
 public function isSatisfied()
 {
  return $this->left == $this->right; 
 }
}
Min Max Length Specification

Spesifikasi ini akan terpenuhi jika strlen($target) berada dalam rentang $min dan $max

class Mox_Util_Specification_MinMaxLength extends Mox_Util_Specification_Base
{
 private $target;
 private $min;
 private $max;
 
 public function __construct($target, $min, $max, $message = 'violating min max length rule')
 {
  parent::__construct($message);
  $this->target = (string) $target;
  $this->min = (int) $min;
  $this->max = (int) $max;
 }
 
 
 public function isSatisfied()
 {
  $strLen = strlen($this->target);
  return $strLen >= $this->min && $strLen <= $this->max; 
 }
}
Required Specification

Spesifikasi ini akan terpenuhi jika $candidate tidak kosong

class Mox_Util_Specification_Required extends Mox_Util_Specification_Base
{
 private $candidate;
 
 public function __construct($candidate, $message = 'violating required rule')
 {
  parent::__construct($message);
  $this->candidate = (string) $candidate;   
 }
 
 public function isSatisfied()
 {
  return strlen($this->candidate) > 0; 
 }
}

Contoh Terpenuhi

$bahasaku = 'sunda';

$spec = new Mox_Util_Specification_Composite();
$spec->addSpecification(new Mox_Util_Specification_Required($bahasaku, 'Bahasaku harus diisi'));
$spec->addSpecification(new Mox_Util_Specification_Domain($bahasaku, array('sunda', 'indonesia', 'inggris'), 'Pilihan bahasa salah');
$spec->addSpecification(new Mox_Util_Specification_MinMaxLength($bahasaku, 4, 10, 'Bahasa harus berkisar 4 sampai 10 karakter'));
$spec->addSpecification(new Mox_Util_Specification_Match($bahasaku, 'sunda', 'Bahasa harus sunda'));

if ($spec->isSatisfied()) {
 echo 'Spesifikasi terpenuhi semua';
}else{
 echo $spec->getMessage();
}

Hasilnya

Spesifikasi terpenuhi semua

Contoh Tidak Terpenuhi

$bahasaku = 'jerman';

$spec = new Mox_Util_Specification_Composite();
$spec->addSpecification(new Mox_Util_Specification_Required($bahasaku, 'Bahasaku harus diisi'));
$spec->addSpecification(new Mox_Util_Specification_Domain($bahasaku, array('sunda', 'indonesia', 'inggris'), 'Pilihan bahasa salah');
$spec->addSpecification(new Mox_Util_Specification_MinMaxLength($bahasaku, 4, 10, 'Bahasa harus berkisar 4 sampai 10 karakter'));
$spec->addSpecification(new Mox_Util_Specification_Match($bahasaku, 'sunda', 'Bahasa harus sunda'));

if ($spec->isSatisfied()) {
 echo 'Spesifikasi terpenuhi semua';
}else{
 echo $spec->getMessage();
}

Hasilnya

Pilihan bahasa salah

0 Komentar:

Poskan Komentar

Berlangganan Poskan Komentar [Atom]



<< Beranda