1: <?php
2:
3: namespace vierbergenlars\Forage\ODM;
4:
5: use vierbergenlars\Forage\SearchIndex\Index;
6: use vierbergenlars\Forage\Transport\TransportInterface;
7:
8: /**
9: * A search index that can index object
10: */
11: class SearchIndex extends Index
12: {
13:
14: /**
15: * The hydration settings
16: * @var \vierbergenlars\Forage\ODM\HydrationSettingsInterface
17: */
18: protected $hydrationSettings;
19:
20: /**
21: * @internal
22: * @param \vierbergenlars\Forage\Transport\TransportInterface $transport
23: * @param \vierbergenlars\Forage\ODM\HydrationSettingsInterface $hydrationSettings
24: */
25: public function __construct(TransportInterface $transport,
26: HydrationSettingsInterface $hydrationSettings)
27: {
28: $this->hydrationSettings = $hydrationSettings;
29: parent::__construct($transport);
30: }
31:
32: /**
33: * {@inheritDoc}
34: *
35: * @param object $document
36: * @param null $_ Unused parameter
37: * @return \vierbergenlars\Forage\ODM\SearchIndex
38: */
39: public function addDocument($document, $_ = null)
40: {
41: parent::addDocument(
42: $this->hydrationSettings->getDocumentId($document),
43: $this->hydrationSettings->getDocument($document)
44: );
45: return $this;
46: }
47:
48: /**
49: * {@inheritDoc}
50: *
51: * @param object|string|int $document
52: * When the document to remove is given as a string or an integer,
53: * it is used as an id. If it is an object, {@link HydrationSettingsInterface::getDocument()}
54: * gets called, and the document id will be determined from the returned array.
55: * @param null $_ Unused parameter
56: * @return \vierbergenlars\Forage\ODM\SearchIndex
57: */
58: public function removeDocument($document, $_ = null)
59: {
60: if(is_object($document)) {
61: parent::removeDocument(
62: $this->hydrationSettings->getDocumentId($document)
63: );
64: } else {
65: parent::removeDocument($document);
66: }
67: return $this;
68: }
69:
70: }
71: