From 278287dcafa0ac058c259d28bf31dc688228d1d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simonas=20=C5=A0erlinskas?= Date: Mon, 2 Feb 2015 10:20:15 +0200 Subject: [PATCH] introduced abstract document --- Document/AbstractDocument.php | 227 ++++++++++++++++++ Document/DocumentTrait.php | 2 + .../fixture/Acme/TestBundle/Document/Bar.php | 4 +- .../Acme/TestBundle/Document/Category.php | 1 - .../TestBundle/Document/ColorDocument.php | 4 +- .../Acme/TestBundle/Document/Comment.php | 5 +- .../Acme/TestBundle/Document/Content.php | 5 +- .../fixture/Acme/TestBundle/Document/Item.php | 5 +- .../Acme/TestBundle/Document/Product.php | 4 +- .../Acme/TestBundle/Document/Test/Item.php | 5 +- 10 files changed, 242 insertions(+), 20 deletions(-) create mode 100644 Document/AbstractDocument.php diff --git a/Document/AbstractDocument.php b/Document/AbstractDocument.php new file mode 100644 index 00000000..af7129ef --- /dev/null +++ b/Document/AbstractDocument.php @@ -0,0 +1,227 @@ + + * + * 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; + } +} diff --git a/Document/DocumentTrait.php b/Document/DocumentTrait.php index 780463e8..f4e294d0 100644 --- a/Document/DocumentTrait.php +++ b/Document/DocumentTrait.php @@ -15,6 +15,8 @@ /** * Trait with common document fields and methods. + * + * @deprecated Use AbstractDocument instead, will remove in 1.0 */ trait DocumentTrait { diff --git a/Tests/app/fixture/Acme/TestBundle/Document/Bar.php b/Tests/app/fixture/Acme/TestBundle/Document/Bar.php index 4c143157..0f1fe1cf 100644 --- a/Tests/app/fixture/Acme/TestBundle/Document/Bar.php +++ b/Tests/app/fixture/Acme/TestBundle/Document/Bar.php @@ -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; @@ -20,7 +21,6 @@ * * @ES\Document(type="bar") */ -class Bar implements DocumentInterface +class Bar extends AbstractDocument { - use DocumentTrait; } diff --git a/Tests/app/fixture/Acme/TestBundle/Document/Category.php b/Tests/app/fixture/Acme/TestBundle/Document/Category.php index c08a5061..066fbdca 100644 --- a/Tests/app/fixture/Acme/TestBundle/Document/Category.php +++ b/Tests/app/fixture/Acme/TestBundle/Document/Category.php @@ -20,6 +20,5 @@ */ class Category { - public $hiddenField; } diff --git a/Tests/app/fixture/Acme/TestBundle/Document/ColorDocument.php b/Tests/app/fixture/Acme/TestBundle/Document/ColorDocument.php index 06e0af86..ac723102 100644 --- a/Tests/app/fixture/Acme/TestBundle/Document/ColorDocument.php +++ b/Tests/app/fixture/Acme/TestBundle/Document/ColorDocument.php @@ -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; @@ -20,7 +21,6 @@ * * @ES\Document(type="color") */ -class ColorDocument implements DocumentInterface +class ColorDocument extends AbstractDocument { - use DocumentTrait; } diff --git a/Tests/app/fixture/Acme/TestBundle/Document/Comment.php b/Tests/app/fixture/Acme/TestBundle/Document/Comment.php index eb13c59e..5ab4a008 100644 --- a/Tests/app/fixture/Acme/TestBundle/Document/Comment.php +++ b/Tests/app/fixture/Acme/TestBundle/Document/Comment.php @@ -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; @@ -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 * diff --git a/Tests/app/fixture/Acme/TestBundle/Document/Content.php b/Tests/app/fixture/Acme/TestBundle/Document/Content.php index c171ebfc..9e0a8e2e 100644 --- a/Tests/app/fixture/Acme/TestBundle/Document/Content.php +++ b/Tests/app/fixture/Acme/TestBundle/Document/Content.php @@ -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; @@ -20,10 +21,8 @@ * * @ES\Document(type="fooContent") */ -class Content implements DocumentInterface +class Content extends AbstractDocument { - use DocumentTrait; - /** * @var string * diff --git a/Tests/app/fixture/Acme/TestBundle/Document/Item.php b/Tests/app/fixture/Acme/TestBundle/Document/Item.php index 7a7a21b2..7d9fcc73 100644 --- a/Tests/app/fixture/Acme/TestBundle/Document/Item.php +++ b/Tests/app/fixture/Acme/TestBundle/Document/Item.php @@ -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; @@ -20,10 +21,8 @@ * * @ES\Document(create=false) */ -class Item implements DocumentInterface +class Item extends AbstractDocument { - use DocumentTrait; - /** * @var string * diff --git a/Tests/app/fixture/Acme/TestBundle/Document/Product.php b/Tests/app/fixture/Acme/TestBundle/Document/Product.php index f20a379d..3474f99a 100644 --- a/Tests/app/fixture/Acme/TestBundle/Document/Product.php +++ b/Tests/app/fixture/Acme/TestBundle/Document/Product.php @@ -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. @@ -22,7 +20,7 @@ * @ES\Skip({"name"}) * @ES\Inherit({"price"}) */ -class Product extends Item implements DocumentInterface +class Product extends Item { /** * @var string diff --git a/Tests/app/fixture/Acme/TestBundle/Document/Test/Item.php b/Tests/app/fixture/Acme/TestBundle/Document/Test/Item.php index 45fc3db2..015b83e9 100644 --- a/Tests/app/fixture/Acme/TestBundle/Document/Test/Item.php +++ b/Tests/app/fixture/Acme/TestBundle/Document/Test/Item.php @@ -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; @@ -20,10 +21,8 @@ * * @ES\Document(create=false) */ -class Item implements DocumentInterface +class Item extends AbstractDocument { - use DocumentTrait; - /** * @var string *