aboutsummaryrefslogtreecommitdiffhomepage
path: root/inc/3rdparty/libraries/MOBIClass/FileString.php
blob: 16e906a6ced705fdff8b4ba53156c6c27c88a3c3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php

/**
 * Description of FileString
 *
 * @author Sander
 */
class FileString extends FileObject {
	private $forcedLength;
	private $data;

	/**
	 * Make a string to be stored in a file
	 * @param string|int $first Optional, if it is a string, it will be the contents,
	 * if it is a number, it will set the forced length.
	 * @param int $second Optional, will set the forced length. Can only be used when the
	 * first argument is contents.
	 */
	public function __construct($first = null, $second = null){
		$this->forcedLength = -1;
		$this->data = "";
		
		if($second != null){
			$this->data = $first;
			$this->forcedLength = $second;
		}else if($first != null){
			if(is_string($first)){
				$this->data = $first;
			}else{
				$this->forcedLength = $first;
			}
		}
	}

	public function getByteLength(){
		return $this->getLength();
	}

	public function getLength(){
		if($this->forcedLength >= 0){
			return $this->forcedLength;
		}
		return strlen($this->data);
	}

	public function get(){
		return $this->data;
	}

	public function set($value){
		$this->data = $value;
	}

	public function serialize() {
		$output = $this->data;
		$curLength = strlen($output);

		if($this->forcedLength >= 0){
			if($this->forcedLength > $curLength){
				return str_pad($output, $this->forcedLength, "\0", STR_PAD_RIGHT);
			}elseif($this->forcedLength == $curLength){
				return $output;
			}else{
				return substr($output, 0, $this->forcedLength);
			}
		}
		return $output;
	}

	public function unserialize($data) {
		__construct($data);
	}

	public function __toString(){
		$out = "FileString";
		if($this->forcedLength >= 0){
			$out .= " ".$this->forcedLength;
		}
		$out .= ": {\"".str_replace(array(" ", "\0"), "&nbsp;", $this->serialize())."\"}";
		return $out;
	}
}
?>