Пример #1
0
 public function test_unpack()
 {
     $result = StringHelper::bracesToArray('test');
     self::assertEquals(['test'], $result);
     $result = StringHelper::bracesToArray('(test)');
     self::assertEquals(['test'], $result);
     $result = StringHelper::bracesToArray('(((test)))');
     self::assertEquals(['test'], $result);
     $result = StringHelper::bracesToArray('(test)(test2)');
     self::assertEquals([['test'], ['test2']], $result);
     $result = StringHelper::bracesToArray('(test) and (test2)');
     self::assertEquals([['test'], 'and', ['test2']], $result);
     $result = StringHelper::bracesToArray('test (test2)');
     self::assertEquals(['test', ['test2']], $result);
     $result = StringHelper::bracesToArray('(test (test2))');
     self::assertEquals([['test', ['test2']]], $result);
     $result = StringHelper::bracesToArray('(test) test3');
     self::assertEquals([['test'], 'test3'], $result);
     $result = StringHelper::bracesToArray('((test) test3)');
     self::assertEquals([[['test'], 'test3']], $result);
     $result = StringHelper::bracesToArray('(test) and (test3 (test5))');
     self::assertEquals([['test'], 'and', ['test3', ['test5']]], $result);
     $result = StringHelper::bracesToArray('(test) and ((test2) or (test3))');
     self::assertEquals([['test'], 'and', [['test2'], 'or', ['test3']]], $result);
     $result = StringHelper::bracesToArray('(test) and (test2) or (test3)');
     self::assertEquals([['test'], 'and', ['test2'], 'or', ['test3']], $result);
     $result = StringHelper::bracesToArray('(test) or (test2) and (test3)');
     self::assertEquals([['test'], 'or', ['test2'], 'and', ['test3']], $result);
 }
Пример #2
0
 /**
  * @param array|string $tokens
  * @return array
  * @throws InvalidCriteriaException
  */
 public function createGraph($tokens)
 {
     if (is_string($tokens)) {
         $tokens = $this->tokenize(StringHelper::bracesToArray($tokens));
     }
     $logical = self::T_AND;
     $result = [];
     foreach ($tokens as $part) {
         $type = key($part);
         if ($type === self::T_AND || $type === self::T_OR) {
             if ($logical !== $type && count($result) > 1) {
                 throw new InvalidCriteriaException('can not use AND and OR in the same part of condition without braces');
             }
             $logical = $type;
         }
         if ($type === self::T_EXPR) {
             $result[] = $part;
         }
         if ($type === self::T_COMPOUND) {
             $result[] = $this->parseCompoundToken($part);
         }
     }
     return [$logical => $result];
 }