Changeset 964

Show
Ignore:
Timestamp:
10/21/07 18:12:03 (9 months ago)
Author:
mikey
Message:

BC-BREAK: fixed ticket #86: net.stubbles.util.validators.stubRegexValidator now works with delimiters as well

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/main/php/net/stubbles/service/jsonrpc/stubJsonRpcProcessor.php

    r955 r964  
    2929     * Regexp to validate method param 
    3030     */ 
    31     const CLASS_AND_METHOD_PATTERN = '^([a-zA-Z0-9_]+\.[a-zA-Z0-9_]+)$'; 
     31    const CLASS_AND_METHOD_PATTERN = '/^([a-zA-Z0-9_]+\.[a-zA-Z0-9_]+)$/'; 
    3232    /** 
    3333     * Regexp to validate param param 
    3434     */ 
    35     const PARAM_PATTERN            = '^[a-zA-Z0-9_]+$'; 
     35    const PARAM_PATTERN            = '/^[a-zA-Z0-9_]+$/'; 
    3636    /** 
    3737     * Regexp to validate id param 
    3838     */ 
    39     const ID_PATTERN               = '^\d{6,7}$'; 
     39    const ID_PATTERN               = '/^\d{6,7}$/'; 
    4040    /** 
    4141     * Registry key for the service config file 
     
    7979 
    8080        if ($this->request->hasValue('__generateProxy')) { 
    81             $proxyClassvalidator = new stubRegexValidator('^[A-Za-z,0-9_\.]+$'); 
     81            $proxyClassvalidator = new stubRegexValidator('/^[A-Za-z,0-9_\.]+$/'); 
    8282            $classes = $this->request->getValidatedValue($proxyClassvalidator, '__generateProxy', stubRequest::SOURCE_PARAM); 
    8383            if ($classes === '__all') { 
     
    8787            } 
    8888        } elseif ($this->request->hasValue('__smd')) { 
    89             $smdClassvalidator = new stubRegexValidator('^[A-Za-z0-9_\.]+$'); 
     89            $smdClassvalidator = new stubRegexValidator('/^[A-Za-z0-9_\.]+$/'); 
    9090            $class = $this->request->getValidatedValue($smdClassvalidator, '__smd', stubRequest::SOURCE_PARAM); 
    9191            $this->generateSmd($class); 
     
    304304    { 
    305305        if ($className === null) { 
    306             if (!preg_match('/'.self::CLASS_AND_METHOD_PATTERN.'/', $methodName)) { 
     306            if (!preg_match(self::CLASS_AND_METHOD_PATTERN, $methodName)) { 
    307307                throw new stubException('Invalid request: method-Pattern has to be <className>.<methodName>.'); 
    308308            } 
  • trunk/src/main/php/net/stubbles/util/validators/stubRegexValidator.php

    r731 r964  
    1212 * 
    1313 * The validator uses preg_match() and checks if the value occurs exactly 
    14  * one time. Please note that a delimiter is automatically applied. The used 
    15  * delimiter is /, therefore it must be escaped if this character is part of 
    16  * the regular expression
     14 * one time. Please make sure that the supplied regular expresion contains 
     15 * correct delimiters, they will not be applied automatically. The validate() 
     16 * method throws a runtime exception in case the regular expression is invalid
    1717 * 
    1818 * @package     stubbles 
     
    3030    /** 
    3131     * constructor 
    32      * 
    33      * Please note that a delimiter is automatically applied. The used 
    34      * delimiter is /, therefore it must be escaped if this character 
    35      * is part of the regular expression. 
    3632     * 
    3733     * @param  string  $regex  regular expression to use for validation 
     
    5753     * @param   mixed  $value 
    5854     * @return  bool   true if value complies with regular expression, else false 
     55     * @throws  stubRuntimeException  in case the used regular expresion is invalid 
    5956     */ 
    6057    public function validate($value) 
    6158    { 
    62         if (preg_match('/' . $this->regex . '/', $value) != 1) { 
    63             return false; 
     59        $check = @preg_match($this->regex, $value); 
     60        if (false === $check) { 
     61            throw new stubRuntimeException('Invalid regular expression ' . $this->regex); 
    6462        } 
    65  
    66         return true
     63         
     64        return ((1 != $check) ? (false) : (true))
    6765    } 
    6866 
  • trunk/src/main/php/net/stubbles/websites/processors/stubAbstractProcessor.php

    r799 r964  
    124124         
    125125        if ($this->request->hasValue('page') == true) { 
    126             $pageName = $this->request->getValidatedValue(new stubRegexValidator('([a-zA-Z0-9_])'), 'page'); 
     126            $pageName = $this->request->getValidatedValue(new stubRegexValidator('/([a-zA-Z0-9_])/'), 'page'); 
    127127            if (null == $pageName || $this->pageFactory->hasPage($dirPrefix . $pageName) == false) { 
    128128                $pageName = 'index'; 
  • trunk/src/test/php/net/stubbles/integration/ValidatorsXJConfTestCase.php

    r959 r964  
    129129        $regexValidator = $this->xjconf->getConfigValue('regex'); 
    130130        $this->assertIsA($regexValidator, 'stubRegexValidator'); 
    131         $this->assertEqual($regexValidator->getCriteria(), array('regex' => '([a-Z]){1,3}')); 
     131        $this->assertEqual($regexValidator->getCriteria(), array('regex' => '/([a-Z]){1,3}/')); 
    132132    } 
    133133 
  • trunk/src/test/php/net/stubbles/ipo/request/broker/annotations/stubStringFilterAnnotationTestCase.php

    r736 r964  
    5151        $this->stringFilterAnnotation->setMinLength(1); 
    5252        $this->stringFilterAnnotation->setMaxLength(2); 
    53         $this->stringFilterAnnotation->setRegex('foo'); 
     53        $this->stringFilterAnnotation->setRegex('/foo/'); 
    5454        $stringFilter = $this->stringFilterAnnotation->getFilter(); 
    5555        $this->assertIsA($stringFilter, 'stubStringFilter'); 
     
    5959        $this->assertEqual($stringFilter->getMaxLengthValidator()->getValue(), 2); 
    6060        $this->assertIsA($stringFilter->getRegexValidator(), 'stubRegexValidator'); 
    61         $this->assertEqual($stringFilter->getRegexValidator()->getValue(), 'foo'); 
     61        $this->assertEqual($stringFilter->getRegexValidator()->getValue(), '/foo/'); 
    6262    } 
    6363 
  • trunk/src/test/php/net/stubbles/util/validators/stubRegexValidatorTestCase.php

    r696 r964  
    11<?php 
    22/** 
    3  * Tests for net.stubbles.util.validators.stubRegexValidator 
     3 * Tests for net.stubbles.util.validators.stubRegexValidator. 
    44 * 
    5  * @author      Frank Kleine <frank@kl-s.com
     5 * @author      Frank Kleine <mikey@stubbles.net
    66 * @package     stubbles 
    77 * @subpackage  util_validators_test 
     
    99stubClassLoader::load('net.stubbles.util.validators.stubRegexValidator'); 
    1010/** 
    11  * Tests for net.stubbles.util.validators.stubRegexValidator 
     11 * Tests for net.stubbles.util.validators.stubRegexValidator. 
    1212 * 
    1313 * @package     stubbles 
     
    2929    { 
    3030        // regex allows only three lowercase characters 
    31         $this->regexValidator = new stubRegexValidator('^([a-z]{3})$'); 
     31        $this->regexValidator = new stubRegexValidator('/^([a-z]{3})$/'); 
    3232    } 
    3333 
     
    4444 
    4545    /** 
     46     * assure that validation using modifiers in regular expression works correct 
     47     */ 
     48    public function testValidationWithModifier() 
     49    { 
     50        $regexValidator = new stubRegexValidator('/^([a-z]{3})$/i'); 
     51        $this->assertTrue($regexValidator->validate('foo')); 
     52        $this->assertTrue($regexValidator->validate('Bar')); 
     53        $this->assertFalse($regexValidator->validate('baz0123')); 
     54        $this->assertFalse($regexValidator->validate(null)); 
     55    } 
     56 
     57    /** 
     58     * assert that a runtime exception is thrown in case an invalid regular 
     59     * expression is used 
     60     */ 
     61    public function testInvalidRegex() 
     62    { 
     63        $regexValidator = new stubRegexValidator('^([a-z]{3})$'); 
     64        $this->expectException('stubRuntimeException'); 
     65        $regexValidator->validate('foo'); 
     66    } 
     67 
     68    /** 
    4669     * assure that returning the criterias works correct 
    4770     */ 
    4871    public function testGetCriteria() 
    4972    { 
    50         $this->assertEqual($this->regexValidator->getCriteria(), array('regex' => '^([a-z]{3})$')); 
     73        $this->assertEqual($this->regexValidator->getCriteria(), array('regex' => '/^([a-z]{3})$/')); 
    5174    } 
    5275} 
  • trunk/src/test/resources/xjconf/validators.xml

    r959 r964  
    1818    </values> 
    1919  </preSelect> 
    20   <regex setterMethod="regex">([a-Z]){1,3}</regex> 
     20  <regex setterMethod="regex">/([a-Z]){1,3}/</regex> 
    2121   
    2222  <and setterMethod="and">