| 
									
										
										
										
											2013-05-27 12:49:32 +04:00
										 |  |  |  | <?php | 
					
						
							| 
									
										
										
										
											2013-06-17 12:01:59 +04:00
										 |  |  |  | /** | 
					
						
							|  |  |  |  |  * @author Alexander Demidov <demidov@dimti.ru> | 
					
						
							|  |  |  |  |  * Full paths to directories and files contains slash as first symbol | 
					
						
							|  |  |  |  |  * Dir name not contains slashes on first or last symbols | 
					
						
							|  |  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2013-05-27 12:49:32 +04:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-17 12:01:59 +04:00
										 |  |  |  | /** | 
					
						
							|  |  |  |  |  * Class File | 
					
						
							|  |  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2013-05-27 12:49:32 +04:00
										 |  |  |  | abstract class File | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     public $path; | 
					
						
							| 
									
										
										
										
											2013-06-26 11:49:27 +04:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-27 12:49:32 +04:00
										 |  |  |  |     public $filename; | 
					
						
							| 
									
										
										
										
											2013-06-26 11:49:27 +04:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-14 10:55:07 +04:00
										 |  |  |  |     private $logger; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     private $error_stream; | 
					
						
							| 
									
										
										
										
											2013-06-26 11:49:27 +04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     public function __construct() | 
					
						
							|  |  |  |  |     { | 
					
						
							| 
									
										
										
										
											2013-08-14 10:55:07 +04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     public function log($message) | 
					
						
							|  |  |  |  |     { | 
					
						
							|  |  |  |  |         if (Config::get('LOGGING')) { | 
					
						
							|  |  |  |  |             if (is_null($this->logger)) { | 
					
						
							|  |  |  |  |                 $this->logger = Logger::getInstance(); | 
					
						
							|  |  |  |  |             } | 
					
						
							|  |  |  |  |             $this->logger->log($message); | 
					
						
							|  |  |  |  |         } else { | 
					
						
							|  |  |  |  |             $this->error_stream = Config::get('ErrorStream', 'php://stderr'); | 
					
						
							|  |  |  |  |             file_put_contents($this->error_stream, PHP_EOL . 'Log ' . '#' . __CLASS__ . ': ' . $message . PHP_EOL, FILE_APPEND); | 
					
						
							|  |  |  |  |         } | 
					
						
							| 
									
										
										
										
											2013-06-26 11:49:27 +04:00
										 |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-27 12:49:32 +04:00
										 |  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2013-06-07 12:18:49 +04:00
										 |  |  |  |      * @param array|string|null $data | 
					
						
							| 
									
										
										
										
											2013-05-27 12:49:32 +04:00
										 |  |  |  |      * @return Image | 
					
						
							|  |  |  |  |      * @throws ErrorException | 
					
						
							|  |  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2013-06-07 12:18:49 +04:00
										 |  |  |  |     public static function getInstance($data = null) | 
					
						
							| 
									
										
										
										
											2013-05-27 12:49:32 +04:00
										 |  |  |  |     { | 
					
						
							| 
									
										
										
										
											2013-06-07 12:18:49 +04:00
										 |  |  |  |         $instance = new static; | 
					
						
							| 
									
										
										
										
											2013-06-07 13:43:07 +04:00
										 |  |  |  |         if (!(is_null($data) || $data == '')) { | 
					
						
							| 
									
										
										
										
											2013-06-07 12:18:49 +04:00
										 |  |  |  |             if (!is_array($data)) { | 
					
						
							| 
									
										
										
										
											2013-06-07 13:43:07 +04:00
										 |  |  |  |                 $data = json_decode($data, true); | 
					
						
							| 
									
										
										
										
											2013-06-07 12:18:49 +04:00
										 |  |  |  |                 if (json_last_error() != JSON_ERROR_NONE) { | 
					
						
							| 
									
										
										
										
											2013-08-14 10:55:07 +04:00
										 |  |  |  |                     $instance->log('Unable to convert json-string to array. Data ' . print_r($data, true)); | 
					
						
							| 
									
										
										
										
											2013-06-07 12:18:49 +04:00
										 |  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2013-05-27 12:49:32 +04:00
										 |  |  |  |             } | 
					
						
							| 
									
										
										
										
											2013-06-07 13:43:07 +04:00
										 |  |  |  |             if (is_array($data)) { | 
					
						
							|  |  |  |  |                 foreach ($data as $attribute_name => $attribute_value) { | 
					
						
							|  |  |  |  |                     if (property_exists($instance, $attribute_name)) { | 
					
						
							|  |  |  |  |                         $instance->{$attribute_name} = $attribute_value; | 
					
						
							|  |  |  |  |                     } | 
					
						
							| 
									
										
										
										
											2013-06-07 12:18:49 +04:00
										 |  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2013-05-27 12:49:32 +04:00
										 |  |  |  |             } | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  |         return $instance; | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2013-05-29 15:01:54 +04:00
										 |  |  |  |      * @param $size string | 
					
						
							|  |  |  |  |      * @return ImageVariant | 
					
						
							|  |  |  |  |      */ | 
					
						
							|  |  |  |  |     public function getImageVariant($size) | 
					
						
							|  |  |  |  |     { | 
					
						
							|  |  |  |  |         if (!array_key_exists($size, $this->variants)) { | 
					
						
							|  |  |  |  |             $this->variants[$size] = new ImageVariant; | 
					
						
							|  |  |  |  |         } else { | 
					
						
							|  |  |  |  |             if (!is_object($this->variants[$size])) { | 
					
						
							| 
									
										
										
										
											2013-05-30 09:14:32 +04:00
										 |  |  |  |                 $this->variants[$size] = self::getInstance($this->variants[$size]); | 
					
						
							| 
									
										
										
										
											2013-05-29 15:01:54 +04:00
										 |  |  |  |             } | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  |         return $this->variants[$size]; | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2013-08-14 22:17:21 +04:00
										 |  |  |  |      * Если объект содержит информацию о файле/изображение вернет - true | 
					
						
							|  |  |  |  |      * Если объект пуст - вернет false | 
					
						
							|  |  |  |  |      * @return bool | 
					
						
							|  |  |  |  |      */ | 
					
						
							|  |  |  |  |     public function getIsNoEmpty() | 
					
						
							|  |  |  |  |     { | 
					
						
							|  |  |  |  |         return (bool) $this->size; | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2013-05-27 12:49:32 +04:00
										 |  |  |  |      * @return string | 
					
						
							| 
									
										
										
										
											2013-08-14 10:55:07 +04:00
										 |  |  |  |      * TODO: Сделать настройку для изображения-заглушки применительно к Image | 
					
						
							| 
									
										
										
										
											2013-05-27 12:49:32 +04:00
										 |  |  |  |      */ | 
					
						
							|  |  |  |  |     public function getWebName() | 
					
						
							|  |  |  |  |     { | 
					
						
							|  |  |  |  |         return $this->path . '/' . $this->filename; | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     public function __toString() | 
					
						
							|  |  |  |  |     { | 
					
						
							| 
									
										
										
										
											2013-05-29 15:01:54 +04:00
										 |  |  |  |         return FileHelper::toString($this); | 
					
						
							| 
									
										
										
										
											2013-05-27 12:49:32 +04:00
										 |  |  |  |     } | 
					
						
							|  |  |  |  | } |