Skip to content

Commit 5ffd865

Browse files
committed
Add new php Validator and Filter interface
1 parent 5e51209 commit 5ffd865

File tree

10 files changed

+265
-5
lines changed

10 files changed

+265
-5
lines changed

ext/spl/config.m4

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ int main(int argc, char **argv) {
2222
CPPFLAGS=$old_CPPFLAGS
2323
AC_DEFINE_UNQUOTED(HAVE_PACKED_OBJECT_VALUE, $ac_result, [Whether struct _zend_object_value is packed])
2424
AC_DEFINE(HAVE_SPL, 1, [Whether you want SPL (Standard PHP Library) support])
25-
PHP_NEW_EXTENSION(spl, php_spl.c spl_functions.c spl_engine.c spl_iterators.c spl_array.c spl_directory.c spl_exceptions.c spl_observer.c spl_dllist.c spl_heap.c spl_fixedarray.c, no)
26-
PHP_INSTALL_HEADERS([ext/spl], [php_spl.h spl_array.h spl_directory.h spl_engine.h spl_exceptions.h spl_functions.h spl_iterators.h spl_observer.h spl_dllist.h spl_heap.h spl_fixedarray.h])
25+
PHP_NEW_EXTENSION(spl, php_spl.c spl_functions.c spl_engine.c spl_iterators.c spl_validators.c spl_filters.c spl_array.c spl_directory.c spl_exceptions.c spl_observer.c spl_dllist.c spl_heap.c spl_fixedarray.c, no)
26+
PHP_INSTALL_HEADERS([ext/spl], [php_spl.h spl_array.h spl_validators.h spl_filters.h spl_directory.h spl_engine.h spl_exceptions.h spl_functions.h spl_iterators.h spl_observer.h spl_dllist.h spl_heap.h spl_fixedarray.h])
2727
PHP_ADD_EXTENSION_DEP(spl, pcre, true)

ext/spl/config.w32

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// $Id$
22
// vim:ft=javascript
33

4-
EXTENSION("spl", "php_spl.c spl_functions.c spl_engine.c spl_iterators.c spl_array.c spl_directory.c spl_exceptions.c spl_observer.c spl_dllist.c spl_heap.c spl_fixedarray.c", false /*never shared */);
4+
EXTENSION("spl", "php_spl.c spl_functions.c spl_engine.c spl_iterators.c spl_validators.c spl_filters.c spl_array.c spl_directory.c spl_exceptions.c spl_observer.c spl_dllist.c spl_heap.c spl_fixedarray.c", false /*never shared */);
55
AC_DEFINE('HAVE_SPL', 1);
66
PHP_SPL="yes";
7-
PHP_INSTALL_HEADERS("ext/spl", "php_spl.h spl_array.h spl_directory.h spl_engine.h spl_exceptions.h spl_functions.h spl_iterators.h spl_observer.h spl_dllist.h spl_heap.h spl_fixedarray.h");
7+
PHP_INSTALL_HEADERS("ext/spl", "php_spl.h spl_validators.h spl_filters.h spl_array.h spl_directory.h spl_engine.h spl_exceptions.h spl_functions.h spl_iterators.h spl_observer.h spl_dllist.h spl_heap.h spl_fixedarray.h");

ext/spl/package.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ standard problems.
2626
<file role="src" name="php_spl.h"/>
2727
<file role="src" name="spl_array.c"/>
2828
<file role="src" name="spl_array.h"/>
29+
<file role="src" name="spl_filters.c"/>
30+
<file role="src" name="spl_filters.h"/>
2931
<file role="src" name="spl_directory.c"/>
3032
<file role="src" name="spl_directory.h"/>
3133
<file role="src" name="spl_engine.c"/>
@@ -34,6 +36,8 @@ standard problems.
3436
<file role="src" name="spl_functions.h"/>
3537
<file role="src" name="spl_iterators.c"/>
3638
<file role="src" name="spl_iterators.h"/>
39+
<file role="src" name="spl_validators.c"/>
40+
<file role="src" name="spl_validators.h"/>
3741
<file role="doc" name="CREDITS"/>
3842
<file role="doc" name="README"/>
3943
<file role="doc" name="TODO"/>

ext/spl/php_spl.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
#include "spl_dllist.h"
3838
#include "spl_fixedarray.h"
3939
#include "spl_heap.h"
40+
#include "spl_validators.h"
41+
#include "spl_filters.h"
4042
#include "zend_exceptions.h"
4143
#include "zend_interfaces.h"
4244
#include "ext/standard/php_rand.h"
@@ -195,6 +197,7 @@ PHP_FUNCTION(class_uses)
195197
SPL_ADD_CLASS(DomainException, z_list, sub, allow, ce_flags); \
196198
SPL_ADD_CLASS(EmptyIterator, z_list, sub, allow, ce_flags); \
197199
SPL_ADD_CLASS(FilesystemIterator, z_list, sub, allow, ce_flags); \
200+
SPL_ADD_CLASS(Filter, z_list, sub, allow, ce_flags); \
198201
SPL_ADD_CLASS(FilterIterator, z_list, sub, allow, ce_flags); \
199202
SPL_ADD_CLASS(GlobIterator, z_list, sub, allow, ce_flags); \
200203
SPL_ADD_CLASS(InfiniteIterator, z_list, sub, allow, ce_flags); \
@@ -239,6 +242,7 @@ PHP_FUNCTION(class_uses)
239242
SPL_ADD_CLASS(SplTempFileObject, z_list, sub, allow, ce_flags); \
240243
SPL_ADD_CLASS(UnderflowException, z_list, sub, allow, ce_flags); \
241244
SPL_ADD_CLASS(UnexpectedValueException, z_list, sub, allow, ce_flags); \
245+
SPL_ADD_CLASS(Validator, z_list, sub, allow, ce_flags); \
242246

243247
/* {{{ proto array spl_classes()
244248
Return an array containing the names of all clsses and interfaces defined in SPL */
@@ -939,7 +943,9 @@ PHP_MINIT_FUNCTION(spl)
939943
PHP_MINIT(spl_heap)(INIT_FUNC_ARGS_PASSTHRU);
940944
PHP_MINIT(spl_fixedarray)(INIT_FUNC_ARGS_PASSTHRU);
941945
PHP_MINIT(spl_observer)(INIT_FUNC_ARGS_PASSTHRU);
942-
946+
PHP_MINIT(spl_filters)(INIT_FUNC_ARGS_PASSTHRU);
947+
PHP_MINIT(spl_validators)(INIT_FUNC_ARGS_PASSTHRU);
948+
943949
return SUCCESS;
944950
}
945951
/* }}} */

