Advertisement
relax4o

ArrayExtractor

Oct 28th, 2015
412
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.23 KB | None | 0 0
  1. trait ArrayExtractor
  2. {
  3.     private $_data = [];
  4.     private $_tempData = [];
  5.     private $_path;
  6.     private $_clear_flag = true;
  7.    
  8.     public function aeLoad( $filename, $as = null ) {
  9.        
  10.         if ( $this->aeExists($filename) ) {
  11.             $file = $this->aeGetPath( $filename );
  12.             $fetch = require $file;
  13.            
  14.             $this->aeSetTempData($fetch, $as);
  15.            
  16.             if ( !sizeof($this->aeGetTempData($as)) ) {
  17.                 throw new \Exception("The file {$as} is empty!");
  18.             }
  19.            
  20.             return $this;
  21.         }
  22.         else {
  23.             throw new \Exception("This file {$filename} doesn't exists!");
  24.         }
  25.     }
  26.    
  27.     public function aeExtractAll($alias = null) {
  28.        
  29.         $this->aeSetData($this->aeGetTempData($alias), $alias);
  30.         $this->aeClearTempData($alias);
  31.     }
  32.    
  33.     public function aeExtract( $indicies, $alias = null ) {
  34.        
  35.         if ( is_array($indicies) ) {
  36.             foreach ( $indicies as $key=>$value ) {
  37.                 $this->aeSetData($this->aeExtract($value, $alias), $alias, $key);
  38.             }
  39.             $this->aeClearTempData($alias);
  40.             return;
  41.         }
  42.        
  43.         if ( strpos($indicies, '.') ) {
  44.             $data = $this->aeGetTempData($alias);
  45.             $parts = explode('.', $indicies);
  46.            
  47.             foreach ( $parts as $part ) {
  48.                 if ( isset($data[$part]) ) {
  49.                     $data = $data[$part];
  50.                 }
  51.                 else {
  52.                     break;
  53.                 }
  54.             }
  55.  
  56.             return $data;
  57.         }
  58.        
  59.         if ( isset($this->aeGetTempData($alias)[$indicies]) ) {
  60.             return $this->aeGetTempData($alias)[$indicies];
  61.         }
  62.     }
  63.    
  64.     public function aeGet( $index, $alias = null ) {
  65.        
  66.         if( isset($this->aeGetData($alias)[$index])) {
  67.             return $this->aeGetData($alias)[$index];
  68.         }
  69.        
  70.         if ( strpos($index, '.') ) {
  71.             $data = $this->aeGetData($alias);
  72.             $parts = explode('.', $index);
  73.            
  74.             foreach ( $parts as $part ) {
  75.                 if ( isset($data[$part]) ) {
  76.                     $data = $data[$part];
  77.                 }
  78.                 else {
  79.                     break;
  80.                 }
  81.             }
  82.  
  83.             return $data;
  84.         }
  85.     }
  86.    
  87.     private function aeGetClearFlag() {
  88.         return $this->_clear_flag;
  89.     }
  90.    
  91.     public function aeSetClearFlag( $flag = true ) {
  92.         $this->_clear_flag = (boolean)$flag;
  93.         return $this;
  94.     }
  95.    
  96.     private function aeClearTempData( $alias = null ) {
  97.         if ( $this->aeGetClearFlag() ) {
  98.             if ( is_null($alias) ) {
  99.                 unset($this->_tempData);
  100.             }
  101.             else {
  102.                 unset($this->_tempData[$alias]);
  103.             }
  104.         }
  105.     }
  106.    
  107.     public function aeGetData($alias = null) {
  108.         return is_null($alias) ? $this->_data : $this->_data[$alias];
  109.     }
  110.    
  111.     public function aeSetData($v, $alias = null, $k = null) {
  112.        
  113.         if ( is_null($alias) ) {
  114.             if ( is_null($k) )
  115.                 $this->_data[] = $v;
  116.             else
  117.                 $this->_data[$k] = $v;
  118.         }
  119.         else {
  120.             if ( is_null($k) )
  121.                 $this->_data[$alias] = $v;
  122.             else
  123.                 $this->_data[$alias][$k] = $v;
  124.         }
  125.     }
  126.    
  127.     public function aeGetTempData($alias = null) {
  128.         return is_null($alias) ? $this->_tempData : $this->_tempData[$alias];
  129.     }
  130.    
  131.     public function aeSetTempData($v, $alias) {
  132.        
  133.         if ( is_null($alias) ) {
  134.             $this->_tempData = $v;
  135.         }
  136.         else {
  137.             $this->_tempData[$alias] = $v;
  138.         }
  139.     }
  140.    
  141.     public function aeGetPath( $filename = null) {
  142.         return $this->_path . DIRECTORY_SEPARATOR . $filename;
  143.     }
  144.    
  145.     private function aeExists( $filename ) {
  146.         return file_exists( $this->aeGetPath( $filename ) ) ? true : false;
  147.     }
  148.    
  149.     public function aeSetPath( $path ) {
  150.         $this->_path = realpath($path);
  151.         return $this;
  152.     }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement