1: <?php
2:
3: /*
4:
5: *
6: * NOTICE OF LICENSE
7: *
8: * This source file is subject to the Open Software License (OSL 3.0)
9: * or OpenGPL v3 license (GNU Public License V3.0)
10: * that is bundled with this package in the file LICENSE.txt.
11: * It is also available through the world-wide-web at this URL:
12: * http://opensource.org/licenses/osl-3.0.php
13: * or
14: * http://www.gnu.org/licenses/gpl-3.0.txt
15: * If you did not receive a copy of the license and are unable to
16: * obtain it through the world-wide-web, please send an email
17: * to info@e-abi.ee so we can send you a copy immediately.
18: *
19: * DISCLAIMER
20: *
21: * Do not edit or add to this file if you wish to upgrade this module to newer
22: * versions in the future.
23: *
24: * @category Eabi
25: * @package Eabi_Dpd
26: * @copyright Copyright (c) 2014 Aktsiamaailm LLC (http://en.e-abi.ee/)
27: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
28: * @license http://www.gnu.org/licenses/gpl-3.0.txt GNU Public License V3.0
29: * @author Matis Halmann
30: *
31:
32: */
33:
34: /**
35: * <p>Replaces Varien_File_Object for PHP5.3+ compatibility</p>
36: *
37: * @author Matis
38: */
39: class Eabi_Livehandler_Model_File_Object extends SplFileObject {
40: protected $_filename;
41: protected $_path;
42: protected $_filter;
43: protected $_isCorrect=true; # - pass or not filter checking
44: protected $filtered;
45:
46: /**
47: * Constructor
48: *
49: * @param string $path - path to directory
50: * @return none
51: */
52: public function __construct($path)
53: {
54: parent::__construct($path);
55: $this->_path=$path;
56: $this->_filename=basename($path);
57: }
58: /**
59: * add file name to array
60: *
61: * @param array &$files - array of files
62: * @return none
63: */
64: public function getFilesName(&$files)
65: {
66: $this->getFileName($files);
67: }
68: /**
69: * add file name to array
70: *
71: * @param array &$files - array of files
72: * @return none
73: */
74: public function getFileName(&$files=null)
75: {
76: if($this->_isCorrect){
77: if($files===null)
78: return $this->_filename;
79: $files[] = $this->_filename;
80: }
81: }
82: /**
83: * add file path to array
84: *
85: * @param array &$paths - array of paths
86: * @return none
87: */
88: public function getFilesPaths(&$paths)
89: {
90: if($this->_isCorrect){
91: $paths[] = (string)$this->_path;
92: }
93: }
94: /**
95: * add file path to array
96: *
97: * @param array &$paths - array of paths
98: * @return none
99: */
100: public function getFilePath(&$path=null)
101: {
102: if($this->_isCorrect){
103: if($path===null)
104: return $this->_path;
105: $paths[] = $this->_path;
106: }
107: }
108: /**
109: * use filter
110: *
111: * @param bool $useFilter - use or not filter
112: * @return none
113: */
114: public function useFilter($useFilter)
115: {
116: if($useFilter){
117: $this->renderFilter();
118: } else {
119: $this->_isCorrect = true;
120: $this->filtered = false;
121: }
122:
123: }
124: /**
125: * add file object to array
126: *
127: * @param array &$objs - array of gile objects
128: * @return none
129: */
130: public function getFilesObj(&$objs)
131: {
132: if($this->_isCorrect){
133: $objs[] = $this;
134: }
135: }
136: /**
137: * nothing
138: *
139: * @param array &$dirs - array of dirs
140: * @return none
141: */
142: public function getDirsName(&$dirs)
143: {
144: return Varien_Directory_Collection::getLastDir($this->_path);
145: }
146: /**
147: * nothing
148: *
149: * @param array &$dirs - array of dirs
150: * @return none
151: */
152: public function getDirName()
153: {
154: return Varien_Directory_Collection::lastDir($this->_path);
155: }
156: /**
157: * set file filter
158: *
159: * @param array $filter - array of filter
160: * @return none
161: */
162: public function setFilesFilter($filter)
163: {
164: $this->addFilter($filter);
165: }
166: /**
167: * set file filter
168: *
169: * @param array $filter - array of filter
170: * @return none
171: */
172: public function addFilter($filter)
173: {
174: $this->_filter = $filter;
175: }
176: /**
177: * get extension of file
178: *
179: * @return string - extension of file
180: */
181: public function getExtension()
182: {
183: return self::getExt($this->_filename);
184: }
185: /**
186: * get extension of file
187: *
188: * @param string $fileName - name of file
189: * @return string - extension of file
190: */
191: static public function getExt($fileName)
192: {
193: $path_parts = pathinfo($fileName);
194: if(isset($path_parts["extension"])) {
195: return $path_parts["extension"];
196: } else {
197: return '';
198: }
199: }
200: /**
201: * get name of file
202: *
203: * @return string - name of file
204: */
205: public function getName()
206: {
207: return basename($this->_filename,'.'.$this->getExtension());
208: }
209: /**
210: * render filters
211: *
212: * @return none
213: */
214: public function renderFilter()
215: {
216: #print_r($this->_filter);
217: if(isset($this->_filter) && count($this->_filter)>0 && $this->filtered==false){
218: $this->filtered = true;
219: if(isset($this->_filter['extension'])){
220: $filter = $this->_filter['extension'];
221: if($filter!=null){
222: if(is_array($filter)){
223: if(!in_array($this->getExtension(),$filter)){
224: $this->_isCorrect = false;
225: }
226: } else {
227: if($this->getExtension()!=$filter){
228: $this->_isCorrect = false;
229: }
230: }
231: }
232: }
233: if(isset($this->_filter['name'])){
234: $filter = $this->_filter['name'];
235: if($filter!=null){
236: if(is_array($filter)){
237: if(!in_array($this->getName(),$filter)){
238: $this->_isCorrect = false;
239: }
240: } else {
241: if($this->getName()!=$filter){
242: $this->_isCorrect = false;
243: }
244: }
245: }
246: }
247:
248: if(isset($this->_filter['regName'])){
249: $filter = $this->_filter['regName'];
250:
251: if($filter!=null){
252: foreach ($filter as $value) {
253: if(!preg_match($value,$this->getName())){
254: $this->_isCorrect = false;
255: }
256: }
257:
258: }
259: }
260: }
261: }
262: /**
263: * add to array file name
264: *
265: * @param array &$arr -export array
266: * @return none
267: */
268: public function toArray(&$arr)
269: {
270: if($this->_isCorrect){
271: $arr['files_in_dirs'][] = $this->_filename;
272: }
273: }
274: /**
275: * add to xml file name
276: *
277: * @param array &$xml -export xml
278: * @param int $recursionLevel - level of recursion
279: * @param bool $addOpenTag - nothing
280: * @param string $rootName - nothing
281: * @return none
282: */
283: public function toXml(&$xml,$recursionLevel=0,$addOpenTag=true,$rootName='Struct')
284: {
285: if($this->_isCorrect){
286: $xml .=str_repeat("\t",$recursionLevel+2).'<fileName>'.$this->_filename.'</fileName>'."\n";
287: }
288: }
289:
290: }
291:
292: