예제 #1
0
 /**
  * Property writing accessor
  * @param string $propertyName The property name
  * @param string $value The property value
  */
 public function setProperty($propertyName, $value)
 {
     // Non-null value
     if ($value !== NULL) {
         // Numeric value
         if (StringTool::isInt($value)) {
             $value = StringTool::toInt($value, false);
         } else {
             if (StringTool::isFloat($value, FALSE)) {
                 $value = StringTool::toFloat($value, false);
             } else {
                 if (StringTool::endsWith($propertyName, 'date')) {
                     // Date has a 10 length (YYYY-mm-dd)
                     if (StringTool::strlen($value) == 10) {
                         $value = DateTool::stringToTimestamp($value, DateTool::FORMAT_MYSQL_DATE);
                     } else {
                         $value = DateTool::stringToTimestamp($value);
                     }
                 }
             }
         }
         // Day property type
     }
     // Removes table name at the beginning of field name, not for id fields nor xxx_has_xxx tables
     $tableName = DatabaseFactory::getElementTableName($this->getElementClass());
     if (!StringTool::contains($tableName, ElementFactory::TABLE_JOIN_SEPARATOR)) {
         $tablePrefix = $tableName . '_';
         $tableIdField = $tablePrefix . 'id';
         if (StringTool::startsWith($propertyName, $tablePrefix) && (!StringTool::endsWith($propertyName, '_id') || $propertyName == $tableIdField)) {
             $propertyName = StringTool::truncateFirstChars($propertyName, StringTool::strlen($tablePrefix));
         }
     }
     // Updates original property list
     if (!ArrayTool::array_key_exists($propertyName, $this->propertyList)) {
         // It's the first time this property gets a value, it will be updated (set a null value to original property list)
         $this->originalPropertyList[$propertyName] = NULL;
     } else {
         if (ArrayTool::array_key_exists($propertyName, $this->originalPropertyList)) {
             // Attribute value had already changed (originalPropertyList already has a value for this property)
             // If value has been reset to original value, removes the update of the property
             if ($value == $this->originalPropertyList[$propertyName]) {
                 unset($this->originalPropertyList[$propertyName]);
             }
         } else {
             if ($value !== $this->propertyList[$propertyName]) {
                 // If value has changed, updates original value
                 $this->originalPropertyList[$propertyName] = $this->propertyList[$propertyName];
             }
         }
     }
     // Sets property new value
     $this->propertyList[$propertyName] = $value;
 }
 /**
  * Checks type of a parameter value and throws an exception if value doesn't match type
  * @param mixed $paramValue The parameter value (passed by reference)
  * @param string $type The parameter type (RequestTool::PARAM_TYPE_INT, RequestTool::PARAM_TYPE_STRING, etc).
  * @return mixed The parameter value (eventually modified)
  */
 public static function checkParameterValue(&$paramValue, $type)
 {
     // Checks value type
     if ($type != RequestTool::PARAM_TYPE_ARRAY && !mb_ereg($type, $paramValue)) {
         throw new ParameterException('Invalid parameter format: "' . $paramValue . '" must be a ' . $type . '');
     }
     // Converts value to numeric
     switch ($type) {
         case RequestTool::PARAM_TYPE_UNSIGNED_INT:
         case RequestTool::PARAM_TYPE_INT:
             $paramValue = StringTool::toInt($paramValue);
             break;
         case RequestTool::PARAM_TYPE_UNSIGNED_FLOAT:
         case RequestTool::PARAM_TYPE_FLOAT:
             $paramValue = StringTool::toFloat($paramValue);
             break;
         case RequestTool::PARAM_TYPE_BOOLEAN:
             $paramValue = 1 == $paramValue ? true : false;
             break;
         case RequestTool::PARAM_TYPE_ARRAY:
             $paramValue = is_array($paramValue) ? $paramValue : false;
             break;
     }
     return $paramValue;
 }
 /**
  * Gets property value in correct format for request
  * @param string $propertyName The name of the property to detect field type
  * @param string $propertyValue The property value
  * @return string the value in correct format for the request
  */
 private static function getFormattedPropertyValue($elementClass, $propertyName, $propertyValue)
 {
     // NULL value
     if ($propertyValue === null) {
         $formattedValue = 'NULL';
     } else {
         $formattedValue = '\'';
         if (StringTool::isInt($propertyValue)) {
             // Datetime / date property type
             if (StringTool::endsWith($propertyName, 'date')) {
                 $formattedValue .= DateTool::timestampToString($propertyValue);
             } else {
                 $formattedValue .= StringTool::toInt($propertyValue, FALSE);
             }
         } else {
             if (StringTool::isFloat($propertyValue, FALSE)) {
                 $formattedValue .= StringTool::toFloat($propertyValue, FALSE);
             } else {
                 $formattedValue .= $elementClass::getDatabaseConnection()->realEscapeString($propertyValue);
             }
         }
         $formattedValue .= '\'';
     }
     return $formattedValue;
 }