Skip to content

Commit

Permalink
Add more functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
foadyousefi committed Jan 31, 2019
1 parent e509041 commit a26147d
Show file tree
Hide file tree
Showing 13 changed files with 617 additions and 27 deletions.
121 changes: 121 additions & 0 deletions lib/Document/Document.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php

namespace FKRediSearch\RediSearch;

class Document {
protected $id;
protected $score = 1.0;
protected $noSave = false;
protected $replace = false;
protected $fields = array();
protected $payload;
protected $language;

public function __construct( $id = null ) {
$this->id = $id ?? uniqid(true);
}

public function getDefinition() {
$properties = [
$this->getId(),
$this->getScore(),
];

if ( $this->isNoSave() ) {
$properties[] = 'NOSAVE';
}

if ( $this->isReplace() ) {
$properties[] = 'REPLACE';
}

if ( !is_null( $this->getLanguage() ) ) {
$properties[] = 'LANGUAGE';
$properties[] = $this->getLanguage();
}

if ( !is_null( $this->getPayload() ) ) {
$properties[] = 'PAYLOAD';
$properties[] = $this->getPayload();
}

$properties[] = 'FIELDS';

foreach ( $this->getFields() as $name => $value) {
if ( isset( $value ) ) {
$properties[] = $name;
$properties[] = $value;
}
}

return $properties;
}

public function setFields( $fields = array() ) {
$this->fields = $fields;
return $this;
}

public function getFields() {
return $this->fields;
}

public function getId() {
return $this->id;
}

public function setId( $id ) {
$this->id = $id;
return $this;
}

public function getScore() {
return $this->score;
}

public function setScore( $score ) {
if ($score < 0.0 || $score > 1.0) {
$score = 1.0;
}

$this->score = $score;
return $this;
}

public function isNoSave() {
return $this->noSave;
}

public function setNoSave( $noSave ) {
$this->noSave = $noSave;
return $this;
}

public function isReplace() {
return $this->replace;
}

public function setReplace( $replace ) {
$this->replace = $replace;
return $this;
}

public function getPayload() {
return $this->payload;
}

public function setPayload( $payload ) {
$this->payload = $payload;
return $this;
}

public function getLanguage() {
return $this->language;
}

public function setLanguage( $language ) {
$this->language = $language;
return $this;
}

}
33 changes: 33 additions & 0 deletions lib/Fields/AbstractField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace FKRediSearch\RediSearch\Fields;

abstract class AbstractField implements FieldInterface {
protected $name;
protected $value;

public function __construct( $name, $value = null ) {
$this->name = $name;
$this->value = $value;
}

public function getName() {
return $this->name;
}

public function getValue() {
return $this->value;
}

public function setValue( $value ) {
$this->value = $value;
return $this;
}

public function getDefinition() {
return [
$this->getName(),
$this->getType(),
];
}
}
11 changes: 11 additions & 0 deletions lib/Fields/FieldInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace FKRediSearch\RediSearch\Fields;

interface FieldInterface {
public function getDefinition();
public function getType();
public function getName();
public function getValue();
public function setValue($value);
}
21 changes: 21 additions & 0 deletions lib/Fields/GeoField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace FKRediSearch\RediSearch\Fields;

class GeoField extends AbstractField {
use Noindex;

public function getType() {
return 'GEO';
}

public function getDefinition() {
$properties = parent::getDefinition();

if ( $this->isNoindex() ) {
$properties[] = 'NOINDEX';
}

return $properties;
}
}
18 changes: 18 additions & 0 deletions lib/Fields/GeoLocation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace FKRediSearch\RediSearch\Fields;

class GeoLocation {
protected $name;
protected $longitude;
protected $latitude;

public function __construct( $longitude, $latitude ) {
$this->longitude = $longitude;
$this->latitude = $latitude;
}

public function __toString() {
return "{$this->longitude} {$this->latitude}";
}
}
16 changes: 16 additions & 0 deletions lib/Fields/Noindex.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace FKRediSearch\RediSearch\Fields;

trait Noindex {
protected $isNoindex = false;

public function isNoindex() {
return $this->isNoindex;
}

public function setNoindex( $noindex ) {
$this->isNoindex = $noindex;
return $this;
}
}
26 changes: 26 additions & 0 deletions lib/Fields/NumericField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace FKRediSearch\RediSearch\Fields;

class NumericField extends AbstractField {
use Sortable;
use Noindex;

public function getType() {
return 'NUMERIC';
}

public function getDefinition() {
$properties = parent::getDefinition();

if ( $this->isSortable() ) {
$properties[] = 'SORTABLE';
}

if ( $this->isNoindex() ) {
$properties[] = 'NOINDEX';
}

return $properties;
}
}
16 changes: 16 additions & 0 deletions lib/Fields/Sortable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace FKRediSearch\RediSearch\Fields;

trait Sortable {
protected $isSortable = false;

public function isSortable() {
return $this->isSortable;
}

public function setSortable( $sortable ) {
$this->isSortable = $sortable;
return $this;
}
}
40 changes: 40 additions & 0 deletions lib/Fields/TagField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace FKRediSearch\RediSearch\Fields;

class TagField extends AbstractField {
use Sortable;
use Noindex;

protected $separator = ',';

public function getType() {
return 'TAG';
}

public function getSeparator() {
return $this->separator;
}

public function setSeparator( $separator ) {
$this->separator = $separator;
return $this;
}

public function getDefinition() {
$properties = parent::getDefinition();

$properties[] = 'SEPARATOR';
$properties[] = $this->getSeparator();

if ( $this->isSortable() ) {
$properties[] = 'SORTABLE';
}

if ( $this->isNoindex() ) {
$properties[] = 'NOINDEX';
}

return $properties;
}
}
53 changes: 53 additions & 0 deletions lib/Fields/TextField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace FKRediSearch\RediSearch\Fields;

class TextField extends AbstractField {
use Sortable;
use Noindex;

protected $weight = 1.0;
protected $noStem = false;

public function getType() {
return 'TEXT';
}

public function getWeight() {
return $this->weight;
}

public function setWeight( $weight ) {
$this->weight = $weight;
return $this;
}

public function isNoStem() {
return $this->noStem;
}

public function setNoStem( $noStem ) {
$this->noStem = $noStem;
return $this;
}

public function getDefinition() {
$properties = parent::getDefinition();
if ($this->isNoStem()) {
$properties[] = 'NOSTEM';
}

$properties[] = 'WEIGHT';
$properties[] = $this->getWeight();

if ( $this->isSortable() ) {
$properties[] = 'SORTABLE';
}

if ( $this->isNoindex() ) {
$properties[] = 'NOINDEX';
}

return $properties;
}
}
Loading

0 comments on commit a26147d

Please sign in to comment.