Allow adding error message and sprintf() variables using setError()

xpressengine/xe-core#2181 적용시 에러 반환 문법을 단순화하기 위한 조치

기존 방식: return new Object(-1, '에러메시지');
XE 제안 방식: return class_exists('BaseObject') ? new BaseObject(-1, '에러메시지') : new Object('에러메시지');
라이믹스 방식: return $this->setError('에러메시지');

기존의 setError() 메소드가 에러 코드만 받을 수 있어서 호환성 보장에 도움이 안 되므로
에러 코드와 에러 메시지를 동시에 넣을 수 있도록 개선하고,
에러 코드를 넣지 않고 에러 메시지만 지정해도 자동으로 -1 에러 코드가 들어가도록 하였음.
(첫 번째 인자가 정수인지 아닌지에 따라 판단함.)

setError(), setMessage(), setMessageType() 등 기존에 무의미한 반환값을 가지던 메소스들 모두
$this를 반환하도록 함으로써 액션이나 트리거 등의 반환값으로 유효하도록 하고,
원할 경우 method chaining까지 사용할 수 있음.

또한 에러메시지에 변수를 넣어야 할 경우
return new Object(-1, sprintf(Context::getLang('error_msg'), $var1, $var2));
이렇게 복잡해지는 문제도 해결하기 위해
setError()에 추가로 넣은 인자는 모두 자동으로 sprintf() 처리를 거치도록 함.
예: return $this->setError('error_msg', $var1, $var2);

즉, 아래와 같은 호출 형태가 모두 유효함.

  - $this->setError(-1);
  - $this->setError(-1, 'error_msg');
  - $this->setError(-1, 'error_msg', $var1, $var2);
  - $this->setError('error_msg');
  - $this->setError('error_msg', $var1, $var2);

단, 이 커밋 이후 신규 작성하는 코어 클래스나 서드파티 자료에서만 사용할 수 있음.
기존 버전과의 호환성을 유지하기를 원하는 서드파티 자료는 XE에서 제안한 삼항식을 사용해야 함.
This commit is contained in:
Kijin Sung 2017-11-27 16:24:08 +09:00
parent bf6ccfcb44
commit 9a83e71bff
2 changed files with 65 additions and 56 deletions

View file

@ -90,43 +90,6 @@ class ModuleObject extends Object
return $this->get('redirect_url');
}
/**
* set message
* @param string $message a message string
* @param string $type type of message (error, info, update)
* @return void
* */
function setMessage($message = 'success', $type = NULL)
{
parent::setMessage($message);
$this->setMessageType($type);
}
/**
* set type of message
* @param string $type type of message (error, info, update)
* @return void
* */
function setMessageType($type)
{
$this->add('message_type', $type);
}
/**
* get type of message
* @return string $type
* */
function getMessageType()
{
$type = $this->get('message_type');
$typeList = array('error' => 1, 'info' => 1, 'update' => 1);
if(!isset($typeList[$type]))
{
$type = $this->getError() ? 'error' : 'info';
}
return $type;
}
/**
* Set the template path for refresh.html
* refresh.html is executed as a result of method execution

View file

@ -47,14 +47,36 @@ class Object
}
/**
* Setter to set error code
* Setter to set error code or message
*
* @param int $error error code
* @return void
* @param int|strong $error error code or message
* @return $this
*/
function setError($error = 0)
{
$this->error = $error;
// If the first argument is an integer, treat it as an error code. Otherwise, treat it as an error message.
$args = func_get_args();
if(strval(intval($error)) === strval($error))
{
$this->error = intval($error);
array_shift($args);
}
else
{
$this->error = -1;
}
// Convert the error message into the correct language and interpolate any other variables into it.
if(count($args))
{
$this->message = lang(array_shift($args));
if(count($args))
{
$this->message = vsprintf($this->message, $args);
}
}
return $this;
}
/**
@ -71,11 +93,12 @@ class Object
* Setter to set HTTP status code
*
* @param int $code HTTP status code. Default value is `200` that means successful
* @return void
* @return $this
*/
function setHttpStatusCode($code = 200)
{
$this->httpStatusCode = (int) $code;
return $this;
}
/**
@ -92,21 +115,17 @@ class Object
* Setter to set set the error message
*
* @param string $message Error message
* @return bool Alaways returns true.
* @param string $type type of message (error, info, update)
* @return $this
*/
function setMessage($message = 'success', $type = NULL)
function setMessage($message = 'success', $type = null)
{
if($str = lang($message))
$this->message = lang($message);
if($type !== null)
{
$this->message = $str;
$this->setMessageType($type);
}
else
{
$this->message = $message;
}
// TODO This method always returns True. We'd better remove it
return TRUE;
return $this;
}
/**
@ -119,23 +138,50 @@ class Object
return $this->message;
}
/**
* set type of message
* @param string $type type of message (error, info, update)
* @return $this
* */
function setMessageType($type)
{
$this->add('message_type', $type);
return $this;
}
/**
* get type of message
* @return string $type
* */
function getMessageType()
{
$type = $this->get('message_type');
$typeList = array('error' => 1, 'info' => 1, 'update' => 1);
if(!isset($typeList[$type]))
{
$type = $this->getError() ? 'error' : 'info';
}
return $type;
}
/**
* Setter to set a key/value pair as an additional variable
*
* @param string $key A variable name
* @param mixed $val A value for the variable
* @return void
* @return $this
*/
function add($key, $val)
{
$this->variables[$key] = $val;
return $this;
}
/**
* Method to set multiple key/value pairs as an additional variables
*
* @param Object|array $object Either object or array containg key/value pairs to be added
* @return void
* @return $this
*/
function adds($object)
{
@ -143,7 +189,6 @@ class Object
{
$object = get_object_vars($object);
}
if(is_array($object))
{
foreach($object as $key => $val)
@ -151,6 +196,7 @@ class Object
$this->variables[$key] = $val;
}
}
return $this;
}
/**