Skip to content

Instantly share code, notes, and snippets.

@randm-ch
Last active August 29, 2015 14:16
Show Gist options
  • Save randm-ch/b535a380caa1a21d5d7e to your computer and use it in GitHub Desktop.
Save randm-ch/b535a380caa1a21d5d7e to your computer and use it in GitHub Desktop.
<?php
namespace MyCompany\MyApplication\Utility;
use TYPO3\Flow\Annotations as Flow;
use MyCompany\MyApplication\Domain\Model\DqlStatement;
use MyCompany\MyApplication\Exception;
class TagsinputTag {
const TYPE_FREE = 'free';
const TYPE_PROPERTY = 'property';
const TYPE_JOIN = 'join';
const TYPE_OPERATION = 'operation';
const TYPE_CONJUNCTION = 'conjunction';
const TYPE_FUNCTION = 'function';
const TYPE_ASC = 'asc';
const TYPE_DESC = 'desc';
/**
* @var string
*/
protected $value;
/**
* @var string
*/
protected $output;
/**
* @var string
*/
protected $type;
/**
* @var boolean
*/
protected $dataTag;
/**
* @var \MyCompany\MyApplication\Utility\TagsinputTagInjector
*/
protected $injector;
/**
* @param string $input
* @param boolean $dataTag
*/
public function __construct($input = NULL, $dataTag = FALSE) {
$this->injector = new \MyCompany\MyApplication\Utility\TagsinputTagInjector();
$this->dataTag = $dataTag;
if(is_string($input) && strlen($input) > 0) $this->buildTag($input);
}
/**
* Populates the properties of this object
*
* @param string $input
* @return void
* @throws \MyCompany\MyApplication\Exception\TagNotAllowedException
*/
public function buildTag($input) {
$this->value = $input;
$operations = array_merge(DqlStatement::getOperations(), DqlStatement::getConjunctions(), DqlStatement::getFunctions(), DqlStatement::getJoins());
if(in_array($input, $operations)) {
$this->output = $this->translate(array_search($input, $operations));
if(in_array($input, DqlStatement::getOperations())) $this->type = self::TYPE_OPERATION;
if(in_array($input, DqlStatement::getConjunctions())) $this->type = self::TYPE_CONJUNCTION;
if(in_array($input, DqlStatement::getFunctions())) $this->type = self::TYPE_FUNCTION;
if(in_array($input, DqlStatement::getJoins()))$this->type = self::TYPE_JOIN;
}
elseif(preg_match('/^\'[^\']+\'$/', $input)) {
$this->output = str_replace('\'', '', $input);
$this->type = self::TYPE_FREE;
}
else {
$output[] = $this->translate($this->getSourceValue());
if($this->hasSourceAndPropertyValue()) {
$name = NULL;
if($this->dataTag === TRUE) {
$dataField = $this->injector->getDataFieldRepository()->findOneByIdentifier($this->getPropertyValue());
if($dataField instanceof \MyCompany\MyApplication\Domain\Model\DataField) {
$name = $dataField->getName();
}
if($name === NULL) $name = $this->translate($this->getPropertyValue());
$output[] = $name;
}
else {
$output[] = $this->translate($this->getPropertyValue());
}
if(end($output) === FALSE) throw new Exception\TagNotAllowedException('Tag for "'.$this->value.'" is not allowed to be created.');
}
$this->output = implode(': ', $output);
$this->type = self::TYPE_PROPERTY;
}
$this->value = addslashes($input);
}
/**
* Retuns an array of two TagsinputTag objects, the first one being the ASC ordering tag, the second DESC
*
* @return array<\MyCompany\MyApplication\Utility\TagsinputTag>
*/
public function getOrderingTags() {
$tags = array();
$ascTag = clone $this;
$ascTag->setValue($this->value.' ASC');
$ascTag->setOutput($this->output.', '.$this->translate('ASC'));
$ascTag->setType(self::TYPE_ASC);
$tags[] = $ascTag;
$descTag = clone $this;
$descTag->setValue($this->value.' DESC');
$descTag->setOutput($this->output.', '.$this->translate('DESC'));
$descTag->setType(self::TYPE_DESC);
$tags[] = $descTag;
return $tags;
}
/**
* @return string
*/
public function getValue() {
return $this->value;
}
/**
* @param string $value
* @return void
*/
public function setValue($value) {
$this->value = $value;
}
/**
* @return string
*/
public function getPropertyValue() {
$property = explode('.', $this->value);
return array_pop($property);
}
/**
* @return string
*/
public function getSourceValue() {
$property = explode('.', $this->value);
return array_shift($property);
}
/**
* @return boolean
*/
public function hasSourceAndPropertyValue() {
return (count(explode('.', $this->value)) > 1);
}
/**
* @return string
*/
public function getOutput() {
return $this->output;
}
/**
* @param string $output
* @return void
*/
public function setOutput($output) {
$this->output = $output;
}
/**
* @return string
*/
public function getType() {
return $this->type;
}
/**
* @param string $type
* @return void
*/
public function setType($type) {
$this->type = $type;
}
/**
* @return boolean
*/
public function isDataTag() {
return $this->dataTag;
}
/**
* @param boolean $dataTag
* @return void
*/
public function setDataTag($dataTag) {
$this->dataTag = $dataTag;
}
/**
* @param string $input
* @return string
*/
public function translate($input) {
return $this->injector->getTranslator()->translateById($input, array(), NULL, NULL, 'Main', 'MyCompany.MyApplication');
}
/**
* Returns multiple tags from a statement recieved by a \MyCompany\MyApplication\Domain\Model\DqlStatement object
*
* @param string $statement
* @param boolean $ordering
* @return array<\MyCompany\MyApplication\Utility\TagsinputTag>
*/
static function fetchMultipleTags($statement, $ordering = FALSE) {
$tags = array();
if(is_string($statement) && strlen($statement) > 0) {
foreach(explode(',', $statement) as $statement) {
if($ordering) {
list($property, $order) = explode(' ', $statement);
$tag = new self(trim($property));
$orderingTags = $tag->getOrderingTags();
$key = ($order === 'DESC') ? 1 : 0;
$tags[] = $orderingTags[$key];
}
else {
$tags[] = new self(trim($statement));
}
}
}
return $tags;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment