Skip to content

Commit

Permalink
send exception report to laravel report function
Browse files Browse the repository at this point in the history
  • Loading branch information
DyanGalih committed Oct 10, 2019
1 parent 2ae6e07 commit 81a1b63
Showing 1 changed file with 52 additions and 48 deletions.
100 changes: 52 additions & 48 deletions src/Tools/Lazy.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,74 +20,78 @@ class Lazy
* @var int
*/
public const NONE = 1;

/**
* @Var int
*/
public const VALIDATE_ORIGIN = 2;

/**
* @var int
*/
public const VALIDATE_DEST = 3;

/**
* @var int
*/
public const VALIDATE_BOTH = 4;

/**
* @var int
*/
public const AUTOCAST = 5;


/**
* @param object $fromClass
* @param object $toClass
* @param int $option
* @return object
* @throws Exception
*/
public static function copy(object $fromClass, object $toClass, int $option = self::NONE): object
{
switch ($option) {
case self::NONE:
foreach (get_object_vars($fromClass) as $key => $value) {
$toClass->$key = $value;
}
break;
case self::VALIDATE_ORIGIN:
foreach (get_object_vars($fromClass) as $key => $value) {
if (self::_validate($fromClass, $fromClass, $key)) {
try {
switch ($option) {
case self::NONE:
foreach (get_object_vars($fromClass) as $key => $value) {
$toClass->$key = $value;
}
};
break;
case self::VALIDATE_DEST:
foreach (get_object_vars($toClass) as $key => $value) {
if (property_exists($toClass, $key)) {
if (self::_validate($fromClass, $toClass, $key)) {
$toClass->$key = $fromClass->$key;
}
}
};
break;
case self::VALIDATE_BOTH:
foreach (get_object_vars($fromClass) as $key => $value) {
if (property_exists($toClass, $key)) {
if (self::_validate($fromClass, $fromClass, $key) && self::_validate($fromClass, $toClass, $key)) {
break;
case self::VALIDATE_ORIGIN:
foreach (get_object_vars($fromClass) as $key => $value) {
if (self::_validate($fromClass, $fromClass, $key)) {
$toClass->$key = $value;
}
}
};
break;

};
break;
case self::VALIDATE_DEST:
foreach (get_object_vars($toClass) as $key => $value) {
if (property_exists($toClass, $key)) {
if (self::_validate($fromClass, $toClass, $key)) {
$toClass->$key = $fromClass->$key;
}
}
};
break;
case self::VALIDATE_BOTH:
foreach (get_object_vars($fromClass) as $key => $value) {
if (property_exists($toClass, $key)) {
if (self::_validate($fromClass, $fromClass, $key) && self::_validate($fromClass, $toClass, $key)) {
$toClass->$key = $value;
}
}
};
break;

}
} catch (Exception $exception) {
report($exception);
}



return $toClass;
}

/**
* @param $fromClass
* @param $toClass
Expand All @@ -98,14 +102,14 @@ public static function copy(object $fromClass, object $toClass, int $option = se
private static function _validate($fromClass, $toClass, $key)
{
$propertyClass = self::_getVarValue($toClass, $key);

if (gettype($fromClass->$key) == $propertyClass) {
return true;
} else {
throw new Exception('Type Mismatch on property ' . $key . '. The property type is ' . $propertyClass . ' but the value type is ' . gettype($fromClass->$key));
}
}

private static function _getVarValue($toClass, $key)
{
$propertyClass = "";
Expand All @@ -115,10 +119,10 @@ private static function _getVarValue($toClass, $key)
} catch (ReflectionException $e) {
report($e);
}

return $propertyClass;
}

/**
* @param object $class
* @return bool
Expand All @@ -132,10 +136,10 @@ public static function validate(object $class)
$status = self::_validate($class, $class, $key);
}
}

return $status;
}

/**
* @param array $fromArray
* @param object $toClass
Expand All @@ -151,7 +155,7 @@ public static function copyFromArray(array $fromArray, object $toClass, int $opt
$toClass->$key = $fromArray[$key];
} else {
$propertyClass = self::_getVarValue($toClass, $key);

switch ($propertyClass) {
case "integer":
$toClass->$key = isset($fromArray[$key]) ? (int)$fromArray[$key] : null;
Expand All @@ -173,11 +177,11 @@ public static function copyFromArray(array $fromArray, object $toClass, int $opt
break;
}
}

}
return $toClass;
}

/**
* @param string $fromJson
* @param object $toClass
Expand All @@ -189,7 +193,7 @@ public static function copyFromJson(string $fromJson, object $toClass, int $opti
{
return self::copyFromArray(json_decode($fromJson, true), $toClass, $option);
}

/**
* @param ReflectionProperty $property
* @return mixed|null
Expand All @@ -199,7 +203,7 @@ private static function getVar(ReflectionProperty $property)
$typeMapping = [];
$typeMapping['int'] = 'integer';
$typeMapping['bool'] = 'boolean';

// Get the content of the @var annotation
if (preg_match('/@var\s+([^\s]+)/', $property->getDocComment(), $matches)) {
if (isset($typeMapping[$matches[1]])) {
Expand Down

0 comments on commit 81a1b63

Please sign in to comment.