Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed and Added #424

Merged
merged 10 commits into from Jun 2, 2019

Property to transfer for bean

  • Loading branch information...
stelin committed May 31, 2019
commit e3d40856dabd86b85b67a65adae022aeb88f225a
@@ -26,6 +26,7 @@
use Swoft\Bean\Definition\PropertyInjection;
use Swoft\Bean\Exception\ContainerException;
use Swoft\Stdlib\Helper\ArrayHelper;
use Swoft\Stdlib\Helper\ObjectHelper;
use Swoft\Stdlib\Reflections;
use function ucfirst;
@@ -1024,6 +1025,12 @@ private function newProperty(
$propertyValue = $this->getRefValue($propertyValue, $id);
}
// Parser property type
$propertyType = ObjectHelper::getPropertyBaseType($reflectProperty);
if (!empty($propertyType)) {
$propertyValue = ObjectHelper::parseParamType($propertyType, $propertyValue);
}
// First, try set value by setter method
$setter = 'set' . ucfirst($propertyName);
if (method_exists($reflectObject, $setter)) {
@@ -1102,7 +1109,7 @@ private function getRefValue($value, string $id = '')
return $value;
}
if(strpos($value, '.') !== 0){
if (strpos($value, '.') !== 0) {
return $this->newBean($value, $id);
}
@@ -2,11 +2,13 @@
namespace Swoft\Stdlib\Helper;
use InvalidArgumentException;
use function is_numeric;
use function method_exists;
use function property_exists;
use ReflectionProperty;
use function spl_object_hash;
use Swoft\Db\Exception\DbException;
use Throwable;
use function ucfirst;
/**
@@ -16,6 +18,20 @@
*/
class ObjectHelper
{
/**
* Base types
*/
public const BASE_TYPES = [
'bool',
'boolean',
'string',
'int',
'integer',
'float',
'double',
'array'
];
/**
* Return object hash value
*
@@ -78,6 +94,7 @@ public static function parseParamType(string $type, $value)
$value = (string)$value;
break;
case 'bool':
case 'boolean':
$value = (bool)$value;
break;
case 'float':
@@ -87,15 +104,41 @@ public static function parseParamType(string $type, $value)
$value = (double)$value;
break;
}
} catch (\Throwable $e) {
throw new \InvalidArgumentException(
sprintf('Convert value(%s) to %s', json_unescaped_encode($value), $type)
} catch (Throwable $e) {
throw new InvalidArgumentException(
sprintf('Convert value(%s) to %s', json_encode($value), $type)
);
}
return $value;
}
/**
* @param ReflectionProperty $property
*
* @return string
*/
public static function getPropertyBaseType(ReflectionProperty $property): string
{
$docComment = $property->getDocComment();
if ($docComment == false || empty($docComment)) {
return '';
}
// Get the content of the @var annotation
if (preg_match('/@var\s+([^\s]+)/', $docComment, $matches)) {
list(, $type) = $matches;
} else {
return '';
}
if (in_array($type, self::BASE_TYPES)) {
return $type;
}
return '';
}
/**
* Get default by type
*
@@ -108,12 +151,14 @@ public static function getDefaultValue(string $type)
$value = null;
switch ($type) {
case 'int':
case 'integer':
$value = 0;
break;
case 'string':
$value = '';
break;
case 'bool':
case 'boolean':
$value = false;
break;
case 'float':
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.