ext/spl/spl_filters.c

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
+----------------------------------------------------------------------+
3+
| PHP Version 5 |
4+
+----------------------------------------------------------------------+
5+
| Copyright (c) 1997-2012 The PHP Group |
6+
+----------------------------------------------------------------------+
7+
| This source file is subject to version 3.01 of the PHP license, |
8+
| that is bundled with this package in the file LICENSE, and is |
9+
| available through the world-wide-web at the following url: |
10+
| http://www.php.net/license/3_01.txt |
11+
| If you did not receive a copy of the PHP license and are unable to |
12+
| obtain it through the world-wide-web, please send a note to |
13+
| [email protected] so we can mail you a copy immediately. |
14+
+----------------------------------------------------------------------+
15+
| Authors: Axel Etcheverry <[email protected]> |
16+
+----------------------------------------------------------------------+
17+
*/
18+
19+
/* $Id$ */
20+
21+
#ifdef HAVE_CONFIG_H
22+
# include "config.h"
23+
#endif
24+
25+
#include "php.h"
26+
#include "php_ini.h"
27+
#include "ext/standard/info.h"
28+
#include "zend_interfaces.h"
29+
30+
#include "php_spl.h"
31+
#include "spl_functions.h"
32+
#include "spl_engine.h"
33+
#include "spl_filters.h"
34+
35+
PHPAPI zend_class_entry *spl_ce_Filter;
36+
37+
ZEND_BEGIN_ARG_INFO(arginfo_filter_filter, 0)
38+
ZEND_ARG_INFO(0, value)
39+
ZEND_END_ARG_INFO()
40+
41+
static const zend_function_entry spl_funcs_Filter[] = {
42+
SPL_ABSTRACT_ME(Filter, filter, arginfo_filter_filter)
43+
PHP_FE_END
44+
};
45+
46+
/* {{{ PHP_MINIT_FUNCTION(spl_filters)
47+
*/
48+
PHP_MINIT_FUNCTION(spl_filters)
49+
{
50+
51+
REGISTER_SPL_INTERFACE(Filter);
52+
53+
return SUCCESS;
54+
}
55+
/* }}} */
56+

