Slashblog
[ class tree: Slashblog ] [ index: Slashblog ] [ all elements ]

Source for file datacontainer.class.php

Documentation is available at datacontainer.class.php

  1. <?php
  2. /**
  3.  * Defines the class DataContainer which handles data in form of DataItems. Handles things such as adding new items, deleting items and returning the value of an item.
  4.  * 
  5.  * @author Daniel Nordstrom <dnordstrom@mankindorganization.com>
  6.  * @link www.mrnordstrom.com
  7.  */
  8.  
  9.  
  10. /**
  11.  * Container of any number of data items.
  12.  */
  13. class DataContainer {
  14.     
  15.     protected $items;
  16.     
  17.     /**
  18.      * Initializes private item variable to an array.
  19.      */
  20.     function __construct(
  21.     {
  22.         $this->items = array ();
  23.     }
  24.     
  25.     /**
  26.      * Adds item to container.
  27.      * 
  28.      * @param string $k Key to add.
  29.      * @param string $v Value to associate with key.
  30.      */
  31.     function AddItem($k$v
  32.     {
  33.         $item new DataItem $k$v );
  34.         array_push $this->items$item );
  35.     }
  36.     
  37.     /**
  38.      * Gets value associated with specified key.
  39.      * 
  40.      * @param string $k Key to request.
  41.      */
  42.     function GetValue($k
  43.     {
  44.         foreach $this->items as $item // Loop to find requested key.
  45.         {
  46.             // Compares key in loop to the requested one, returns value if equal.
  47.             if (strcmp strtoupper($k)strtoupper($item->GetKey ()) ) == 0// And convert them both to uppercase, just in case...
  48.                 return $item->GetValue ();
  49.         }
  50.         
  51.         // If key was not found, -1 is returned.
  52.         return -1;
  53.     }
  54.     
  55.     /**
  56.      * Edits the value associated with specified key.
  57.      * 
  58.      * @param string $k Key of item to edit.
  59.      * @param string $value New value of item.
  60.      * 
  61.      * @return bool True on success and false if key does not exist.
  62.      */
  63.     function EditItem($k$value
  64.     {
  65.         foreach $this->items as $item // Loop to find requested key.
  66.         {
  67.             // Compares key in loop to the requested one, returns value if equal.
  68.             if (strcmp strtoupper($k)strtoupper($item->GetKey ()) ) == 0// And convert them both to uppercase, just in case...
  69.             {
  70.                 $item->SetValue($value)// Set new value.
  71.                 return 1// And return true on success.
  72.             }
  73.         }
  74.         
  75.         // If key was not found, 0 is returned.
  76.         return 0;
  77.     }
  78.     
  79.     /**
  80.      * Removes item with the specified key.
  81.      * 
  82.      * @param string $k Key to remove.
  83.      */
  84.     function DeleteItem($k
  85.     {
  86.         for($x 0$x count($this->items)$x++// Find out which one we should delete, try to match against specified key.
  87.         {
  88.             if (strcmp $k$this->items[$x]->GetKey() ) == 0)
  89.             {
  90.                 unset($this->items[$x])// Here it is, let's delete it and...
  91.                 return 1// ...return 1 on success.
  92.             }
  93.         }
  94.         
  95.         // If key was not found, -1 is returned.
  96.         return -1;
  97.     }
  98. }
  99.  
  100. ?>

Documentation generated on Mon, 26 May 2008 19:59:42 +0100 by phpDocumentor 1.4.0