Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- trait ArrayExtractor
- {
- private $_data = [];
- private $_tempData = [];
- private $_path;
- private $_clear_flag = true;
- public function aeLoad( $filename, $as = null ) {
- if ( $this->aeExists($filename) ) {
- $file = $this->aeGetPath( $filename );
- $fetch = require $file;
- $this->aeSetTempData($fetch, $as);
- if ( !sizeof($this->aeGetTempData($as)) ) {
- throw new \Exception("The file {$as} is empty!");
- }
- return $this;
- }
- else {
- throw new \Exception("This file {$filename} doesn't exists!");
- }
- }
- public function aeExtractAll($alias = null) {
- $this->aeSetData($this->aeGetTempData($alias), $alias);
- $this->aeClearTempData($alias);
- }
- public function aeExtract( $indicies, $alias = null ) {
- if ( is_array($indicies) ) {
- foreach ( $indicies as $key=>$value ) {
- $this->aeSetData($this->aeExtract($value, $alias), $alias, $key);
- }
- $this->aeClearTempData($alias);
- return;
- }
- if ( strpos($indicies, '.') ) {
- $data = $this->aeGetTempData($alias);
- $parts = explode('.', $indicies);
- foreach ( $parts as $part ) {
- if ( isset($data[$part]) ) {
- $data = $data[$part];
- }
- else {
- break;
- }
- }
- return $data;
- }
- if ( isset($this->aeGetTempData($alias)[$indicies]) ) {
- return $this->aeGetTempData($alias)[$indicies];
- }
- }
- public function aeGet( $index, $alias = null ) {
- if( isset($this->aeGetData($alias)[$index])) {
- return $this->aeGetData($alias)[$index];
- }
- if ( strpos($index, '.') ) {
- $data = $this->aeGetData($alias);
- $parts = explode('.', $index);
- foreach ( $parts as $part ) {
- if ( isset($data[$part]) ) {
- $data = $data[$part];
- }
- else {
- break;
- }
- }
- return $data;
- }
- }
- private function aeGetClearFlag() {
- return $this->_clear_flag;
- }
- public function aeSetClearFlag( $flag = true ) {
- $this->_clear_flag = (boolean)$flag;
- return $this;
- }
- private function aeClearTempData( $alias = null ) {
- if ( $this->aeGetClearFlag() ) {
- if ( is_null($alias) ) {
- unset($this->_tempData);
- }
- else {
- unset($this->_tempData[$alias]);
- }
- }
- }
- public function aeGetData($alias = null) {
- return is_null($alias) ? $this->_data : $this->_data[$alias];
- }
- public function aeSetData($v, $alias = null, $k = null) {
- if ( is_null($alias) ) {
- if ( is_null($k) )
- $this->_data[] = $v;
- else
- $this->_data[$k] = $v;
- }
- else {
- if ( is_null($k) )
- $this->_data[$alias] = $v;
- else
- $this->_data[$alias][$k] = $v;
- }
- }
- public function aeGetTempData($alias = null) {
- return is_null($alias) ? $this->_tempData : $this->_tempData[$alias];
- }
- public function aeSetTempData($v, $alias) {
- if ( is_null($alias) ) {
- $this->_tempData = $v;
- }
- else {
- $this->_tempData[$alias] = $v;
- }
- }
- public function aeGetPath( $filename = null) {
- return $this->_path . DIRECTORY_SEPARATOR . $filename;
- }
- private function aeExists( $filename ) {
- return file_exists( $this->aeGetPath( $filename ) ) ? true : false;
- }
- public function aeSetPath( $path ) {
- $this->_path = realpath($path);
- return $this;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement