Advertisement
rfv123

/31797167/class-map-POST-array-to-required-group-structure

Aug 4th, 2015
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.89 KB | None | 0 0
  1. <?php
  2.  
  3. // Provide a mapping between our 'logical group and the $_POST array.
  4.        // This will allow simplified 'group' processing
  5.        // As there are various functions why not use a class?
  6.  
  7. class GroupFieldMap
  8. {
  9.     protected $post = null;
  10.  
  11.     protected $fieldNames = array();
  12.     protected $fieldGroup = array();
  13.  
  14.     public function __construct($post, $fieldGroup) {
  15.         $this->post = $post;
  16.         $this->fieldGroup = $fieldGroup;
  17.         foreach($fieldGroup as $fieldList) {
  18.             foreach ($fieldList as $fieldName) {
  19.                 $this->fieldNames[] = $fieldName;
  20.             }
  21.         }
  22.     }
  23.  
  24.     public function groupCount() {
  25.         return count($this->post);
  26.     }
  27.  
  28.     public function groupExists($groupIdx) {
  29.         $found = false;
  30.         foreach ($this->fieldNames as $fieldName) {
  31.             if (isset($this->post[$fieldName][$groupIdx])) {
  32.                 return true;
  33.             }
  34.         }
  35.         return false;
  36.     }
  37.  
  38.     public function groupFieldExists($groupIdx, $fieldName) {
  39.  
  40.         return isset($this->post[$fieldName][$groupIdx]);
  41.     }
  42.  
  43.     public function groupFieldHasValue($groupIdx, $fieldName, $fieldIdx) {
  44.  
  45.         return !empty($this->post[$fieldName][$groupIdx][$fieldIdx]);
  46.     }
  47.  
  48.     public function groupFieldValue($groupIdx, $fieldName, $fieldIdx) {
  49.  
  50.         if (isset($this->post[$fieldName][$groupIdx][$fieldIdx])) {
  51.             return $this->post[$fieldName][$groupIdx][$fieldIdx];
  52.         }
  53.         return null;
  54.     }
  55.  
  56.     public function groupFieldCount($groupIdx, $fieldName) {
  57.  
  58.         if (isset($this->post[$fieldName][$groupIdx])) {
  59.             return count($this->post[$fieldName][$groupIdx]);
  60.         }
  61.         return 0;
  62.     }
  63.  
  64.     public function fields() {
  65.         return $this->fieldNames;
  66.     }
  67.  
  68.      public function fieldGroup() {
  69.         return $this->fieldGroup;
  70.     }
  71.  
  72.  
  73. } // end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement