Пример #1
0
 /**
  * Cast a value to ensure that it will fit in a particular field within QuickBooks
  * 
  * QuickBooks has some strange length limits on some fields (the max. 
  * length of the CompanyName field for Customers is only 41 characters, 
  * etc.) so this method provides an easy way to cast the data type and data 
  * length of a value to the correct type and length for a specific field.
  * 
  * @param string $object_type	The QuickBooks object type (Customer, Invoice, etc.)
  * @param string $field_name	The QuickBooks field name (these correspond to the qbXML field names: Addr1, Name, CompanyName, etc.)
  * @param mixed $value			The value you want to cast
  * @param boolean $use_abbrevs	There are a lot of strings which can be abbreviated to shorten lengths, this is whether or not you want to use those abbrevaitions ("University" to "Univ.", "Incorporated" to "Inc.", etc.)
  * @param boolean $htmlspecialchars
  * @return string
  */
 public static function cast($type_or_action, $field, $value, $use_abbrevs = true, $htmlspecialchars = true)
 {
     $type_or_action = strtolower($type_or_action);
     if ($htmlspecialchars) {
         $entities = array('&' => '&amp;', '<' => '&lt;', '>' => '&gt;', '\'' => '&apos;', '"' => '&quot;');
         // First, *unreplace* things so that we don't double escape them
         $value = str_replace(array_values($entities), array_keys($entities), $value);
         // Then, replace XML entities
         $value = str_replace(array_keys($entities), array_values($entities), $value);
         //$value = htmlspecialchars($value, ENT_QUOTES, null, false);
     }
     $types = array();
     $types3 = array();
     $types5 = array();
     $files = array();
     // @TODO Optimize this so that it doesn't have to read this directory on *every* cast (singleton pattern)
     $dh = opendir(dirname(__FILE__) . '/QBXML/Schema/Object');
     while (false !== ($file = readdir($dh))) {
         if ($file[0] == '.' or substr($file, -6, 6) != 'Rq.php') {
             continue;
         }
         $files[] = $file;
     }
     sort($files);
     foreach ($files as $file) {
         $substr = substr($file, 0, -4);
         $substrlower = strtolower($substr);
         $types[$substrlower] = $substr;
         $substr3 = substr($file, 0, -3 + -3);
         $substr3lower = strtolower($substr3);
         $substr5 = substr($file, 0, -3 + -6);
         $substr5lower = strtolower($substr5);
         if (!isset($types3[$substr3lower])) {
             $types3[$substr3lower] = $substr;
         }
         if (!isset($types5[$substr5lower])) {
             $types5[$substr5lower] = $substr;
         }
     }
     /*
     print('	looking for schema: ' . $type_or_action . "\n");
     print_r($types);
     print_r($types3);
     print_r($types5);
     */
     if (isset($types[$type_or_action])) {
         require_once 'QuickBooks/QBXML/Schema/Object/' . $types[$type_or_action] . '.php';
         $class = 'QuickBooks_QBXML_Schema_Object_' . $types[$type_or_action];
         $schema = new $class();
     } else {
         if (isset($types3[$type_or_action])) {
             require_once 'QuickBooks/QBXML/Schema/Object/' . $types3[$type_or_action] . '.php';
             $class = 'QuickBooks_QBXML_Schema_Object_' . $types3[$type_or_action];
             $schema = new $class();
         } else {
             if (isset($types5[$type_or_action])) {
                 require_once 'QuickBooks/QBXML/Schema/Object/' . $types5[$type_or_action] . '.php';
                 $class = 'QuickBooks_QBXML_Schema_Object_' . $types5[$type_or_action];
                 $schema = new $class();
             } else {
                 return $value;
             }
         }
     }
     //print('	casting using schema: ' . get_class($schema) . "\n");
     if (!$schema->exists($field) and false !== strpos($field, '_')) {
         $field = str_replace('_', ' ', $field);
     }
     if ($schema->exists($field)) {
         switch ($schema->dataType($field)) {
             case QUICKBOOKS_DATATYPE_STRING:
                 $maxlength = $schema->maxLength($field);
                 // Use only ASCII characters
                 $value = QuickBooks_Cast::_castCharset($value);
                 // Make sure it'll fit in the allocated field length
                 if (is_int($maxlength) and $maxlength > 0) {
                     $value = QuickBooks_Cast::_castTruncate($value, $maxlength, $use_abbrevs);
                 }
                 break;
             case QUICKBOOKS_DATATYPE_DATE:
                 $value = date('Y-m-d', strtotime($value));
                 break;
             case QUICKBOOKS_DATATYPE_DATETIME:
                 $value = date('Y-m-d', strtotime($value)) . 'T' . date('H:i:s', strtotime($value));
                 break;
             case QUICKBOOKS_DATATYPE_ENUM:
                 // do nothing
                 break;
             case QUICKBOOKS_DATATYPE_ID:
                 // do nothing
                 break;
             case QUICKBOOKS_DATATYPE_FLOAT:
                 $value = (double) $value;
                 break;
             case QUICKBOOKS_DATATYPE_BOOLEAN:
                 if ($value) {
                     $value = 'true';
                 } else {
                     $value = 'false';
                 }
                 break;
             case QUICKBOOKS_DATATYPE_INTEGER:
                 $value = (int) $value;
                 break;
         }
     }
     /*
     if ($htmlspecialchars)
     {			
     	$entities = array(
     		'&' => '&amp;', 
     		'<' => '&lt;', 
     		'>' => '&gt;',
     		'\'' => '&apos;', 
     		'"' => '&quot;', 
     		);
     	
     	// First, *unreplace* things so that we don't double escape them
     	$value = str_replace(array_values($entities), array_keys($entities), $value);
     	
     	// Then, replace XML entities
     	$value = str_replace(array_keys($entities), array_values($entities), $value);
     	
     	//$value = htmlspecialchars($value, ENT_QUOTES, null, false);
     }
     */
     return $value;
 }