/** * 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); static $files = array(); if (!count($files)) { $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); } /* if ($htmlspecialchars) { $entities = array( '&' => '&', '<' => '<', '>' => '>', //'\'' => ''', '"' => '"', ); // 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(); reset($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); */ $class = null; $schema = null; if (isset($types[$type_or_action])) { QuickBooks_Loader::load('/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])) { QuickBooks_Loader::load('/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])) { QuickBooks_Loader::load('/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 ($class and $schema) { 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: if ($value) { $value = date('Y-m-d', strtotime($value)); } break; case QUICKBOOKS_DATATYPE_DATETIME: if ($value) { $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 and $value !== 'false') { $value = 'true'; } else { $value = 'false'; } break; case QUICKBOOKS_DATATYPE_INTEGER: $value = (int) $value; break; } } } /* if ($htmlspecialchars) { $entities = array( '&' => '&', '<' => '<', '>' => '>', //'\'' => ''', '"' => '"', ); // 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); } */ if ($htmlspecialchars) { //print("DECODING"); $entities = array('&' => '&', '<' => '<', '>' => '>', '"' => '"'); // 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); //print($value . "\n\n\n"); // UTF8 character handling, decode UTF8 to character decimal codes $value = QuickBooks_Cast::_decodeUTF8($value); //die($value . "\n\n"); } return $value; }