function testGetErrorMessages()
 {
     $this->validator->validate('TEXU3070077');
     $result = $this->validator->getErrorMessages();
     $expected = array('Check digit is not match');
     $this->assertIdentical($result, $expected);
     $this->validator->validate('');
     $result = $this->validator->getErrorMessages();
     $expected = array('Container number must be a string');
     $this->assertIdentical($result, $expected);
     $myValidator = new ContainerNumberValidator();
     $myValidator->getOwnerCode();
     $myValidator->getProductGroupCode();
     $myValidator->getRegistrationDigit();
     $myValidator->getCheckDigit();
     $result = $myValidator->getErrorMessages();
     $expected = array('You must call validate or isValid first', 'You must call validate or isValid first', 'You must call validate or isValid first', 'You must call validate or isValid first');
     $this->assertIdentical($result, $expected);
     $this->validator->clearErrors();
     $this->validator->generate('EP', 'ZZ', 0, 1);
     $result = $this->validator->getErrorMessages();
     $expected = 'Invalid owner code or product group code';
     $this->assertIdentical($result[0], $expected);
     $this->validator->clearErrors();
     $this->validator->generate('EPX', 'U', -1, 2);
     $result = $this->validator->getErrorMessages();
     $expected = 'Invalid number to generate, minimal is 0 and maximal is 999999';
     $this->assertIdentical($result[0], $expected);
     $this->validator->clearErrors();
     $this->validator->generate('EPX', '9', 1, 2);
     $result = $this->validator->getErrorMessages();
     $expected = array('Invalid container number', 'Error generating container number at number 1');
     $this->assertIdentical($result, $expected);
 }
        
        <p>
        <label for="to">To</label>
        <input type="text" name="data[to]" id="to" maxlength="6" size="6" />
        </p>
        
        <input type="submit" name="submit" value="Generate" />
    </form>
    <?php 
} else {
    // data being posted
    $owner_code = $_POST['data']['owner_code'];
    $product_group_code = $_POST['data']['product_group_code'];
    $from = $_POST['data']['from'];
    $to = $_POST['data']['to'];
    $validator = new ContainerNumberValidator();
    $containers = $validator->generate($owner_code, $product_group_code, $from, $to);
    $errors = $validator->getErrorMessages();
    echo '<a href="" title="generate again">Generate again</a><br />';
    if (empty($errors)) {
        foreach ($containers as $container) {
            echo $container . '<br />';
        }
    } else {
        $inflector = count($errors) > 1 ? 'There are some errors!' : 'There is an error!';
        echo "<h3>{$inflector}</h3>";
        echo '<ul>';
        foreach ($errors as $error) {
            echo '<li>' . $error . '</li>';
        }
        echo '</ul>';
Пример #3
0
<?php

$start_all = xdebug_time_index();
require_once 'container_number_validator.php';
$validator = new ContainerNumberValidator();
echo '<h1>Example of valid container number : TEXU3070079</h1>';
// test valid container
// if number is valid, validate() will return array of segment code otherwise an empty array returned
$codeSegment = $validator->validate('TEXU3070079');
print_r($codeSegment);
// will return true on valid number
var_dump($validator->isValid('TEXU3070079'));
echo '<hr />';
echo '<h1>Example of invalid check digit : TEXU3070070</h1>';
// example of invalid container number, will return an empty array
$codeSegment = $validator->validate('TEXU3070070');
print_r($codeSegment);
var_dump($validator->isValid('TEXU3070070'));
// get error messages
var_dump($validator->getErrorMessages());
echo '<hr />';
echo '<h1>Example of getting owner code, product group code, registration digit and check digit from valid container number : TEXU3070079</h1>';
// validate() or isValid() must be called before getting segmentCode
if ($validator->isValid('TEXU3070079')) {
    echo 'Owner code: ' . implode('', $validator->getOwnerCode()) . '<br />';
    echo 'Product group code: ' . $validator->getProductGroupCode() . '<br />';
    echo 'Registration digit: ' . implode('', $validator->getRegistrationDigit()) . '<br />';
    echo 'Check digit: ' . $validator->getCheckDigit() . '<br />';
}
echo '<hr />';
echo '<h1>Example of creating check digit from container number without check digit: TEXU307007</h1>';