ext/spl/spl_filters.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
+----------------------------------------------------------------------+
3+
| PHP Version 5 |
4+
+----------------------------------------------------------------------+
5+
| Copyright (c) 1997-2012 The PHP Group |
6+
+----------------------------------------------------------------------+
7+
| This source file is subject to version 3.01 of the PHP license, |
8+
| that is bundled with this package in the file LICENSE, and is |
9+
| available through the world-wide-web at the following url: |
10+
| http://www.php.net/license/3_01.txt |
11+
| If you did not receive a copy of the PHP license and are unable to |
12+
| obtain it through the world-wide-web, please send a note to |
13+
| [email protected] so we can mail you a copy immediately. |
14+
+----------------------------------------------------------------------+
15+
| Authors: Axel Etcheverry <[email protected]> |
16+
+----------------------------------------------------------------------+
17+
*/
18+
19+
/* $Id$ */
20+
21+
#ifndef SPL_FILTER_H
22+
#define SPL_FILTER_H
23+
24+
#include "php.h"
25+
#include "php_spl.h"
26+
#if HAVE_PCRE || HAVE_BUNDLED_PCRE
27+
#include "ext/pcre/php_pcre.h"
28+
#endif
29+
30+
extern PHPAPI zend_class_entry *spl_ce_Filter;
31+
32+
PHP_MINIT_FUNCTION(spl_filters);
33+
34+
#endif /* SPL_FILTER_H */
35+
36+
/*
37+
* Local Variables:
38+
* c-basic-offset: 4
39+
* tab-width: 4
40+
* End:
41+
* vim600: fdm=marker
42+
* vim: noet sw=4 ts=4
43+
*/

ext/spl/spl_validators.c

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
+----------------------------------------------------------------------+
3+
| PHP Version 5 |
4+
+----------------------------------------------------------------------+
5+
| Copyright (c) 1997-2012 The PHP Group |
6+
+----------------------------------------------------------------------+
7+
| This source file is subject to version 3.01 of the PHP license, |
8+
| that is bundled with this package in the file LICENSE, and is |
9+
| available through the world-wide-web at the following url: |
10+
| http://www.php.net/license/3_01.txt |
11+
| If you did not receive a copy of the PHP license and are unable to |
12+
| obtain it through the world-wide-web, please send a note to |
13+
| [email protected] so we can mail you a copy immediately. |
14+
+----------------------------------------------------------------------+
15+
| Authors: Axel Etcheverry <[email protected]> |
16+
+----------------------------------------------------------------------+
17+
*/
18+
19+
/* $Id$ */
20+
21+
22+
#ifdef HAVE_CONFIG_H
23+
# include "config.h"
24+
#endif
25+
26+
#include "php.h"
27+
#include "php_ini.h"
28+
#include "ext/standard/info.h"
29+
#include "zend_interfaces.h"
30+
31+
#include "php_spl.h"
32+
#include "spl_functions.h"
33+
#include "spl_engine.h"
34+
#include "spl_validators.h"
35+
36+
PHPAPI zend_class_entry *spl_ce_Validator;
37+
38+
ZEND_BEGIN_ARG_INFO(arginfo_validator_void, 0)
39+
ZEND_END_ARG_INFO()
40+
41+
ZEND_BEGIN_ARG_INFO(arginfo_validator_is_valid, 0)
42+
ZEND_ARG_INFO(0, value)
43+
ZEND_END_ARG_INFO()
44+
45+
static const zend_function_entry spl_funcs_Validator[] = {
46+
SPL_ABSTRACT_ME(Validator, isValid, arginfo_validator_is_valid)
47+
SPL_ABSTRACT_ME(Validator, getMessages, arginfo_validator_void)
48+
PHP_FE_END
49+
};
50+
51+
/* {{{ PHP_MINIT_FUNCTION(spl_validators)
52+
*/
53+
PHP_MINIT_FUNCTION(spl_validators)
54+
{
55+
56+
REGISTER_SPL_INTERFACE(Validator);
57+
58+
return SUCCESS;
59+
}
60+
/* }}} */

