[Solar-svn] Revision 3128
pmjones at solarphp.com
pmjones at solarphp.com
Fri Apr 25 08:16:48 CDT 2008
Solar_Filter_SanitizeIso(Date|Time|Timestamp): [ADD] These filters now accept the date/time/timestamp value as an array of parts keyed on their date() format characters (Y, m, d, H, i, s) and convert that array to a string.
Modified: trunk/Solar/Filter/SanitizeIsoDate.php
===================================================================
--- trunk/Solar/Filter/SanitizeIsoDate.php 2008-04-25 13:10:55 UTC (rev 3127)
+++ trunk/Solar/Filter/SanitizeIsoDate.php 2008-04-25 13:16:47 UTC (rev 3128)
@@ -24,7 +24,7 @@
* @package Solar_Filter
*
*/
-class Solar_Filter_SanitizeIsoDate extends Solar_Filter_Abstract {
+class Solar_Filter_SanitizeIsoDate extends Solar_Filter_SanitizeIsoTimestamp {
/**
*
@@ -39,6 +39,11 @@
*/
public function sanitizeIsoDate($value)
{
+ // look for Ymd keys?
+ if (is_array($value)) {
+ $value = $this->_arrayToDate($value);
+ }
+
// if the value is not required, and is blank, sanitize to null
$null = ! $this->_filter->getRequire() &&
$this->_filter->validateBlank($value);
Modified: trunk/Solar/Filter/SanitizeIsoTime.php
===================================================================
--- trunk/Solar/Filter/SanitizeIsoTime.php 2008-04-25 13:10:55 UTC (rev 3127)
+++ trunk/Solar/Filter/SanitizeIsoTime.php 2008-04-25 13:16:47 UTC (rev 3128)
@@ -24,7 +24,7 @@
* @package Solar_Filter
*
*/
-class Solar_Filter_SanitizeIsoTime extends Solar_Filter_Abstract {
+class Solar_Filter_SanitizeIsoTime extends Solar_Filter_SanitizeIsoTimestamp {
/**
*
@@ -39,6 +39,11 @@
*/
public function sanitizeIsoTime($value)
{
+ // look for His keys?
+ if (is_array($value)) {
+ $value = $this->_arrayToTime($value);
+ }
+
// if the value is not required, and is blank, sanitize to null
$null = ! $this->_filter->getRequire() &&
$this->_filter->validateBlank($value);
Modified: trunk/Solar/Filter/SanitizeIsoTimestamp.php
===================================================================
--- trunk/Solar/Filter/SanitizeIsoTimestamp.php 2008-04-25 13:10:55 UTC (rev 3127)
+++ trunk/Solar/Filter/SanitizeIsoTimestamp.php 2008-04-25 13:16:47 UTC (rev 3128)
@@ -31,15 +31,22 @@
* Forces the value to an ISO-8601 formatted timestamp using a space
* separator ("yyyy-mm-dd hh:ii:ss") instead of a "T" separator.
*
- * @param string $value The value to be sanitized. If an integer, it
+ * @param mixed $value The value to be sanitized. If an integer, it
* is used as a Unix timestamp; otherwise, converted to a Unix
- * timestamp using [[php::strtotime() | ]].
+ * timestamp using [[php::strtotime() | ]]. If an array, and it has *all*
+ * the keys for `Y, m, d, h, i, s`, then the array is converted into
+ * an ISO 8601 string before sanitizing.
*
* @return string The sanitized value.
*
*/
public function sanitizeIsoTimestamp($value)
{
+ // look for YmdHis keys?
+ if (is_array($value)) {
+ $value = $this->_arrayToTimestamp($value);
+ }
+
// if the value is not required, and is blank, sanitize to null
$null = ! $this->_filter->getRequire() &&
$this->_filter->validateBlank($value);
@@ -56,4 +63,78 @@
return date($format, strtotime($value));
}
}
+
+ /**
+ *
+ * Converts an array of timestamp parts to a string timestamp.
+ *
+ * @var array $array The array of timestamp parts.
+ *
+ * @return string
+ *
+ */
+ protected function _arrayToTimestamp($array)
+ {
+ $value = $this->_arrayToDate($array)
+ . ' '
+ . $this->_arrayToTime($array);
+
+ return trim($value);
+ }
+
+ /**
+ *
+ * Converts an array of date parts to a string date.
+ *
+ * @var array $array The array of date parts.
+ *
+ * @return string
+ *
+ */
+ protected function _arrayToDate($array)
+ {
+ $date = array_key_exists('Y', $array) &&
+ trim($array['Y']) != '' &&
+ array_key_exists('m', $array) &&
+ trim($array['m']) != '' &&
+ array_key_exists('d', $array) &&
+ trim($array['d']) != '';
+
+ if (! $date) {
+ return;
+ }
+
+ return $array['Y'] . '-'
+ . $array['m'] . '-'
+ . $array['d'];
+ }
+
+ /**
+ *
+ * Converts an array of time parts to a string time.
+ *
+ * @var array $array The array of time parts.
+ *
+ * @return string
+ *
+ */
+ protected function _arrayToTime($array)
+ {
+ $time = array_key_exists('H', $array) &&
+ trim($array['H']) != '' &&
+ array_key_exists('i', $array) &&
+ trim($array['i']) != '';
+
+ if (! $time) {
+ return;
+ }
+
+ $s = array_key_exists('s', $array) && trim($array['s']) != ''
+ ? $array['s']
+ : '00';
+
+ return $array['H'] . ':'
+ . $array['i'] . ':'
+ . $s;
+ }
}
\ No newline at end of file
More information about the Solar-svn
mailing list