Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
227 changes: 227 additions & 0 deletions Document/AbstractDocument.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
<?php

/*
* This file is part of the ONGR package.
*
* (c) NFQ Technologies UAB <info@nfq.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ONGR\ElasticsearchBundle\Document;

use ONGR\ElasticsearchBundle\Result\DocumentHighlight;

/**
* Document abstraction which introduces mandatory fields for the document.
*/
class AbstractDocument implements DocumentInterface
{
/**
* @var string
*/
public $id;

/**
* @var string
*/
public $score;

/**
* @var string
*/
public $parent;

/**
* @var string
*/
public $ttl;

/**
* @var DocumentHighlight
*/
public $highlight;

/**
* Legacy property support.
*
* @param string $property
*
* @return null|string
*/
public function __get($property)
{
switch ($property) {
case '_id':
return $this->id;
case '_score':
return $this->score;
case '_ttl':
return $this->ttl;
case '_parent':
return $this->parent;
default:
return null;
}
}

/**
* Legacy property support and some special properties.
*
* @param string $property
* @param mixed $value
*/
public function __set($property, $value)
{
switch ($property) {
case '_id':
$this->setId($value);
break;
case '_score':
$this->setScore($value);
break;
case '_ttl':
$this->setTtl($value);
break;
case '_parent':
$this->setParent($value);
break;
default:
$this->{$property} = $value;
break;
}
}

/**
* Sets document unique id.
*
* @param string $documentId
*
* @return DocumentInterface
*/
public function setId($documentId)
{
$this->id = $documentId;

return $this;
}

/**
* Returns document id.
*
* @return string
*/
public function getId()
{
return $this->id;
}

/**
* Sets document score.
*
* @param string $documentScore
*
* @return DocumentInterface
*/
public function setScore($documentScore)
{
$this->score = $documentScore;

return $this;
}

/**
* Gets document score.
*
* @return string
*/
public function getScore()
{
return $this->score;
}

/**
* Sets parent document id.
*
* @param string $parent
*
* @return DocumentInterface
*/
public function setParent($parent)
{
$this->parent = $parent;

return $this;
}

/**
* Returns parent document id.
*
* @return null|string
*/
public function getParent()
{
return $this->parent;
}

/**
* Checks if document has a parent.
*
* @return bool
*/
public function hasParent()
{
return $this->parent !== null;
}

/**
* Sets highlight.
*
* @param DocumentHighlight $highlight
*/
public function setHighlight(DocumentHighlight $highlight)
{
$this->highlight = $highlight;
}

/**
* Returns highlight.
*
* @throws \UnderflowException
*
* @return DocumentHighlight
*/
public function getHighLight()
{
if ($this->highlight === null) {
throw new \UnderflowException('Highlight not set.');
}

return $this->highlight;
}

/**
* Sets time to live timestamp.
*
* @param string $ttl
*
* @return DocumentInterface
*/
public function setTtl($ttl)
{
$this->ttl = $ttl;

return $this;
}

/**
* Returns time to live value.
*
* @return int
*/
public function getTtl()
{
return $this->ttl;
}
}
2 changes: 2 additions & 0 deletions Document/DocumentTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