ext/spl/spl_validators.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
+----------------------------------------------------------------------+
3+
| PHP Version 5 |
4+
+----------------------------------------------------------------------+
5+
| Copyright (c) 1997-2012 The PHP Group |
6+
+----------------------------------------------------------------------+
7+
| This source file is subject to version 3.01 of the PHP license, |
8+
| that is bundled with this package in the file LICENSE, and is |
9+
| available through the world-wide-web at the following url: |
10+
| http://www.php.net/license/3_01.txt |
11+
| If you did not receive a copy of the PHP license and are unable to |
12+
| obtain it through the world-wide-web, please send a note to |
13+
| [email protected] so we can mail you a copy immediately. |
14+
+----------------------------------------------------------------------+
15+
| Authors: Axel Etcheverry <[email protected]> |
16+
+----------------------------------------------------------------------+
17+
*/
18+
19+
/* $Id$ */
20+
21+
#ifndef SPL_VALIDATOR_H
22+
#define SPL_VALIDATOR_H
23+
24+
#include "php.h"
25+
#include "php_spl.h"
26+
27+
extern PHPAPI zend_class_entry *spl_ce_Validator;
28+
29+
PHP_MINIT_FUNCTION(spl_validators);
30+
31+
#endif /* SPL_VALIDATOR_H */

ext/spl/tests/filters_001.phpt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
SPL: Filter
3+
--FILE--
4+
<?php
5+
class TrimFilter implements Filter
6+
{
7+
public function filter($str)
8+
{
9+
return trim($str);
10+
}
11+
}
12+
13+
$o = new TrimFilter();
14+
15+
var_dump($o instanceof Filter) . PHP_EOL;
16+
var_dump($o->filter(' good '));
17+
?>
18+
--EXPECT--
19+
bool(true)
20+
string(4) "good"

ext/spl/tests/validator_001.phpt

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
--TEST--
2+
SPL: Validator
3+
--FILE--
4+
<?php
5+
class Test implements Validator
6+
{
7+
protected $messages = array();
8+
9+
public function isValid($value)
10+
{
11+
if($value == 'test') {
12+
return true;
13+
}
14+
15+
$this->messages[] = sprintf("Error : '%s' is not equal to 'test'", $value);
16+
17+
return false;
18+
}
19+
20+
public function getMessages()
21+
{
22+
return $this->messages;
23+
}
24+
}
25+
26+
$test = new Test();
27+
28+
var_dump($test instanceof Validator) . PHP_EOL;
29+
var_dump($test->isValid('test')) . PHP_EOL;
30+
var_dump($test->isValid('test1')) . PHP_EOL;
31+
var_dump($test->getMessages()) . PHP_EOL;
32+
?>
33+
--EXPECT--
34+
bool(true)
35+
bool(true)
36+
bool(false)
37+
array(1) {
38+
[0]=>
39+
string(38) "Error : 'test1' is not equal to 'test'"
40+
}

0 commit comments

Comments
 (0)