Skip to content

Commit

Permalink
Merge pull request #3 from do-aki/unified_message
Browse files Browse the repository at this point in the history
Unified exception message format
  • Loading branch information
do-aki committed Jul 5, 2016
2 parents f6e2a2a + 9696790 commit e970e2b
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 23 deletions.
9 changes: 5 additions & 4 deletions src/ClassAccessor/AccessorCommon.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ trait AccessorCommon
* @param string $message_format exception message format
* @throws TypeError
*/
protected function validatePrimitiveType($value, $type, $message_format = 'Argument 1 passed to %s::%s must be %s, %s given')
protected function validatePrimitiveType($value, $type, $message_format = AccessorUtility::PRIMITIVE_SETTER_MESSAGE_FORMAT)
{
$validator = "is_{$type}";
if (!$validator($value)) {
Expand All @@ -44,7 +44,7 @@ protected function validatePrimitiveType($value, $type, $message_format = 'Argum
* @param string $message_format exception message format
* @throws TypeError
*/
protected function validatePrimitiveTypeOrNull($value, $type, $message_format = 'Argument 1 passed to %s::%s must be %s, %s given')
protected function validatePrimitiveTypeOrNull($value, $type, $message_format = AccessorUtility::PRIMITIVE_SETTER_MESSAGE_FORMAT)
{
$validator = "is_{$type}";
if ($value !== null && !$validator($value)) {
Expand All @@ -68,7 +68,7 @@ protected function validatePrimitiveTypeOrNull($value, $type, $message_format =
* @param string $message_format exception message format
* @throws TypeError
*/
protected function validateObjectType($value, $type, $message_format = '%s::%s must be an instance of %s, %s given')
protected function validateObjectType($value, $type, $message_format = AccessorUtility::OBJECT_SETTER_MESSAGE_FORMAT)
{
if (!is_a($value, $type)) {
throw new TypeError(
Expand All @@ -88,9 +88,10 @@ protected function validateObjectType($value, $type, $message_format = '%s::%s m
*
* @param mixed $value validating value
* @param string $type expected type
* @param string $message_format exception message format
* @throws TypeError
*/
protected function validateObjectTypeOrNull($value, $type, $message_format = '%s::%s must be an instance of %s, %s given')
protected function validateObjectTypeOrNull($value, $type, $message_format = AccessorUtility::OBJECT_SETTER_MESSAGE_FORMAT)
{
if ($value !== null && !is_a($value, $type)) {
throw new TypeError(
Expand Down
12 changes: 11 additions & 1 deletion src/ClassAccessor/AccessorUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@
*/
class AccessorUtility {

/** @var string message format for primitive typed setter */
const PRIMITIVE_SETTER_MESSAGE_FORMAT = 'Argument 1 passed to %s::%s must be %s, %s given';
/** @var string message format for primitive typed setter */
const PRIMITIVE_GETTER_MESSAGE_FORMAT = 'Return value of %s::%s must be %s, %s returned';

/** @var string message format for object typed setter */
const OBJECT_SETTER_MESSAGE_FORMAT = 'Argument 1 passed to %s::%s must be an instance of %s, %s given';
/** @var string message format for object typed getter */
const OBJECT_GETTER_MESSAGE_FORMAT = 'Return value of %s::%s must be an instance of %s, %s returned';

/**
* get accessing property name
*
Expand All @@ -24,7 +34,7 @@ public static function getAccessingPropertyName()
)
);
}

/**
* return type string
*
Expand Down
28 changes: 14 additions & 14 deletions src/ClassAccessor/PrimitiveAccessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ private function _helperIntGetter()
}

$value = $this->$name;
$this->validatePrimitiveType($value, 'int', 'Return value of %s::%s must be %s, %s returned');
$this->validatePrimitiveType($value, 'int', AccessorUtility::PRIMITIVE_GETTER_MESSAGE_FORMAT);
return $value;
}

Expand Down Expand Up @@ -51,7 +51,7 @@ private function _helperIntOrNullGetter()
}

$value = $this->$name;
$this->validatePrimitiveTypeOrNull($value, 'int', 'Return value of %s::%s must be %s, %s returned');
$this->validatePrimitiveTypeOrNull($value, 'int', AccessorUtility::PRIMITIVE_GETTER_MESSAGE_FORMAT);
return $value;
}

Expand Down Expand Up @@ -81,7 +81,7 @@ private function _helperFloatGetter()
}

$value = $this->$name;
$this->validatePrimitiveType($value, 'float', 'Return value of %s::%s must be %s, %s returned');
$this->validatePrimitiveType($value, 'float', AccessorUtility::PRIMITIVE_GETTER_MESSAGE_FORMAT);
return $value;
}

Expand Down Expand Up @@ -111,7 +111,7 @@ private function _helperFloatOrNullGetter()
}

$value = $this->$name;
$this->validatePrimitiveTypeOrNull($value, 'float', 'Return value of %s::%s must be %s, %s returned');
$this->validatePrimitiveTypeOrNull($value, 'float', AccessorUtility::PRIMITIVE_GETTER_MESSAGE_FORMAT);
return $value;
}

Expand Down Expand Up @@ -141,7 +141,7 @@ private function _helperStringGetter()
}

$value = $this->$name;
$this->validatePrimitiveType($value, 'string', 'Return value of %s::%s must be %s, %s returned');
$this->validatePrimitiveType($value, 'string', AccessorUtility::PRIMITIVE_GETTER_MESSAGE_FORMAT);
return $value;
}

Expand Down Expand Up @@ -171,7 +171,7 @@ private function _helperStringOrNullGetter()
}

$value = $this->$name;
$this->validatePrimitiveTypeOrNull($value, 'string', 'Return value of %s::%s must be %s, %s returned');
$this->validatePrimitiveTypeOrNull($value, 'string', AccessorUtility::PRIMITIVE_GETTER_MESSAGE_FORMAT);
return $value;
}

Expand Down Expand Up @@ -201,7 +201,7 @@ private function _helperBoolGetter()
}

$value = $this->$name;
$this->validatePrimitiveType($value, 'bool', 'Return value of %s::%s must be %s, %s returned');
$this->validatePrimitiveType($value, 'bool', AccessorUtility::PRIMITIVE_GETTER_MESSAGE_FORMAT);
return $value;
}

Expand Down Expand Up @@ -231,7 +231,7 @@ private function _helperBoolOrNullGetter()
}

$value = $this->$name;
$this->validatePrimitiveTypeOrNull($value, 'bool', 'Return value of %s::%s must be %s, %s returned');
$this->validatePrimitiveTypeOrNull($value, 'bool', AccessorUtility::PRIMITIVE_GETTER_MESSAGE_FORMAT);
return $value;
}

Expand Down Expand Up @@ -261,7 +261,7 @@ private function _helperArrayGetter()
}

$value = $this->$name;
$this->validatePrimitiveType($value, 'array', 'Return value of %s::%s must be %s, %s returned');
$this->validatePrimitiveType($value, 'array', AccessorUtility::PRIMITIVE_GETTER_MESSAGE_FORMAT);
return $value;
}

Expand Down Expand Up @@ -291,7 +291,7 @@ private function _helperArrayOrNullGetter()
}

$value = $this->$name;
$this->validatePrimitiveTypeOrNull($value, 'array', 'Return value of %s::%s must be %s, %s returned');
$this->validatePrimitiveTypeOrNull($value, 'array', AccessorUtility::PRIMITIVE_GETTER_MESSAGE_FORMAT);
return $value;
}

Expand Down Expand Up @@ -321,7 +321,7 @@ private function _helperCallableGetter()
}

$value = $this->$name;
$this->validatePrimitiveType($value, 'callable', 'Return value of %s::%s must be %s, %s returned');
$this->validatePrimitiveType($value, 'callable', AccessorUtility::PRIMITIVE_GETTER_MESSAGE_FORMAT);
return $value;
}

Expand Down Expand Up @@ -351,7 +351,7 @@ private function _helperCallableOrNullGetter()
}

$value = $this->$name;
$this->validatePrimitiveTypeOrNull($value, 'callable', 'Return value of %s::%s must be %s, %s returned');
$this->validatePrimitiveTypeOrNull($value, 'callable', AccessorUtility::PRIMITIVE_GETTER_MESSAGE_FORMAT);
return $value;
}

Expand Down Expand Up @@ -381,7 +381,7 @@ private function _helperResourceGetter()
}

$value = $this->$name;
$this->validatePrimitiveType($value, 'resource', 'Return value of %s::%s must be %s, %s returned');
$this->validatePrimitiveType($value, 'resource', AccessorUtility::PRIMITIVE_GETTER_MESSAGE_FORMAT);
return $value;
}

Expand Down Expand Up @@ -411,7 +411,7 @@ private function _helperResourceOrNullGetter()
}

$value = $this->$name;
$this->validatePrimitiveTypeOrNull($value, 'resource', 'Return value of %s::%s must be %s, %s returned');
$this->validatePrimitiveTypeOrNull($value, 'resource', AccessorUtility::PRIMITIVE_GETTER_MESSAGE_FORMAT);
return $value;
}

Expand Down
4 changes: 2 additions & 2 deletions src/ClassAccessor/Template/ObjectTypeMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ private function _helper%TYPE%ObjectGetter()
}

$value = $this->$name;
$this->validateObjectType($value, %TYPE%::class, 'Return value of %s::%s must be an instance of %s, %s returned');
$this->validateObjectType($value, %TYPE%::class, AccessorUtility::OBJECT_GETTER_MESSAGE_FORMAT);
return $value;
}
_TPL;
Expand Down Expand Up @@ -96,7 +96,7 @@ private function _helper%TYPE%ObjectOrNullGetter()
}

$value = $this->$name;
$this->validateObjectTypeOrNull($value, %TYPE%::class, 'Return value of %s::%s must be an instance of %s, %s returned');
$this->validateObjectTypeOrNull($value, %TYPE%::class, AccessorUtility::OBJECT_GETTER_MESSAGE_FORMAT);
return $value;
}
_TPL;
Expand Down
4 changes: 2 additions & 2 deletions src/ClassAccessor/Template/PrimitiveTypeMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ private function _helper%TYPE_U%Getter()
}
$value = $this->$name;
$this->validatePrimitiveType($value, '%TYPE%', 'Return value of %s::%s must be %s, %s returned');
$this->validatePrimitiveType($value, '%TYPE%', AccessorUtility::PRIMITIVE_GETTER_MESSAGE_FORMAT);
return $value;
}
_TPL;
Expand Down Expand Up @@ -93,7 +93,7 @@ private function _helper%TYPE_U%OrNullGetter()
}
$value = $this->$name;
$this->validatePrimitiveTypeOrNull($value, '%TYPE%', 'Return value of %s::%s must be %s, %s returned');
$this->validatePrimitiveTypeOrNull($value, '%TYPE%', AccessorUtility::PRIMITIVE_GETTER_MESSAGE_FORMAT);
return $value;
}
_TPL;
Expand Down

0 comments on commit e970e2b

Please sign in to comment.