Commits

ajaxray committed e2983b4

Proximity Phrase issue fixed

Comments (0)

Files changed (3)

 Solr Query Builder
 ======================
 
-This project will help to make Solr Queries with an object oriented, dynamic approach. Primarily wrote for using with 
-[solr-php-client](http://code.google.com/p/solr-php-client), but can be used with any solr client that accepts simple string queries.
+A simple PHP 5.3 library that will help to make Solr Queries with an object oriented, dynamic approach. Primarily wrote for using with
+[solr-php-client](http://code.google.com/p/solr-php-client), but can be used with any solr client that works with simple query-string.
+Can be integrated with any PHP 5.3 Framework easily.
 
 Features
 --------------------
     * Fuzzy Search
     * Required/Restricted
     * Boost factors
+    * Proximity Search
 * Range Queries (Date/Numeric)
 * (Any number of) Subqueries
 

classes/field.php

 
     public function addProximityPhrase($value, $distance, $operator = null)
     {
-        $proximity = array(
-            'modifier' => '~',
-            'factor'   => intval($distance),
-        );
+        $proximity = array('modifier' => '~');
+
+        if(! is_null($distance)) {
+            if(!is_numeric($distance) || $distance <= 0 || is_float($distance)) {
+                throw new \InvalidArgumentException("Supported proximity value is a number greater then 1, $distance given");
+            }
+
+            if(strpos(trim($value), ' ') < 1){
+                throw new \InvalidArgumentException("Proximity cab be applied on a phrase of multiple words, $value given");
+            }
+
+            $proximity['factor'] = $distance;
+        }
+
         $this->addTerm($value, $operator, $proximity);
 
         return $this;

tests/FieldTest.php

 class FieldTest extends PHPUnit_Framework_TestCase
 {
     /**
-     * @var Field
+     * @var Solr\QueryBuilder\Field
      */
     protected $param;
 
         $this->assertEquals('name:"Baby Toy"~3', $this->param->__toString());
     }
 
+    /**
+     * @expectedException InvalidArgumentException
+     * @dataProvider invalidProximityProvider
+     */
+    public function testProximityTermThrowsExceptionOnInvalidProximity($proximityValue)
+    {
+        $this->param->addProximityPhrase('WNeeds Ltd', $proximityValue);
+        $this->assertEquals("name:\"WNeeds Ltd\"~$proximityValue", $this->param->__toString());
+    }
+
+    /**
+     * @expectedException InvalidArgumentException
+     */
+    public function testProximityTermThrowsExceptionOnInvalidPhrase()
+    {
+        $this->param->addProximityPhrase('WNeeds', 2);
+        $this->assertEquals('"name:WNeeds"~2', $this->param->__toString());
+    }
+
     public function testRangeQueryWorksForDate()
     {
         $dateField = new Field('create_date');
             array('NOT-A-NUMBER')
         );
     }
+
+    public static function invalidProximityProvider()
+    {
+        return array(
+            array(-1),
+            array(0),
+            array(1.2),
+            array('NOT-A-NUMBER')
+        );
+    }
 }