value = (string) $value; if ($context) { $this->context = (string) $context; } } public function getValue() { return $this->value; } public function hasContext() { return $this->context !== null; } public function getContext() { return $this->context; } public function __toString() { return self::dump($this); } public static function parse($string) { // preg_match(self::TERM_REGEX, $string, $matches); // // return new self( // isset($matches[1]) ? $matches[1] : null, // isset($matches[2]) ? $matches[2] : null // ); $term = $string; $context = ''; if( ($p0 = strpos($string, '(')) !== false) { $term = substr($term, 0, $p0); $context = substr($string, $p0+1); if( ($p1 = strpos($context, ')')) !== false) { $context = substr($context, 0, $p1); } } if(($term = trim($term)) === '') { $term = null; } if(($context = trim($context)) === '') { $context = null; } if($term === null && $context !== null) { // special case "(foo)" $term = $context; $context = null; } return new self($term, $context); } public static function dump(TermInterface $term) { if ($term->hasContext()) { return sprintf('"%s" context:"%s"', $term->getValue(), $term->getContext()); } return sprintf('"%s"', $term->getValue()); } }