Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #680 add getPut/hasPut for \Phalcon\Http\Request #1403

Merged
merged 6 commits into from
Oct 19, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
166 changes: 166 additions & 0 deletions ext/http/request.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "Zend/zend_exceptions.h"
#include "Zend/zend_interfaces.h"

#include "main/php_variables.h"
#include "main/SAPI.h"

#include "ext/standard/php_smart_str.h"
Expand Down Expand Up @@ -78,6 +79,7 @@ PHALCON_INIT_CLASS(Phalcon_Http_Request){
zend_declare_property_null(phalcon_http_request_ce, SL("_dependencyInjector"), ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_null(phalcon_http_request_ce, SL("_filter"), ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_null(phalcon_http_request_ce, SL("_rawBody"), ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_null(phalcon_http_request_ce, SL("_put"), ZEND_ACC_PROTECTED TSRMLS_CC);

zend_class_implements(phalcon_http_request_ce TSRMLS_CC, 2, phalcon_http_requestinterface_ce, phalcon_di_injectionawareinterface_ce);

Expand Down Expand Up @@ -311,6 +313,125 @@ PHP_METHOD(Phalcon_Http_Request, getPost){
RETURN_CTOR(post);
}

/**
* Gets a variable from put request
*
*<code>
* $userEmail = $request->getPut("user_email");
*
* $userEmail = $request->getPut("user_email", "email");
*</code>
*
* @param string $name
* @param string|array $filters
* @param mixed $defaultValue
* @param boolean $notAllowEmpty
* @param boolean $noRecursive
* @return mixed
*/
PHP_METHOD(Phalcon_Http_Request, getPut){

zval *name = NULL, *filters = NULL, *default_value = NULL, *not_allow_empty = NULL, *norecursive = NULL;
zval *is_put, *put, *raw, *value, *filter = NULL, *dependency_injector, *service;
char *tmp;

PHALCON_MM_GROW();

phalcon_fetch_params(1, 0, 5, &name, &filters, &default_value, &not_allow_empty, &norecursive);

if (!name) {
name = PHALCON_GLOBAL(z_null);
}

if (!filters) {
filters = PHALCON_GLOBAL(z_null);
}

if (!default_value) {
default_value = PHALCON_GLOBAL(z_null);
}

if (!not_allow_empty) {
not_allow_empty = PHALCON_GLOBAL(z_false);
}

if (!norecursive) {
norecursive = PHALCON_GLOBAL(z_false);
}

PHALCON_INIT_VAR(is_put);
phalcon_call_method(is_put, this_ptr, "isPut");

if (!zend_is_true(is_put)) {
RETURN_CTOR(default_value);
}

PHALCON_OBS_VAR(put);
phalcon_read_property_this(&put, this_ptr, SL("_put"), PH_NOISY_CC);
if (Z_TYPE_P(put) != IS_ARRAY) {
PHALCON_INIT_VAR(raw);
phalcon_call_method(raw, this_ptr, "getRawBody");

PHALCON_INIT_NVAR(put);
array_init(put);

PHALCON_ENSURE_IS_STRING(&raw);
tmp = estrndup(Z_STRVAL_P(raw), Z_STRLEN_P(raw));
sapi_module.treat_data(PARSE_STRING, tmp, put TSRMLS_CC);

phalcon_update_property_this(getThis(), SL("_put"), put TSRMLS_CC);
}

if (Z_TYPE_P(name) != IS_NULL) {
if (phalcon_array_isset(put, name)) {

PHALCON_OBS_VAR(value);
phalcon_array_fetch(&value, put, name, PH_NOISY);
if (Z_TYPE_P(filters) != IS_NULL) {

PHALCON_OBS_VAR(filter);
phalcon_read_property_this(&filter, this_ptr, SL("_filter"), PH_NOISY_CC);
if (Z_TYPE_P(filter) != IS_OBJECT) {

PHALCON_OBS_VAR(dependency_injector);
phalcon_read_property_this(&dependency_injector, this_ptr, SL("_dependencyInjector"), PH_NOISY_CC);
if (Z_TYPE_P(dependency_injector) != IS_OBJECT) {
PHALCON_THROW_EXCEPTION_STR(phalcon_http_request_exception_ce, "A dependency injection object is required to access the 'filter' service");
return;
}

PHALCON_INIT_VAR(service);
PHALCON_ZVAL_MAYBE_INTERNED_STRING(service, phalcon_interned_filter);

PHALCON_INIT_NVAR(filter);
phalcon_call_method_p1(filter, dependency_injector, "getshared", service);
PHALCON_VERIFY_INTERFACE(filter, phalcon_filterinterface_ce);
phalcon_update_property_this(this_ptr, SL("_filter"), filter TSRMLS_CC);
}

phalcon_call_method_p3(return_value, filter, "sanitize", value, filters, norecursive);

if ((PHALCON_IS_EMPTY(return_value) && zend_is_true(not_allow_empty)) || PHALCON_IS_FALSE(return_value)) {
zval_dtor(return_value);
RETURN_CTOR(default_value);
} else {
RETURN_MM();
}
} else {
if (PHALCON_IS_EMPTY(value) && zend_is_true(not_allow_empty)) {
RETURN_CTOR(default_value);
} else {
RETURN_CTOR(value);
}
}
}

RETURN_CTOR(default_value);
}

RETURN_CTOR(put);
}

/**
* Gets variable from $_GET superglobal applying filters if needed
* If no parameters are given the $_GET superglobal is returned
Expand Down Expand Up @@ -475,6 +596,51 @@ PHP_METHOD(Phalcon_Http_Request, hasPost){
RETURN_FALSE;
}

/**
* Checks whether put has certain index
*
* @param string $name
* @return boolean
*/
PHP_METHOD(Phalcon_Http_Request, hasPut){

zval *name, *is_put, *put, *raw;
char *tmp;

PHALCON_MM_GROW();

phalcon_fetch_params(1, 1, 0, &name);

PHALCON_INIT_VAR(is_put);
phalcon_call_method(is_put, this_ptr, "isPut");

if (!zend_is_true(is_put)) {
RETURN_MM_FALSE;
}

PHALCON_OBS_VAR(put);
phalcon_read_property_this(&put, this_ptr, SL("_put"), PH_NOISY_CC);
if (Z_TYPE_P(put) != IS_ARRAY) {
PHALCON_INIT_VAR(raw);
phalcon_call_method(raw, this_ptr, "getRawBody");

PHALCON_INIT_NVAR(put);
array_init(put);

PHALCON_ENSURE_IS_STRING(&raw);
tmp = estrndup(Z_STRVAL_P(raw), Z_STRLEN_P(raw));
sapi_module.treat_data(PARSE_STRING, tmp, put TSRMLS_CC);

phalcon_update_property_this(getThis(), SL("_put"), put TSRMLS_CC);
}

if (phalcon_array_isset(put, name)) {
RETURN_MM_TRUE;
}

RETURN_MM_FALSE;
}

/**
* Checks whether $_GET superglobal has certain index
*
Expand Down
16 changes: 16 additions & 0 deletions ext/http/request.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ PHP_METHOD(Phalcon_Http_Request, setDI);
PHP_METHOD(Phalcon_Http_Request, getDI);
PHP_METHOD(Phalcon_Http_Request, get);
PHP_METHOD(Phalcon_Http_Request, getPost);
PHP_METHOD(Phalcon_Http_Request, getPut);
PHP_METHOD(Phalcon_Http_Request, getQuery);
PHP_METHOD(Phalcon_Http_Request, getServer);
PHP_METHOD(Phalcon_Http_Request, has);
PHP_METHOD(Phalcon_Http_Request, hasPost);
PHP_METHOD(Phalcon_Http_Request, hasPut);
PHP_METHOD(Phalcon_Http_Request, hasQuery);
PHP_METHOD(Phalcon_Http_Request, hasServer);
PHP_METHOD(Phalcon_Http_Request, getHeader);
Expand Down Expand Up @@ -88,6 +90,14 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_http_request_getpost, 0, 0, 0)
ZEND_ARG_INFO(0, noRecursive)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_http_request_getput, 0, 0, 0)
ZEND_ARG_INFO(0, name)
ZEND_ARG_INFO(0, filters)
ZEND_ARG_INFO(0, defaultValue)
ZEND_ARG_INFO(0, notAllowEmpty)
ZEND_ARG_INFO(0, noRecursive)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_http_request_getquery, 0, 0, 0)
ZEND_ARG_INFO(0, name)
ZEND_ARG_INFO(0, filters)
Expand All @@ -108,6 +118,10 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_http_request_haspost, 0, 0, 1)
ZEND_ARG_INFO(0, name)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_http_request_hasput, 0, 0, 1)
ZEND_ARG_INFO(0, name)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_http_request_hasquery, 0, 0, 1)
ZEND_ARG_INFO(0, name)
ZEND_END_ARG_INFO()
Expand Down Expand Up @@ -141,10 +155,12 @@ PHALCON_INIT_FUNCS(phalcon_http_request_method_entry){
PHP_ME(Phalcon_Http_Request, getDI, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Http_Request, get, arginfo_phalcon_http_request_get, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Http_Request, getPost, arginfo_phalcon_http_request_getpost, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Http_Request, getPut, arginfo_phalcon_http_request_getput, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Http_Request, getQuery, arginfo_phalcon_http_request_getquery, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Http_Request, getServer, arginfo_phalcon_http_request_getserver, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Http_Request, has, arginfo_phalcon_http_request_has, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Http_Request, hasPost, arginfo_phalcon_http_request_haspost, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Http_Request, hasPut, arginfo_phalcon_http_request_haspost, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Http_Request, hasQuery, arginfo_phalcon_http_request_hasquery, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Http_Request, hasServer, arginfo_phalcon_http_request_hasserver, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Http_Request, getHeader, arginfo_phalcon_http_request_getheader, ZEND_ACC_PUBLIC)
Expand Down
37 changes: 37 additions & 0 deletions ext/tests/issue-680.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
--TEST--
Implement getPut() - https://github.com/phalcon/cphalcon/issues/680
--SKIPIF--
<?php include('skipif.inc'); ?>
--PUT--
string=hello&array[string]=world
--FILE--
<?php
$di = new \Phalcon\DI\FactoryDefault();

$request = new \Phalcon\Http\Request();
$request->setDI($di);

var_dump($request->getPut('string', 'string'));
var_dump($request->getPut('string', 'string', null, true, true));

var_dump($request->getPut('array', 'string'));
var_dump($request->getPut('array', 'string', null, true, true));
echo PHP_EOL;

var_dump($request->hasPut('string'));
var_dump($request->hasPut('array'));
var_dump($request->hasPut('object'));

?>
--EXPECT--
string(5) "hello"
string(5) "hello"
array(1) {
["string"]=>
string(5) "world"
}
NULL

bool(true)
bool(true)
bool(false)
15 changes: 14 additions & 1 deletion php-tests/tests/PHPTTestSuite.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ public static function suite()
}

$directory = __DIR__ . '/../../ext/tests/';
return new PHPUnit_Extensions_PhptTestSuite($directory);

$facade = new File_Iterator_Facade;
$files = $facade->getFilesAsArray($directory, '.phpt');

$suite = new PHPUnit_Framework_TestSuite();

foreach ($files as $file) {
$c = file_get_contents($file);
if (!preg_match('/^--(?:PUT|(?:GZIP|DEFLATE)_POST|CGI)--$/m', $c)) {
$suite->addTestFile($file);
}
}

return $suite;
}
}
15 changes: 14 additions & 1 deletion unit-tests/PHPTTestSuite.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ public static function suite()
}

$directory = __DIR__ . '/../ext/tests/';
return new PHPUnit_Extensions_PhptTestSuite($directory);

$facade = new File_Iterator_Facade;
$files = $facade->getFilesAsArray($directory, '.phpt');

$suite = new PHPUnit_Framework_TestSuite();

foreach ($files as $file) {
$c = file_get_contents($file);
if (!preg_match('/^--(?:PUT|(?:GZIP|DEFLATE)_POST|CGI)--$/m', $c)) {
$suite->addTestFile($file);
}
}

return $suite;
}
}