public function testValidate()
 {
     $service = new ValidationService();
     $object = new AnnotationTestClass();
     $context = $service->validate($object);
     $expected = array('AnnotationTestClass.publicField' => array('Value must be null!'));
     $this->assertEquals($expected, $context->getMessages());
     $context = $service->validate($object, array('protected'));
     $expected = array('AnnotationTestClass.privateField' => array('Value must not be null!'), 'AnnotationTestClass.protectedField' => array('Value must not be null!'), 'AnnotationTestClass.publicField' => array('Value must be null!'));
     $this->assertEquals($expected, $context->getMessages());
 }
 public function SetPassword($value, $repeatValue)
 {
     // Check if passwords match
     if ($value != $repeatValue) {
         ValidationService::AddValidationError(self::$constraints['password']['doNotMatchMsg']);
         return false;
     }
     // Check if password is valid
     if ($this->IsValidString("Password", $value, self::$constraints["password"])) {
         // Set password
         $this->password = trim($value);
         return true;
     }
     return false;
 }
Ejemplo n.º 3
0
<?php

/**
 * Mock validation service.
 *
 * All services must send back:
 * 1. An HTTP return code indicating the outcome of the service. This codes is
 * then interpreted by the Core based on the extension configuration to further
 * route the request.
 * 2. A body consisting of a raw string with the input data transformed, OR
 * an empty result (such as in the case of this example, where data is not
 * manuipulated).
 */
// Execute main method.
//echo "Validation service";
$m = new ValidationService();
$m->main();
/**
 * Validation class.
 *
 * It only implements two validation methods: data type and cardinality.
 */
class ValidationService
{
    function main()
    {
        if ($this->validate()) {
            echo "\nValidation: Validation pass.";
            http_response_code(204);
        } else {
            echo "\nValidation: Validation failed.";
Ejemplo n.º 4
0
 protected function IsValidString($stringName, $stringContent, $constraints = [])
 {
     // Default values
     if (!isset($constraints['minLength'])) {
         $constraints['minLength'] = 1;
     }
     if (!isset($constraints['maxLength'])) {
         $constraints['maxLength'] = 100;
     }
     if (!isset($constraints['regex'])) {
         $constraints['regex'] = '/[^a-z_\\-0-9]/i';
     }
     if (!isset($constraints['throwException'])) {
         $constraints['throwException'] = false;
     }
     // Default messages
     if (!isset($constraints['emptyMsg'])) {
         $constraints['emptyMsg'] = "{$stringName} is missing";
     }
     if (!isset($constraints['minLengthMsg'])) {
         $constraints['minLengthMsg'] = "{$stringName} has too few characters, at least " . $constraints['minLength'] . " characters.";
     }
     if (!isset($constraints['maxLengthMsg'])) {
         $constraints['maxLengthMsg'] = "{$stringName} is too long. Max length is " . $constraints['maxLength'] . " characters.";
     }
     if (!isset($constraints['regexMsg'])) {
         $constraints['regexMsg'] = "{$stringName} contains invalid characters.";
     }
     // Check if $stringContent is empty
     if ($constraints['minLength'] == 1 && trim(strlen($stringContent)) == 0) {
         // Throw exception if specified
         if ($constraints['throwException']) {
             throw new \Exception($constraints['emptyMsg']);
         }
         ValidationService::AddValidationError($constraints['emptyMsg']);
     }
     // Check if $stringContent is too short
     if ($constraints['minLength'] > 1 && trim(strlen($stringContent)) < $constraints['minLength']) {
         // Throw exception if specified
         if ($constraints['throwException']) {
             throw new \Exception($constraints['minLengthMsg']);
         }
         ValidationService::AddValidationError($constraints['minLengthMsg']);
     }
     // Check if $stringContent is too long
     if (strlen($stringContent) > $constraints['maxLength']) {
         // Throw exception if specified
         if ($constraints['throwException']) {
             throw new \Exception($constraints['maxLengthMsg']);
         }
         ValidationService::AddValidationError($constraints['maxLength']);
     }
     // Check if $stringContent is valid
     if (preg_match($constraints['regex'], $stringContent)) {
         // Throw exception if specified
         if ($constraints['throwException']) {
             throw new \Exception($constraints['regexMsg']);
         }
         ValidationService::AddValidationError($constraints['regexMsg']);
     }
     return true;
 }
Ejemplo n.º 5
0
 protected function IsClassType($objName, $objContent, $constraints = [])
 {
     // Return false if classType is not defined
     if (!isset($constraints['classType'])) {
         $constraints['classType'] = 'Unspecified';
     }
     // Default settings
     if (!isset($constraints['allowNull'])) {
         $constraints['allowNull'] = false;
     }
     // Do not throw exception as default
     if (!isset($constraints['throwException'])) {
         $constraints['throwException'] = false;
     }
     // Default messages
     if (!isset($constraints['notClassTypeMsg'])) {
         $constraints['notClassTypeMsg'] = "{$objName} måste vara ett objekt av typen: " . $constraints['classType'];
     }
     // Check if its a valid class
     if (!($constraints['allowNull'] && is_null($objContent)) && !$objContent instanceof $constraints['classType']) {
         // Throw exception if specified
         if ($constraints['throwException']) {
             throw new \Exception($constraints['notClassTypeMsg']);
         }
         ValidationService::AddValidationError($constraints['notClassTypeMsg']);
         return false;
     }
     return true;
 }