123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518 |
- <?php
- namespace Sabre\VObject;
- abstract class Property extends Node {
-
- public $name;
-
- public $group;
-
- public $parameters = array();
-
- protected $value;
-
- public $delimiter = ';';
-
- public function __construct(Component $root, $name, $value = null, array $parameters = array(), $group = null) {
- $this->name = $name;
- $this->group = $group;
- $this->root = $root;
- foreach($parameters as $k=>$v) {
- $this->add($k, $v);
- }
- if (!is_null($value)) {
- $this->setValue($value);
- }
- }
-
- public function setValue($value) {
- $this->value = $value;
- }
-
- public function getValue() {
- if (is_array($this->value)) {
- if (count($this->value)==0) {
- return null;
- } elseif (count($this->value)===1) {
- return $this->value[0];
- } else {
- return $this->getRawMimeDirValue($this->value);
- }
- } else {
- return $this->value;
- }
- }
-
- public function setParts(array $parts) {
- $this->value = $parts;
- }
-
- public function getParts() {
- if (is_null($this->value)) {
- return array();
- } elseif (is_array($this->value)) {
- return $this->value;
- } else {
- return array($this->value);
- }
- }
-
- public function add($name, $value = null) {
- $noName = false;
- if ($name === null) {
- $name = Parameter::guessParameterNameByValue($value);
- $noName = true;
- }
- if (isset($this->parameters[strtoupper($name)])) {
- $this->parameters[strtoupper($name)]->addValue($value);
- }
- else {
- $param = new Parameter($this->root, $name, $value);
- $param->noName = $noName;
- $this->parameters[$param->name] = $param;
- }
- }
-
- public function parameters() {
- return $this->parameters;
- }
-
- abstract public function getValueType();
-
- abstract public function setRawMimeDirValue($val);
-
- abstract public function getRawMimeDirValue();
-
- public function serialize() {
- $str = $this->name;
- if ($this->group) $str = $this->group . '.' . $this->name;
- foreach($this->parameters as $param) {
- $str.=';' . $param->serialize();
- }
- $str.=':' . $this->getRawMimeDirValue();
- $out = '';
- while(strlen($str)>0) {
- if (strlen($str)>75) {
- $out.= mb_strcut($str,0,75,'utf-8') . "\r\n";
- $str = ' ' . mb_strcut($str,75,strlen($str),'utf-8');
- } else {
- $out.=$str . "\r\n";
- $str='';
- break;
- }
- }
- return $out;
- }
-
- public function getJsonValue() {
- return $this->getParts();
- }
-
- public function setJsonValue(array $value) {
- if (count($value)===1) {
- $this->setValue(reset($value));
- } else {
- $this->setValue($value);
- }
- }
-
- public function jsonSerialize() {
- $parameters = array();
- foreach($this->parameters as $parameter) {
- if ($parameter->name === 'VALUE') {
- continue;
- }
- $parameters[strtolower($parameter->name)] = $parameter->jsonSerialize();
- }
-
-
- if ($this->group) {
- $parameters['group'] = $this->group;
- }
- return array_merge(
- array(
- strtolower($this->name),
- (object)$parameters,
- strtolower($this->getValueType()),
- ),
- $this->getJsonValue()
- );
- }
-
- public function __toString() {
- return (string)$this->getValue();
- }
-
-
- public function offsetExists($name) {
- if (is_int($name)) return parent::offsetExists($name);
- $name = strtoupper($name);
- foreach($this->parameters as $parameter) {
- if ($parameter->name == $name) return true;
- }
- return false;
- }
-
- public function offsetGet($name) {
- if (is_int($name)) return parent::offsetGet($name);
- $name = strtoupper($name);
- if (!isset($this->parameters[$name])) {
- return null;
- }
- return $this->parameters[$name];
- }
-
- public function offsetSet($name, $value) {
- if (is_int($name)) {
- parent::offsetSet($name, $value);
-
-
-
- return;
-
- }
- $param = new Parameter($this->root, $name, $value);
- $this->parameters[$param->name] = $param;
- }
-
- public function offsetUnset($name) {
- if (is_int($name)) {
- parent::offsetUnset($name);
-
-
-
- return;
-
- }
- unset($this->parameters[strtoupper($name)]);
- }
-
-
- public function __clone() {
- foreach($this->parameters as $key=>$child) {
- $this->parameters[$key] = clone $child;
- $this->parameters[$key]->parent = $this;
- }
- }
-
- public function validate($options = 0) {
- $warnings = array();
-
- if (!StringUtil::isUTF8($this->getRawMimeDirValue())) {
- $oldValue = $this->getRawMimeDirValue();
- $level = 3;
- if ($options & self::REPAIR) {
- $newValue = StringUtil::convertToUTF8($oldValue);
- if (true || StringUtil::isUTF8($newValue)) {
- $this->setRawMimeDirValue($newValue);
- $level = 1;
- }
- }
- if (preg_match('%([\x00-\x08\x0B-\x0C\x0E-\x1F\x7F])%', $oldValue, $matches)) {
- $message = 'Property contained a control character (0x' . bin2hex($matches[1]) . ')';
- } else {
- $message = 'Property is not valid UTF-8! ' . $oldValue;
- }
- $warnings[] = array(
- 'level' => $level,
- 'message' => $message,
- 'node' => $this,
- );
- }
-
- if (!preg_match('/^([A-Z0-9-]+)$/', $this->name)) {
- $warnings[] = array(
- 'level' => 1,
- 'message' => 'The propertyname: ' . $this->name . ' contains invalid characters. Only A-Z, 0-9 and - are allowed',
- 'node' => $this,
- );
- if ($options & self::REPAIR) {
-
- $this->name = strtoupper(
- str_replace('_', '-', $this->name)
- );
-
- $this->name = preg_replace('/([^A-Z0-9-])/u', '', $this->name);
- }
- }
-
- foreach($this->parameters as $param) {
- $warnings = array_merge($warnings, $param->validate($options));
- }
- return $warnings;
- }
- }
|