[pLog-svn] r5889 - plog/trunk/class/file

oscar at devel.lifetype.net oscar at devel.lifetype.net
Sun Sep 2 05:15:31 EDT 2007


Author: oscar
Date: 2007-09-02 05:15:31 -0400 (Sun, 02 Sep 2007)
New Revision: 5889

Modified:
   plog/trunk/class/file/fileuploads.class.php
Log:
Modified the FileUploads class so that it implements the Iterator interface and we can loop through its items via the standard foreach() construction. Iterators are cool :)


Modified: plog/trunk/class/file/fileuploads.class.php
===================================================================
--- plog/trunk/class/file/fileuploads.class.php	2007-09-02 07:44:49 UTC (rev 5888)
+++ plog/trunk/class/file/fileuploads.class.php	2007-09-02 09:15:31 UTC (rev 5889)
@@ -12,11 +12,11 @@
      * Handles file uploads in pLog.
 	 * @see FileUpload
      */
-	class FileUploads  
+	class FileUploads implements Iterator
 	{
 
-    	var $_files;
-        var $_blogInfo;
+    	private $_files;
+		private $_valid;
 
     	/**
          * Creates a new object to handle file uploads. $files is either the
@@ -24,13 +24,17 @@
          * $HTTP_POST_FILES if earlier version.
          *
          * @param files The contents of the array generated by php after a file
-         * has been uploaded.
+         * has been uploaded. If empty, the value will be fetched automatically from the request.
          */
-    	function FileUploads( $files )
+    	function FileUploads( $files = null )
         {
-        	
-
-            $this->_files = $files;
+			if( $files == null ) {
+				lt_include( PLOG_CLASS_PATH."class/net/http/httpvars.class.php" );
+				$this->_files = HttpVars::getFiles();
+			}
+			else {
+            	$this->_files = $files;
+			}
         }
 		
 		/**
@@ -125,5 +129,54 @@
             
             return File::rename( $filename, $destination );
          }
+
+		//----------
+		// Implemented for the Iterator interface, so that we can access the files inside the
+		// list of uploads as a normal array that returns FileUpload objects
+		//----------
+
+		/**
+		 * Implemented from the Iterator::current interface. Returns a FileUpload reference
+		 * to the current object
+		 */
+		function current()
+		{
+			return( new FileUpload( current( $this->_files )));
+		}
+		
+		/**
+		 * Implemented from the Iterator::key interface. Returns the key of the current object
+		 */
+		function key()
+		{
+			return( key( $this->_files ));
+		}
+		
+		/**
+		 * Implemented from the Iterator::next() interface.
+		 * Moves the pointer to the next FileUpload object.
+		 */
+		function next()
+		{
+			$this->_valid = ( next( $this->_files ) !== FALSE );
+		}		
+		
+		/**
+		 * Implemented from Iterator::rewind()
+		 * Resets the pointer in the array to the first position.
+		 */
+		function rewind()
+		{
+			$this->_valid = ( reset( $this->_files ) !== FALSE );
+		}
+		
+		/**
+		 * Implemented from Iterator::valid()
+		 * Returns true if the current element is valid.
+		 */
+		function valid()
+		{
+			return( $this->_valid );
+		}
     }
-?>
+?>
\ No newline at end of file



More information about the pLog-svn mailing list