@nsilly/support
Version:
NSilly Support package
85 lines (68 loc) • 2.06 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.Request = void 0;
var _lodash = _interopRequireDefault(require("lodash"));
var _container = require("@nsilly/container");
var Request =
/*#__PURE__*/
function () {
function Request() {}
Request.createFromRequest = function createFromRequest(request) {
return _container.App.make('Request').createFromRequest(request);
};
/**
* Returns the parameters.
*
* @return array An object of parameters
*/
Request.all = function all() {
return _container.App.make('Request').all();
};
/**
* Gets a "parameter" value from any bag.
*
* This method is mainly useful for libraries that want to provide some flexibility. If you don't need the
* flexibility in controllers, it is better to explicitly get request parameters from the appropriate
* public property instead (attributes, query, request).
*
* Order of precedence: PATH (routing placeholders or custom attributes), GET, BODY
*
* @param string key The key
* @param mixed default The default value if the parameter key does not exist
*
* @return mixed
*/
Request.get = function get(param, defaultValue) {
if (defaultValue === void 0) {
defaultValue = null;
}
if (this.has(param)) {
return _container.App.make('Request').all()[param];
} else {
return defaultValue;
}
};
/**
* Returns true if the parameter is defined.
*
* @param string key The key
*
* @return bool true if the parameter exists, false otherwise
*/
Request.has = function has(param) {
return !_lodash.default.isUndefined(_lodash.default.find(_lodash.default.keys(_container.App.make('Request').all()), function (item) {
return item === param;
}));
};
/**
* Clear the request
*
* @return void
*/
Request.clear = function clear() {
_container.App.make('Request').clear();
};
return Request;
}();
exports.Request = Request;