3 * A class for writing files
5 class FrameworkFileOutputPointer extends BaseFrameworkSystem {
7 * The current file we are working in
9 private $fileName = "";
14 private $filePointer = null;
19 private final function __construct () {
20 // Call parent constructor
21 parent::constructor(__CLASS__);
23 // Set part description
24 $this->setPartDescr("Dateiausgabe-Handler");
27 $this->createUniqueID();
30 $this->removeNumberFormaters();
34 * Destructor for cleaning purposes, etc
36 public final function __destruct() {
37 // Is there a resource pointer? Then we have to close the file here!
38 if (is_resource($this->getPointer())) {
39 // Try to close a file
43 // Call the parent destructor
48 * Create a file pointer based on the given file. The file will also
51 * @param $fileName The file name we shall pass
53 * @param $mode The output mode ('w', 'a' are valid)
54 * @throws FileIsEmptyException If the provided file name is empty.
55 * @throws FilePointerNotOpened If fopen() returns not a file
59 public final static function createFrameworkFileOutputPointer ($fileName, $mode) {
60 // Some pre-sanity checks...
61 if (is_null($fileName)) {
63 throw new FileIsEmptyException(null, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
66 // Try to open a handler
67 $filePointer = @fopen($fileName, $mode);
68 if (($filePointer === null) || ($filePointer === false)) {
69 // Something bad happend
70 throw new FilePointerNotOpenedException ($fileName, self::EXCEPTION_FILE_POINTER_INVALID);
73 // Create new instance
74 $pointerInstance = new FrameworkFileOutputPointer();
76 // Set file pointer and file name
77 $pointerInstance->setPointer($filePointer);
78 $pointerInstance->setFileName($fileName);
80 // Return the instance
81 return $pointerInstance;
85 * Write data to a file pointer
87 * @param $dataStream The data stream we shall write to the file
88 * @return mixed The result of fwrite()
89 * @throws NullPointerException If the file pointer instance
90 * is not set by setPointer()
91 * @throws InvalidFileResourceException If there is being set
92 * an invalid file resource
94 public function writeToFile ($dataStream) {
95 if (is_null($this->getPointer())) {
96 // Pointer not initialized
97 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
98 } elseif (!is_resource($this->getPointer())) {
99 // Pointer is not a valid resource!
100 throw new InvalidFileResourceException($this, self::EXCEPTION_INVALID_DIRECTORY_POINTER);
103 // Read data from the file pointer and return it
104 return fwrite($this->getPointer(), $dataStream);
108 * Close a file source and set it's instance to null and the file name
112 * @throws NullPointerException If the file pointer instance
113 * is not set by setPointer()
114 * @throws InvalidFileResourceException If there is being set
116 public function closeFile () {
117 if (is_null($this->getPointer())) {
118 // Pointer not initialized
119 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
120 } elseif (!is_resource($this->getPointer())) {
121 // Pointer is not a valid resource!
122 throw new InvalidFileResourceException($this, self::EXCEPTION_INVALID_DIRECTORY_POINTER);
125 // Close the file pointer and reset the instance variable
126 @fclose($this->getPointer());
127 $this->setPointer(null);
128 $this->setFileName("");
132 * Setter for the file pointer
134 * @param $filePointer File resource
137 public final function setPointer ($filePointer) {
138 // Sanity-check if the pointer is a valid file resource
139 if (is_resource($filePointer) || is_null($filePointer)) {
140 // Is a valid resource
141 $this->filePointer = $filePointer;
144 throw new InvalidFileResourceException($this, self::EXCEPTION_INVALID_DIRECTORY_POINTER);
149 * Getter for the file pointer
151 * @return $filePointer The file pointer which shall be a valid
154 public final function getPointer () {
155 return $this->filePointer;
159 * Setter for file name
161 * @param $fileName The new file name
164 public final function setFileName ($fileName) {
165 $fileName = (string) $fileName;
166 $this->fileName = $fileName;
170 * Getter for file name
172 * @return $fileName The current file name
174 public final function getFileName () {
175 return $this->fileName;