From 81a1b63f3fe2d53d38449383d1da5b4100b5a99d Mon Sep 17 00:00:00 2001 From: Dyan Galih Date: Thu, 10 Oct 2019 15:47:35 +0700 Subject: [PATCH] send exception report to laravel report function --- src/Tools/Lazy.php | 100 +++++++++++++++++++++++---------------------- 1 file changed, 52 insertions(+), 48 deletions(-) diff --git a/src/Tools/Lazy.php b/src/Tools/Lazy.php index 63912a0..f820094 100644 --- a/src/Tools/Lazy.php +++ b/src/Tools/Lazy.php @@ -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 @@ -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 = ""; @@ -115,10 +119,10 @@ private static function _getVarValue($toClass, $key) } catch (ReflectionException $e) { report($e); } - + return $propertyClass; } - + /** * @param object $class * @return bool @@ -132,10 +136,10 @@ public static function validate(object $class) $status = self::_validate($class, $class, $key); } } - + return $status; } - + /** * @param array $fromArray * @param object $toClass @@ -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; @@ -173,11 +177,11 @@ public static function copyFromArray(array $fromArray, object $toClass, int $opt break; } } - + } return $toClass; } - + /** * @param string $fromJson * @param object $toClass @@ -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 @@ -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]])) {