Skip to content

Commit

Permalink
Adjust Score to be more inline with spec
Browse files Browse the repository at this point in the history
* Removed concept of "scaling factor" in favor of just "scaled" being an
  unrelated value
* Improved exception messages and validation of values in setters
* Dropped `getValue` and `validate` methods as inappropriate without the
  scaling factor concept
* Improved test coverage
  • Loading branch information
brianjmiller committed Aug 29, 2016
1 parent cf2e61f commit dbd8b3f
Show file tree
Hide file tree
Showing 2 changed files with 170 additions and 138 deletions.
88 changes: 42 additions & 46 deletions src/Score.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

/**
* An optional field that represents the outcome of a graded Activity achieved
* by an Agent.
* by an actor.
*/
class Score implements VersionableInterface, ComparableInterface
{
Expand All @@ -32,7 +32,6 @@ class Score implements VersionableInterface, ComparableInterface
*
* @var int
*/
const DEFAULT_PRECISION = 2;
const SCALE_MIN = -1;
const SCALE_MAX = 1;
/**#@- */
Expand Down Expand Up @@ -69,66 +68,38 @@ class Score implements VersionableInterface, ComparableInterface
/**
* Constructor
*
* @param float|array $aRawValue the score value, may also be an array of properties
* @param float|array $aRawValue the raw score value, may also be an array of properties
* @param float $aMin the score minimum
* @param float $aMax the score maximum
* @param float $aScalingFactor the score scaling factor
* @param float $aScaledValue the scaled score
*/
public function __construct($aRawValue = null, $aMin = null, $aMax = null, $aScalingFactor = null) {
public function __construct($aRawValue = null, $aMin = null, $aMax = null, $aScaledValue = null) {
if (!is_array($aRawValue)) {
$aRawValue = [
'raw' => $aRawValue,
'min' => $aMin,
'max' => $aMax,
'scaled' => $aScalingFactor
'scaled' => $aScaledValue
];
}
$this->_fromArray($aRawValue);
}

/**
* @param float $aValue
* @throws InvalidArgumentException
* @return null
*/
public function validate($aValue) {
if (!isset($this->min, $this->max)) {
return;
}
if ($aValue < $this->min || $aValue > $this->max) {
throw new InvalidArgumentException(
sprintf("Value must be between %s and %s", $this->min, $this->max)
);
}
}

/**
* @param int $aPrecision a rounding precision integer
* @return null|float
*/
public function getValue($aPrecision = self::DEFAULT_PRECISION) {
if (!isset($this->raw)) {
return null;
}
if (isset($this->scaled)) {
return round($this->raw * $this->scaled, $aPrecision);
}
return round($this->raw, $aPrecision);
}

/**
* @param float $value
* @throws InvalidArgumentException
* @return self
*/
public function setScaled($value) {
if ($value < static::SCALE_MIN || $value > static::SCALE_MAX) {
throw new InvalidArgumentException(sprintf(
"Scale must be between %s and %s [%s]",
static::SCALE_MIN,
static::SCALE_MAX,
$value
));
if ($value < static::SCALE_MIN) {
throw new InvalidArgumentException(
sprintf( "Value must be greater than or equal to %s [%s]", static::SCALE_MIN, $value)
);
}
if ($value > static::SCALE_MAX) {
throw new InvalidArgumentException(
sprintf( "Value must be less than or equal to %s [%s]", static::SCALE_MAX, $value)
);
}
$this->scaled = (float) $value;
return $this;
Expand All @@ -143,10 +114,21 @@ public function getScaled() {

/**
* @param float $value
* @throws InvalidArgumentException
* @return self
*/
public function setRaw($value) {
$this->validate($value);
if (isset($this->min) && $value < $this->min) {
throw new InvalidArgumentException(
sprintf("Value must be greater than or equal to 'min' (%s) [%s]", $this->min, $value)
);
}
if (isset($this->max) && $value > $this->max) {
throw new InvalidArgumentException(
sprintf("Value must be less than or equal to 'max' (%s) [%s]", $this->max, $value)
);
}

$this->raw = (float) $value;
return $this;
}
Expand All @@ -164,8 +146,15 @@ public function getRaw() {
* @return self
*/
public function setMin($value) {
if (isset($this->raw) && $value > $this->raw) {
throw new InvalidArgumentException(
sprintf("Value must be less than or equal to 'raw' (%s) [%s]", $this->raw, $value)
);
}
if (isset($this->max) && $value >= $this->max) {
throw new InvalidArgumentException("Min must be less than max");
throw new InvalidArgumentException(
sprintf("Value must be less than 'max' (%s) [%s]", $this->max, $value)
);
}
$this->min = (float) $value;
return $this;
Expand All @@ -184,8 +173,15 @@ public function getMin() {
* @return self
*/
public function setMax($value) {
if (isset($this->raw) && $value < $this->raw) {
throw new InvalidArgumentException(
sprintf("Value must be greater than or equal to 'raw' (%s) [%s]", $this->raw, $value)
);
}
if (isset($this->min) && $value <= $this->min) {
throw new InvalidArgumentException("Max must be greater than min");
throw new InvalidArgumentException(
sprintf("Value must be greater than 'min' (%s) [%s]", $this->min, $value)
);
}
$this->max = (float) $value;
return $this;
Expand Down
Loading

0 comments on commit dbd8b3f

Please sign in to comment.