/**
  * @param string $string
  *
  * @return AbstractSort[]
  */
 public function buildSortFromString($string)
 {
     $sorts = [];
     if (!empty($string)) {
         $sortBuilder = new SortBuilder();
         foreach ($this->parse($string) as $item) {
             /*
              * Position map:
              * - 0 = field name
              * - 1 = order (asc/desc)
              * - 2 = mode (min/max/sum/avg/median)
              */
             if (isset($item[0])) {
                 if ($item[0] === '_score') {
                     $sort = $sortBuilder->createScoreSort();
                 } elseif ($item[0] === '_doc') {
                     $sort = $sortBuilder->createDocSort();
                 } else {
                     $sort = $sortBuilder->createFieldSort()->setField($item[0]);
                 }
                 if (isset($item[1])) {
                     $sort->setOrder($item[1]);
                 }
                 if (isset($item[2])) {
                     $sort->setMode($item[2]);
                 }
                 $sorts[] = $sort;
             }
         }
     }
     return $sorts;
 }
예제 #2
0
 /**
  * Tests setters & getters.
  */
 public function testSetterGetter()
 {
     $this->specify('sorts can be set and get', function () {
         $this->sort->setSorts([$this->sortBuilder->createFieldSort()->setField('f'), $this->sortBuilder->createScoreSort()]);
         verify($this->sort->getSorts())->count(2);
     });
     $this->specify('sort can be added and get', function () {
         $this->sort->addSort($this->sortBuilder->createScoreSort());
         verify($this->sort->getSorts())->count(1);
     });
 }
 /**
  * Tests the Doc sort format.
  */
 public function testSortFormat()
 {
     $this->specify('doc sort was created', function () {
         $sort = $this->sortBuilder->createDocSort();
         verify($sort)->isInstanceOf('\\Nord\\Lumen\\Elasticsearch\\Search\\Sort\\DocSort');
     });
     $this->specify('doc sort format', function () {
         $sort = $this->sortBuilder->createDocSort();
         $array = $sort->toArray();
         verify($array)->equals('_doc');
     });
     $this->specify('doc sort format with order', function () {
         $sort = $this->sortBuilder->createDocSort();
         $sort->setOrder('asc');
         $array = $sort->toArray();
         verify($array)->equals(['_doc' => ['order' => 'asc']]);
     });
 }
 /**
  * Tests the Field sort format.
  */
 public function testSortFormat()
 {
     $this->specify('field sort was created', function () {
         $sort = $this->sortBuilder->createFieldSort();
         verify($sort)->isInstanceOf('\\Nord\\Lumen\\Elasticsearch\\Search\\Sort\\FieldSort');
     });
     $this->specify('field sort format', function () {
         $sort = $this->sortBuilder->createFieldSort();
         $sort->setField('field');
         $array = $sort->toArray();
         verify($array)->equals('field');
     });
     $this->specify('field sort format with order', function () {
         $sort = $this->sortBuilder->createFieldSort();
         $sort->setField('field')->setOrder('asc');
         $array = $sort->toArray();
         verify($array)->equals(['field' => ['order' => 'asc']]);
     });
     $this->specify('field sort format with mode', function () {
         $sort = $this->sortBuilder->createFieldSort();
         $sort->setField('field')->setMode('avg');
         $array = $sort->toArray();
         verify($array)->equals(['field' => ['mode' => 'avg']]);
     });
     $this->specify('field sort format with order and mode', function () {
         $sort = $this->sortBuilder->createFieldSort();
         $sort->setField('field')->setOrder('asc')->setMode('avg');
         $array = $sort->toArray();
         verify($array)->equals(['field' => ['order' => 'asc', 'mode' => 'avg']]);
     });
     $this->specify('field sort format with missing', function () {
         $sort = $this->sortBuilder->createFieldSort();
         $sort->setField('field')->setMissing('_last');
         $array = $sort->toArray();
         verify($array)->equals(['field' => ['missing' => '_last']]);
     });
     $this->specify('field sort format with unmapped_type', function () {
         $sort = $this->sortBuilder->createFieldSort();
         $sort->setField('field')->setUnmappedType('long');
         $array = $sort->toArray();
         verify($array)->equals(['field' => ['unmapped_type' => 'long']]);
     });
 }