Remove product literal strings in "pht()", part 5
[phabricator.git] / src / applications / files / document / PhabricatorDocumentRef.php
blob98dfda88561170ff3298c923914322afcae2441b
1 <?php
3 final class PhabricatorDocumentRef
4 extends Phobject {
6 private $name;
7 private $mimeType;
8 private $file;
9 private $byteLength;
10 private $snippet;
11 private $symbolMetadata = array();
12 private $blameURI;
13 private $coverage = array();
14 private $data;
16 public function setFile(PhabricatorFile $file) {
17 $this->file = $file;
18 return $this;
21 public function getFile() {
22 return $this->file;
25 public function setMimeType($mime_type) {
26 $this->mimeType = $mime_type;
27 return $this;
30 public function getMimeType() {
31 if ($this->mimeType !== null) {
32 return $this->mimeType;
35 if ($this->file) {
36 return $this->file->getMimeType();
39 return null;
42 public function setName($name) {
43 $this->name = $name;
44 return $this;
47 public function getName() {
48 if ($this->name !== null) {
49 return $this->name;
52 if ($this->file) {
53 return $this->file->getName();
56 return null;
59 public function setByteLength($length) {
60 $this->byteLength = $length;
61 return $this;
64 public function getByteLength() {
65 if ($this->byteLength !== null) {
66 return $this->byteLength;
69 if ($this->data !== null) {
70 return strlen($this->data);
73 if ($this->file) {
74 return (int)$this->file->getByteSize();
77 return null;
80 public function setData($data) {
81 $this->data = $data;
82 return $this;
85 public function loadData($begin = null, $end = null) {
86 if ($this->data !== null) {
87 $data = $this->data;
89 if ($begin !== null && $end !== null) {
90 $data = substr($data, $begin, $end - $begin);
91 } else if ($begin !== null) {
92 $data = substr($data, $begin);
93 } else if ($end !== null) {
94 $data = substr($data, 0, $end);
97 return $data;
100 if ($this->file) {
101 $iterator = $this->file->getFileDataIterator($begin, $end);
103 $result = '';
104 foreach ($iterator as $chunk) {
105 $result .= $chunk;
107 return $result;
110 throw new PhutilMethodNotImplementedException();
113 public function hasAnyMimeType(array $candidate_types) {
114 $mime_full = $this->getMimeType();
116 if (!phutil_nonempty_string($mime_full)) {
117 return false;
120 $mime_parts = explode(';', $mime_full);
122 $mime_type = head($mime_parts);
123 $mime_type = $this->normalizeMimeType($mime_type);
125 foreach ($candidate_types as $candidate_type) {
126 if ($this->normalizeMimeType($candidate_type) === $mime_type) {
127 return true;
131 return false;
134 private function normalizeMimeType($mime_type) {
135 $mime_type = trim($mime_type);
136 $mime_type = phutil_utf8_strtolower($mime_type);
137 return $mime_type;
140 public function isProbablyText() {
141 $snippet = $this->getSnippet();
142 return (strpos($snippet, "\0") === false);
145 public function isProbablyJSON() {
146 if (!$this->isProbablyText()) {
147 return false;
150 $snippet = $this->getSnippet();
152 // If the file is longer than the snippet, we don't detect the content
153 // as JSON. We could use some kind of heuristic here if we wanted, but
154 // see PHI749 for a false positive.
155 if (strlen($snippet) < $this->getByteLength()) {
156 return false;
159 // If the snippet is the whole file, just check if the snippet is valid
160 // JSON. Note that `phutil_json_decode()` only accepts arrays and objects
161 // as JSON, so this won't misfire on files with content like "3".
162 try {
163 phutil_json_decode($snippet);
164 return true;
165 } catch (Exception $ex) {
166 return false;
170 public function getSnippet() {
171 if ($this->snippet === null) {
172 $this->snippet = $this->loadData(null, (1024 * 1024 * 1));
175 return $this->snippet;
178 public function setSymbolMetadata(array $metadata) {
179 $this->symbolMetadata = $metadata;
180 return $this;
183 public function getSymbolMetadata() {
184 return $this->symbolMetadata;
187 public function setBlameURI($blame_uri) {
188 $this->blameURI = $blame_uri;
189 return $this;
192 public function getBlameURI() {
193 return $this->blameURI;
196 public function addCoverage($coverage) {
197 $this->coverage[] = array(
198 'data' => $coverage,
200 return $this;
203 public function getCoverage() {
204 return $this->coverage;