Some updates:
[core.git] / framework / main / classes / compressor / class_GzipCompressor.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Compressor\Gzip;
4
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Compressor\Compressor;
7 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
8
9 /**
10  * GZIP compression and decompression class
11  *
12  * @author              Roland Haeder <webmaster@shipsimu.org>
13  * @version             0.0.0
14 <<<<<<< HEAD:framework/main/classes/compressor/class_GzipCompressor.php
15  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
16 =======
17  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2016 Core Developer Team
18 >>>>>>> Some updates::inc/main/classes/compressor/class_GzipCompressor.php
19  * @license             GNU GPL 3.0 or any newer version
20  * @link                http://www.shipsimu.org
21  *
22  * This program is free software: you can redistribute it and/or modify
23  * it under the terms of the GNU General Public License as published by
24  * the Free Software Foundation, either version 3 of the License, or
25  * (at your option) any later version.
26  *
27  * This program is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30  * GNU General Public License for more details.
31  *
32  * You should have received a copy of the GNU General Public License
33  * along with this program. If not, see <http://www.gnu.org/licenses/>.
34  */
35 class GzipCompressor extends BaseFrameworkSystem implements Compressor {
36         /**
37          * Protected constructor
38          *
39          * @return      void
40          */
41         protected function __construct () {
42                 // Call parent constructor!
43                 parent::__construct(__CLASS__);
44         }
45
46         /**
47          * Create a new compressor channel based a given compression handler or NULL if the required PHP functions are not found.
48          *
49          * @return      $compressorInstance             An instance of this class or NULL if the required PHP functions are not found.
50          */
51         public static final function createGzipCompressor () {
52                 // Routines not found by default
53                 $compressorInstance = NULL;
54
55                 // Get new instance
56                 if ((function_exists('gzencode')) && (function_exists('gzdecode'))) {
57                         // Compressor can maybe be used
58                         $compressorInstance = new GzipCompressor();
59                 } // END - if
60
61                 // Return the compressor instance
62                 return $compressorInstance;
63         }
64
65         /**
66          * GZIP compression stream
67          *
68          * @param       $streamData             Mixed non-object stream data
69          * @return      $streamData             The compressed stream data
70          * @throws      InvalidObjectException  If the stream is an object
71          */
72         public function compressStream ($streamData) {
73                 if (is_object($streamData)) {
74                         // Throw an exception
75                         throw new InvalidObjectException($streamData, self::EXCEPTION_UNEXPECTED_OBJECT);
76                 } // END - if
77
78                 // Return the compressed stream
79                 return gzencode($streamData, 1);
80         }
81
82         /**
83          * GZIP decompression stream
84          *
85          * @param       $streamData             Mixed non-object stream data
86          * @return      $streamData             The decompressed stream data
87          * @throws      InvalidObjectException  If the stream is an object
88          */
89         public function decompressStream ($streamData) {
90                 if (is_object($streamData)) {
91                         // Throw an exception
92                         throw new InvalidObjectException($streamData, self::EXCEPTION_UNEXPECTED_OBJECT);
93                 } // END - if
94
95                 // Return the decompressed stream
96                 return gzdecode($streamData);
97         }
98
99         /**
100          * Getter for the file extension of this compressor
101          *
102          * @return      $string         Returns always 'gz'
103          */
104         public final function getCompressorExtension () {
105                 return 'gz';
106         }
107
108 }