/** * Convert Datetime String from Iso8601 to Sql Format * * @author Everton Yoshitani <*****@*****.**> * @since 1.0 * @param array $data * @return array */ public function convertIso8601ToSqlDatetime($data = array()) { foreach ($data as $attribute => $value) { if (empty($this->_attributes['original'][$attribute]) || is_array($value) || empty($this->_attributes['original'][$attribute]['type']) || $this->_attributes['original'][$attribute]['type'] !== 'datetime') { continue; } $field = $this->_attributes['original'][$attribute]; $data[$attribute] = CakeTime::toServer($value, $this->_timezone); } return $data; }
/** * Parse Request Query Parameters to Model Conditions * * @author Everton Yoshitani <*****@*****.**> * @since 1.0 * @return array */ public function rendersConditions() { $field_map = $this->_field_map; $this->withFieldMap(); if (empty($field_map)) { return array(); } $query = $this->_passed_params; $this->withPassedParams(); if (empty($query)) { $query = $this->Controller->request->query; } if (empty($query)) { return array(); } $model = $this->_model; $this->onModel(); if (empty($model)) { $model = $this->Controller->modelClass; } if (!isset($this->{$model})) { $this->{$model} = ClassRegistry::init($model); } $timezone = $this->getTimezone(); $attributes = $this->{$model}->attributes(); $conditions = array(); foreach ($query as $condition => $value) { $attribute = $condition; $prefix_match = null; $prefixes = $this->prefixes(); foreach ($prefixes as $prefix) { $match = $prefix . '-'; $strlen = strlen($match); if (!strncmp($condition, $match, $strlen)) { $prefix_match = $prefix; $attribute = substr($condition, $strlen); } } $field = array_search($attribute, $field_map); if (empty($field)) { continue; } if (in_array($field, $this->_reserved_query_parameters)) { continue; } if (empty($prefix_match)) { if (strstr($value, '|') && strpos($value, '"') !== 0) { $value = explode('|', $value); } elseif (strpos($value, '"') === 0 && strrpos($value, '"') === strlen($value) - 1) { $value = substr($value, 1, -1); } } $is_value_array = is_array($value); if (!$is_value_array) { $value = array($value); } if ($attributes[$attribute]['query'] === false) { continue; } $type = null; if (!empty($attributes[$attribute]['type'])) { $type = $attributes[$attribute]['type']; } if ($type === 'datetime') { foreach ($value as $value_key => $value_val) { $value[$value_key] = CakeTime::toServer($value_val, $timezone); } } $field_options = $this->{$model}->getOptions($field); if (!empty($field_options)) { foreach ($value as $key => $individual_value) { if (in_array($individual_value, $field_options)) { $value[$key] = array_search($individual_value, $field_options); } } } if (!$is_value_array) { $value = $value[0]; } switch ($prefix_match) { case 'not': $value = $this->onModel($model)->onField($field)->withValue($value)->returnsFormattedValue(); if ($value === 'null') { $conditions["{$model}.{$field} !="] = NULL; } else { $conditions[] = array('OR' => array("{$model}.{$field} !=" => $value, "{$model}.{$field}" => NULL)); } break; case 'min': $value = $this->onModel($model)->onField($field)->withValue($value)->returnsFormattedValue(); $conditions["{$model}.{$field} >="] = $value; break; case 'max': $value = $this->onModel($model)->onField($field)->withValue($value)->returnsFormattedValue(); $conditions["{$model}.{$field} <="] = $value; break; case 'contains': if ($type === 'string') { $value = str_replace('%', '\\\\%', $value); $conditions["{$model}.{$field} LIKE"] = "%{$value}%"; } break; case 'not-contains': if ($type === 'string') { $value = str_replace('%', '\\\\%', $value); $conditions["{$model}.{$field} NOT LIKE"] = "%{$value}%"; } break; case 'starts-with': if ($type === 'string') { $value = str_replace('%', '\\\\%', $value); $conditions["{$model}.{$field} LIKE"] = "{$value}%"; } break; case 'ends-with': if ($type === 'string') { $value = str_replace('%', '\\\\%', $value); $conditions["{$model}.{$field} LIKE"] = "%{$value}"; } break; default: $value = $this->onModel($model)->onField($field)->withValue($value)->returnsFormattedValue(); if (empty($value)) { $value = false; } if ($value === 'null') { $conditions[] = array('OR' => array("{$model}.{$field}" => '', "{$model}.{$field}" => NULL)); } else { $conditions["{$model}.{$field}"] = $value; } break; } } return $conditions; }
/** * Test Convert Iso8601 To Sql Datetime - Results * * @author Everton Yoshitani <*****@*****.**> * @author Anthony Putignano <*****@*****.**> * @since 1.0 * @return void */ public function testConvertIso8601ToSqlDatetime() { $data = array('id' => 1, 'name' => 'Name', 'tags' => array('one', 'two', 'three'), 'lastView.tricky' => '2013-03-05T13:56:02+0900', 'created' => '2013-03-01T13:00:00+0000'); $this->InputData->_timezone = 'UTC'; $this->InputData->_ModelObject = $this->getMock('ApiInputDataComponentThing'); $this->InputData->_model = $this->test_model; $this->InputData->_attributes['original'] = array('id' => array('type' => 'integer'), 'name' => array('type' => 'text'), 'tag' => array('type' => 'text'), 'lastView.tricky' => array('field' => 'last_view.tricky', 'type' => 'datetime'), 'created' => array('type' => 'datetime')); $result = $this->InputData->convertIso8601ToSqlDatetime($data); $expected = $data; $expected['lastView.tricky'] = CakeTime::toServer($expected['lastView.tricky'], 'UTC'); $expected['created'] = CakeTime::toServer($expected['created'], 'UTC'); $this->assertEquals($expected, $result); }