Code snippets for symfony 1.x

Navigation

Refine Tags

Snippets tagged "integer"

Validator for Hebrew-character integers

The Hebrew alphabet has its own traditional representation for integers. Here is a validator to clean them into conventional Arabic integers. There may be gotchas involved with the Unicode processing. I developed this on a 64-bit Ubuntu system.

<?php
class ynValidatorIntegerHebrew extends sfValidatorInteger
{
  protected function configure($options = array(), $messages = array())
  {
    parent::configure( $options, $messages );
 
    // value to add to Hebrew entries, useful for assumed millenia
    $this->addOption('add_to_hebrew', 0);
  }
 
  public function clean( $value )
  {
    if ( preg_match( '/^\p{Hebrew}+(?:"\p{Hebrew})?$/u', $value ) ) {
      $value = $this->convertStringToInt( $value );
    }
 
    return parent::clean( $value );
  }
 
  /**
   * Iterates through the Hebrew characters in the string, converting
   * each to its Unicode code and evaluating for numerical value
   *
   * @param string $value The integer represented in Hebrew characters
   * @return integer
   */
  protected function convertStringToInt( $value )
  {
    if ( ! is_string( $value ) ) {
      throw new UnexpectedValueException( 'Argument must be a string' );
    }
 
    preg_match_all( '/\p{Hebrew}/u', $value, $matches );
 
    $value_int = 0;
 
    foreach ( $matches[0] as $char ) {
      $char     = iconv( 'UTF-8', 'UTF-32', $char );
      $char_dec = unpack( 'L*', $char );
      $char_dec = $char_dec[2];
 
      if ( $char_dec > 1510 ) {
        $base = $char_dec - 1510;
        $value_int += $base * 100;
      }
      elseif ( $char_dec > 1496 ) {
        $base = $char_dec - 1496;
 
        // have to deal with final forms
        if ( $base > 2 ) $base--;
        if ( $base > 4 ) $base--;
        if ( $base > 5 ) $base--;
        if ( $base > 8 ) $base--;
        if ( $base > 9 ) $base--;
 
        $value_int += $base * 10;
      }
      else {
        $base = $char_dec - 1487;
        $value_int += $base;
      }
    }
 
    $value_int += $this->getOption('add_to_hebrew');
 
    return $value_int;
  }
}
 
// UNIT TEST
 
// The Hebrew literals are displaying oddly because of right-to-left; it should
// still work
 
require_once dirname(__FILE__).'/../../bootstrap/Doctrine.php';
 
$t = new lime_test();
 
$val = new ynValidatorIntegerHebrew();
 
$t->is( $val->clean('א'), 1 );
$t->is( $val->clean('ב'), 2 );
$t->is( $val->clean('ג'), 3 );
$t->is( $val->clean('ד'), 4 );
$t->is( $val->clean('ה'), 5 );
$t->is( $val->clean('ו'), 6 );
$t->is( $val->clean('ז'), 7 );
$t->is( $val->clean('ח'), 8 );
$t->is( $val->clean('ט'), 9 );
$t->is( $val->clean('י'), 10 );
$t->is( $val->clean('כ'), 20 );
$t->is( $val->clean('ך'), 20 );
$t->is( $val->clean('ל'), 30 );
$t->is( $val->clean('מ'), 40 );
$t->is( $val->clean('ם'), 40 );
$t->is( $val->clean('נ'), 50 );
$t->is( $val->clean('ן'), 50 );
$t->is( $val->clean('ס'), 60 );
$t->is( $val->clean('ע'), 70 );
$t->is( $val->clean('פ'), 80 );
$t->is( $val->clean('ף'), 80 );
$t->is( $val->clean('צ'), 90 );
$t->is( $val->clean('ץ'), 90 );
$t->is( $val->clean('ק'), 100 );
$t->is( $val->clean('ר'), 200 );
$t->is( $val->clean('ש'), 300 );
$t->is( $val->clean('ת'), 400 );
$t->is( $val->clean('תשע'), 770 );
$t->is( $val->clean('תתרתשע'), 1770 );
 
$val->setOption('add_to_hebrew', 5000);
$t->is( $val->clean('תשע'), 5770 );
 
by yitznewton on 2011-02-17, tagged hebrew  integer  validator