1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-12-28 00:10:57 +01:00
phorge-phorge/src/applications/herald/storage/transcript/HeraldObjectTranscript.php
Bob Trahan b93bc7e479 phutil_utf8_shorten => PhutilUTF8StringTruncator
Summary: Ref T3307. Only one I thought was tricky was Excel; I went with bytes there like it was email.

Test Plan: played around on a few endpoints but mostly thought carefully

Reviewers: epriestley

Reviewed By: epriestley

Subscribers: epriestley, Korvin

Maniphest Tasks: T3307

Differential Revision: https://secure.phabricator.com/D10392
2014-08-29 15:15:13 -07:00

75 lines
1.5 KiB
PHP

<?php
final class HeraldObjectTranscript {
protected $phid;
protected $type;
protected $name;
protected $fields;
public function setPHID($phid) {
$this->phid = $phid;
return $this;
}
public function getPHID() {
return $this->phid;
}
public function setType($type) {
$this->type = $type;
return $this;
}
public function getType() {
return $this->type;
}
public function setName($name) {
$this->name = $name;
return $this;
}
public function getName() {
return $this->name;
}
public function setFields(array $fields) {
foreach ($fields as $key => $value) {
$fields[$key] = self::truncateValue($value, 4096);
}
$this->fields = $fields;
return $this;
}
public function getFields() {
return $this->fields;
}
private static function truncateValue($value, $length) {
if (is_string($value)) {
if (strlen($value) <= $length) {
return $value;
} else {
// NOTE: PhutilUTF8StringTruncator has huge runtime for giant strings.
return phutil_utf8ize(substr($value, 0, $length)."\n<...>");
}
} else if (is_array($value)) {
foreach ($value as $key => $v) {
if ($length <= 0) {
$value['<...>'] = '<...>';
unset($value[$key]);
} else {
$v = self::truncateValue($v, $length);
$length -= strlen($v);
$value[$key] = $v;
}
}
return $value;
} else {
return $value;
}
}
}