/**
* Trait with common document fields and methods.
*
* @deprecated Use AbstractDocument instead, will remove in 1.0
*/
trait DocumentTrait
{
Expand Down
4 changes: 2 additions & 2 deletions Tests/app/fixture/Acme/TestBundle/Document/Bar.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace ONGR\ElasticsearchBundle\Tests\app\fixture\Acme\TestBundle\Document;

use ONGR\ElasticsearchBundle\Annotation as ES;
use ONGR\ElasticsearchBundle\Document\AbstractDocument;
use ONGR\ElasticsearchBundle\Document\DocumentInterface;
use ONGR\ElasticsearchBundle\Document\DocumentTrait;

Expand All @@ -20,7 +21,6 @@
*
* @ES\Document(type="bar")
*/
class Bar implements DocumentInterface
class Bar extends AbstractDocument
{
use DocumentTrait;
}
1 change: 0 additions & 1 deletion Tests/app/fixture/Acme/TestBundle/Document/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,5 @@
*/
class Category
{

public $hiddenField;
}
4 changes: 2 additions & 2 deletions Tests/app/fixture/Acme/TestBundle/Document/ColorDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace ONGR\ElasticsearchBundle\Tests\app\fixture\Acme\TestBundle\Document;

use ONGR\ElasticsearchBundle\Annotation as ES;
use ONGR\ElasticsearchBundle\Document\AbstractDocument;
use ONGR\ElasticsearchBundle\Document\DocumentInterface;
use ONGR\ElasticsearchBundle\Document\DocumentTrait;

Expand All @@ -20,7 +21,6 @@
*
* @ES\Document(type="color")
*/
class ColorDocument implements DocumentInterface
class ColorDocument extends AbstractDocument
{
use DocumentTrait;
}
5 changes: 2 additions & 3 deletions Tests/app/fixture/Acme/TestBundle/Document/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace ONGR\ElasticsearchBundle\Tests\app\fixture\Acme\TestBundle\Document;

use ONGR\ElasticsearchBundle\Annotation as ES;
use ONGR\ElasticsearchBundle\Document\AbstractDocument;
use ONGR\ElasticsearchBundle\Document\DocumentInterface;
use ONGR\ElasticsearchBundle\Document\DocumentTrait;

Expand All @@ -20,10 +21,8 @@
*
* @ES\Document(type="comment", parent="AcmeTestBundle:Content", ttl={"enabled":true, "default": "1d"})
*/
class Comment implements DocumentInterface
class Comment extends AbstractDocument
{
use DocumentTrait;

/**
* @var string
*
Expand Down
5 changes: 2 additions & 3 deletions Tests/app/fixture/Acme/TestBundle/Document/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace ONGR\ElasticsearchBundle\Tests\app\fixture\Acme\TestBundle\Document;

use ONGR\ElasticsearchBundle\Annotation as ES;
use ONGR\ElasticsearchBundle\Document\AbstractDocument;
use ONGR\ElasticsearchBundle\Document\DocumentInterface;
use ONGR\ElasticsearchBundle\Document\DocumentTrait;

Expand All @@ -20,10 +21,8 @@
*
* @ES\Document(type="fooContent")
*/
class Content implements DocumentInterface
class Content extends AbstractDocument
{
use DocumentTrait;

/**
* @var string
*
Expand Down
5 changes: 2 additions & 3 deletions Tests/app/fixture/Acme/TestBundle/Document/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace ONGR\ElasticsearchBundle\Tests\app\fixture\Acme\TestBundle\Document;

use ONGR\ElasticsearchBundle\Annotation as ES;
use ONGR\ElasticsearchBundle\Document\AbstractDocument;
use ONGR\ElasticsearchBundle\Document\DocumentInterface;
use ONGR\ElasticsearchBundle\Document\DocumentTrait;

Expand All @@ -20,10 +21,8 @@
*
* @ES\Document(create=false)
*/
class Item implements DocumentInterface
class Item extends AbstractDocument
{
use DocumentTrait;

/**
* @var string
*
Expand Down
4 changes: 1 addition & 3 deletions Tests/app/fixture/Acme/TestBundle/Document/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
namespace ONGR\ElasticsearchBundle\Tests\app\fixture\Acme\TestBundle\Document;

use ONGR\ElasticsearchBundle\Annotation as ES;
use ONGR\ElasticsearchBundle\Document\DocumentInterface;
use ONGR\ElasticsearchBundle\Document\DocumentTrait;

/**
* Product document for testing.
Expand All @@ -22,7 +20,7 @@
* @ES\Skip({"name"})
* @ES\Inherit({"price"})
*/
class Product extends Item implements DocumentInterface
class Product extends Item
{
/**
* @var string
Expand Down
5 changes: 2 additions & 3 deletions Tests/app/fixture/Acme/TestBundle/Document/Test/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace ONGR\ElasticsearchBundle\Tests\app\fixture\Acme\TestBundle\Document\Test;

use ONGR\ElasticsearchBundle\Annotation as ES;
use ONGR\ElasticsearchBundle\Document\AbstractDocument;
use ONGR\ElasticsearchBundle\Document\DocumentInterface;
use ONGR\ElasticsearchBundle\Document\DocumentTrait;

Expand All @@ -20,10 +21,8 @@
*
* @ES\Document(create=false)
*/
class Item implements DocumentInterface
class Item extends AbstractDocument
{
use DocumentTrait;

/**
* @var string
*
Expand Down