Skip to content

Commit

Permalink
add visitor
Browse files Browse the repository at this point in the history
  • Loading branch information
scabo committed Aug 12, 2012
1 parent 52353af commit d0e641f
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
13 changes: 13 additions & 0 deletions library/Pattern/Visitor/Product.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

class Pattern_Visitor_Product
{
public $brand = null;
public $model = null;
public $price = 0;

public function applyVisitor(Pattern_Visitor_Visitor_Interface $visitor)
{
$visitor->visitProduct($this);
}
}
12 changes: 12 additions & 0 deletions library/Pattern/Visitor/Visitor/Discount.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

class Pattern_Visitor_Visitor_Discount implements Pattern_Visitor_Visitor_Interface
{
public function visitProduct(Pattern_Visitor_Product $product)
{
if (0 === $product->price) return;

$product->price =
$product->price - number_format( (20 * 100) / $product->price);
}
}
6 changes: 6 additions & 0 deletions library/Pattern/Visitor/Visitor/Interface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

interface Pattern_Visitor_Visitor_Interface
{
function visitProduct(Pattern_Visitor_Product $product);
}
14 changes: 14 additions & 0 deletions tests/Pattern/Visitor/ProductTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

class Pattern_Visitor_ProductTest extends PHPUnit_Framework_TestCase
{
public function testDiscount()
{
$product = new Pattern_Visitor_Product();
$product->price = 100;
$discount = new Pattern_Visitor_Visitor_Discount();
$product->applyVisitor($discount);

$this->assertEquals(80, $product->price);
}
}

0 comments on commit d0e641f

Please sign in to comment.