1: <?php
2: /*
3: You may not change or alter any portion of this comment or credits
4: of supporting developers from this source code or any supporting source code
5: which is considered copyrighted (c) material of the original comment or credit authors.
6:
7: This program is distributed in the hope that it will be useful,
8: but WITHOUT ANY WARRANTY; without even the implied warranty of
9: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10: */
11:
12: namespace Xoops\Core\Service;
13:
14: /**
15: * Xoops service manager response object
16: *
17: * An instance of this object is passed as the first argument to all contract provider methods.
18: * The contract provider should return values and error data in this object. The object is then
19: * the return value from the provider.
20: *
21: * @category Xoops\Core\Service\Response
22: * @package Xoops\Core
23: * @author Richard Griffith <richard@geekwright.com>
24: * @copyright 2014 The XOOPS Project https://github.com/XOOPS/XoopsCore
25: * @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
26: * @version Release: 1.0
27: * @link http://xoops.org
28: * @since 2.6.0
29: */
30: class Response
31: {
32: /** @var mixed $value - return value from Provider */
33: protected $value = null;
34:
35: /** @var boolean $success - success as determined by service manager or provider */
36: protected $success = true;
37:
38: /** @var mixed $errorMessage - error description(s) as returned by service manager or provider */
39: protected $errorMessage = null;
40:
41: /**
42: * __construct
43: *
44: * @param mixed $value - value returned by provider
45: * @param boolean $success - true if service request was successful
46: * @param mixed $errorMessage - string or array of strings of any errors to be reported
47: */
48: public function __construct($value = null, $success = true, $errorMessage = null)
49: {
50: $this->value = $value;
51: $this->success = $success;
52: if ($errorMessage!==null) {
53: $this->addErrorMessage($errorMessage);
54: }
55: }
56:
57: /**
58: * getValue - get return value from provider
59: *
60: * @return mixed
61: */
62: public function getValue()
63: {
64: return $this->value;
65: }
66:
67: /**
68: * isSuccess - success of service request as determined by service manager or provider
69: *
70: * @return boolean
71: */
72: public function isSuccess()
73: {
74: return $this->success;
75: }
76:
77: /**
78: * getErrorMessage - get any messages set by service manager or provider
79: *
80: * @return mixed
81: */
82: public function getErrorMessage()
83: {
84: return $this->errorMessage;
85: }
86:
87: /**
88: * setValue - set value returned by request
89: *
90: * @param mixed $value value returned from provider
91: *
92: * @return Response object
93: */
94: public function setValue($value)
95: {
96: $this->value = $value;
97:
98: return $this;
99: }
100:
101: /**
102: * setSuccess - record success of request
103: *
104: * @param boolean $success - success of service request as determined by manager or provider
105: *
106: * @return Response object
107: */
108: public function setSuccess($success)
109: {
110: $this->success = $success;
111:
112: return $this;
113: }
114:
115: /**
116: * addErrorMessage - add a message
117: *
118: * @param mixed $errorMessage - message, or array of messages to be added
119: *
120: * @return Response object
121: */
122: public function addErrorMessage($errorMessage)
123: {
124: $ret = array();
125: if (is_array($this->errorMessage)) {
126: $ret = $this->errorMessage;
127: } elseif (is_scalar($this->errorMessage)) {
128: $ret[] = $this->errorMessage;
129: }
130: if (is_array($errorMessage)) {
131: $ret = array_merge($ret, $errorMessage);
132: } elseif (is_scalar($errorMessage)) {
133: $ret[] = $errorMessage;
134: }
135:
136: $this->errorMessage = $ret;
137:
138: return $this;
139: }
140: }
141: