From 4ef4bb2f5b773f14c8f5533b5eeee36c33c46302 Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Wed, 26 Apr 2017 10:04:31 -0500 Subject: [PATCH] Update: add s_return() as a non-valued alternative to s_value() for error handling --- common/base/classes/base_return.php | 50 ++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/common/base/classes/base_return.php b/common/base/classes/base_return.php index fc36562..630867a 100644 --- a/common/base/classes/base_return.php +++ b/common/base/classes/base_return.php @@ -1596,10 +1596,12 @@ class c_base_return_error { /** * Creates a return boolean TRUE with the error value populated. * + * This will assign a value to the class. + * * @param $value * A value to provide * @param $class - * A custom class name. + * A custom class name of any class that is an instance of c_base_return_value. * @param c_base_error|null $error * (optional) a custom error setting. * Can be an array of c_base_error for returning multiple errors. @@ -1608,6 +1610,8 @@ class c_base_return_error { * @return c_base_return_false|c_base_return_value * A c_base_return_value object is returned with the error value populated * If the passed class is invalid or not of type c_base_return_value, then a c_base_return_true object with the error value populated. + * + * * @see: self::s_return() */ public static function s_value($value, $class, $error = NULL) { if (!class_exists($class) || !($class instanceof c_base_return_value)) { @@ -1635,4 +1639,48 @@ class c_base_return_error { $object_return->set_value($value); return $object_return; } + + /** + * Creates a return boolean TRUE with the error value populated. + * + * This will not assign any value to the class. + * + * @param $class + * A custom class name of any class that is an instance of c_base_return. + * @param c_base_error|null $error + * (optional) a custom error setting. + * Can be an array of c_base_error for returning multiple errors. + * When NULL, no errors are defined. + * + * @return c_base_return_false|c_base_return_value + * A c_base_return_value object is returned with the error value populated + * If the passed class is invalid or not of type c_base_return_value, then a c_base_return_true object with the error value populated. + * + * @see: self::s_value() + */ + public static function s_return($class, $error = NULL) { + if (!class_exists($class) || !($class instanceof c_base_return)) { + return self::s_false($error); + } + + $object_return = new $class(); + + if (is_null($error)) { + $object_error = new c_base_error(); + $object_return->set_error($object_error); + unset($object_error); + } + elseif (is_array($error)) { + foreach ($error as $delta => $value) { + $object_return->set_error($error, $delta); + } + unset($delta); + unset($value); + } + else { + $object_return->set_error($error); + } + + return $object_return; + } } -- 1.8.3.1