Added:
trunk/Tipos/Filter/SanitizeEmailList.php (contents, props changed)
trunk/Tipos/Filter/ValidateEmailList.php (contents, props changed)
Log:
Filters to sanitize/validate list of emails typed in a form field.
Added: trunk/Tipos/Filter/SanitizeEmailList.php
==============================================================================
--- (empty file)
+++ trunk/Tipos/Filter/SanitizeEmailList.php Sat Oct 11 07:24:42 2008
@@ -0,0 +1,59 @@
+<?php
+/**
+ *
+ * Transforms a string of separated emails into an array of emails. Also
+ * considers line breaks as separators (useful to sanitize emails typed
+ * one by line in textareas).
+ *
+ * @category Tipos
+ *
+ * @package Tipos_Filter
+ *
+ * @author Rodrigo Moraes <rodrigo...@gmail.com>
+ *
+ * @license http://opensource.org/licenses/bsd-license.php BSD
+ *
+ * @version $Id: ValidateXml.php 1322 2007-11-11 10:11:35Z rodrigo.moraes $
+ *
+ */
+class Tipos_Filter_SanitizeEmailList extends Solar_Filter_Abstract
+{
+ /**
+ *
+ * Transforms a string of separated emails into an array of emails.
Also
+ * considers line breaks as separators (useful to sanitize emails typed
+ * one by line in textareas).
+ *
+ * @param mixed $value The value to validate.
+ *
+ * @return bool True if valid, false if not.
+ *
+ */
+ public function sanitizeEmailList($value, $sep = ',')
+ {
+ if (! $this->_filter->getRequire() && $value === null) {
+ return true;
+ }
+
+ // get form values.
+ $value = trim($value);
+
+ // put everything in a single line separated by commas
+ $value = str_replace(array("\r\n", "\r"), $sep, $value);
+
+ // transform into an array
+ $emails = explode($sep, $value);
+
+ // trim all emails and remove empty ones
+ foreach($emails as $key => $value) {
+ $emails[$key] = trim($value);
+
+ if (empty($emails[$key])) {
+ unset($emails[$key]);
+ }
+ }
+
+ // done
+ return $emails;
+ }
+}
\ No newline at end of file
Added: trunk/Tipos/Filter/ValidateEmailList.php
==============================================================================
--- (empty file)
+++ trunk/Tipos/Filter/ValidateEmailList.php Sat Oct 11 07:24:42 2008
@@ -0,0 +1,52 @@
+<?php
+/**
+ *
+ * Validates a list of emails.
+ *
+ * @category Tipos
+ *
+ * @package Tipos_Filter
+ *
+ * @author Rodrigo Moraes <rodrigo...@gmail.com>
+ *
+ * @license http://opensource.org/licenses/bsd-license.php BSD
+ *
+ * @version $Id: ValidateXml.php 1322 2007-11-11 10:11:35Z rodrigo.moraes $
+ *
+ */
+class Tipos_Filter_ValidateEmailList extends Solar_Filter_Abstract
+{
+ /**
+ *
+ * Validates a list of emails.
+ *
+ * @param mixed $value The value to validate.
+ *
+ * @return bool True if valid, false if not.
+ *
+ */
+ public function validateEmailList($value)
+ {
+ if (! $this->_filter->getRequire() && $value === null) {
+ return true;
+ }
+
+ // drop duplicated values
+ $value = array_unique($value);
+
+ // start the list as not valid
+ $is_valid = false;
+
+ // validate each email
+ foreach($value as $key => $email) {
+ $is_valid = $this->_filter->validateEmail($email);
+
+ if (! $is_valid) {
+ // found a invalid one, so stop here
+ break;
+ }
+ }
+
+ return $is_valid;
+ }
+}
\ No newline at end of file