Created
April 16, 2015 16:04
-
-
Save chukShirley/27c1d877c61814bab124 to your computer and use it in GitHub Desktop.
ValueObjects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
abstract class AbstractValue | |
{ | |
protected $value; | |
private function __construct(){} | |
protected function filter() | |
{ | |
$this->setValue(trim($this->value)); | |
return $this; | |
} | |
public function getValue() | |
{ | |
return $this->value; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
interface AbstractValueInterface | |
{ | |
public function getValue(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Sabel\AbstractValue; | |
use Sabel\AbstractValueInterface; | |
class PriceValue extends AbstractValue implements AbstractValueInterface | |
{ | |
const MINIMUM_ALLOWED = 0; | |
const MAXIMUM_ALLOWED = 99999.9999; | |
public static function fromString($string) | |
{ | |
$price = new PriceValue; | |
$price->setValue($string)->filter()->validate(); | |
return $price; | |
} | |
protected function filter() | |
{ | |
return $this; | |
} | |
protected function validate() | |
{ | |
if ($this->value < self::MINIMUM_ALLOWED || $this->value > self::MAXIMUM_ALLOWED) | |
{ | |
throw new \InvalidArgumentException("Price must be between 0 and 99999.9999"); | |
} | |
return $this; | |
} | |
protected function setValue($value) | |
{ | |
$this->value = (float) $value; | |
return $this; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment