Skip to content

Instantly share code, notes, and snippets.

@chukShirley
Created April 16, 2015 16:04
Show Gist options
  • Save chukShirley/27c1d877c61814bab124 to your computer and use it in GitHub Desktop.
Save chukShirley/27c1d877c61814bab124 to your computer and use it in GitHub Desktop.
ValueObjects
<?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;
}
}
<?php
interface AbstractValueInterface
{
public function getValue();
}
<?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