1
|
|
|
<?php |
2
|
|
|
namespace ApacheSolrForTypo3\Solr\FieldProcessor; |
3
|
|
|
|
4
|
|
|
/*************************************************************** |
5
|
|
|
* Copyright notice |
6
|
|
|
* |
7
|
|
|
* (c) 2009-2015 Daniel Poetzinger <[email protected]> |
8
|
|
|
* All rights reserved |
9
|
|
|
* |
10
|
|
|
* This script is part of the TYPO3 project. The TYPO3 project is |
11
|
|
|
* free software; you can redistribute it and/or modify |
12
|
|
|
* it under the terms of the GNU General Public License as published by |
13
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
14
|
|
|
* (at your option) any later version. |
15
|
|
|
* |
16
|
|
|
* The GNU General Public License can be found at |
17
|
|
|
* http://www.gnu.org/copyleft/gpl.html. |
18
|
|
|
* |
19
|
|
|
* This script is distributed in the hope that it will be useful, |
20
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
21
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22
|
|
|
* GNU General Public License for more details. |
23
|
|
|
* |
24
|
|
|
* This copyright notice MUST APPEAR in all copies of the script! |
25
|
|
|
***************************************************************/ |
26
|
|
|
|
27
|
|
|
use ApacheSolrForTypo3\Solr\FieldProcessor\CategoryUidToHierarchy; |
28
|
|
|
use ApacheSolrForTypo3\Solr\FieldProcessor\PageUidToHierarchy; |
29
|
|
|
use ApacheSolrForTypo3\Solr\FieldProcessor\PathToHierarchy; |
30
|
|
|
use ApacheSolrForTypo3\Solr\FieldProcessor\TimestampToIsoDate; |
31
|
|
|
use ApacheSolrForTypo3\Solr\FieldProcessor\TimestampToUtcIsoDate; |
32
|
|
|
use ApacheSolrForTypo3\Solr\System\Solr\Document\Document; |
33
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Service class that modifies fields in a Apache Solr Document, used for |
37
|
|
|
* common field processing during indexing or resolving |
38
|
|
|
* |
39
|
|
|
* @author Daniel Poetzinger <[email protected]> |
40
|
|
|
*/ |
41
|
|
|
class Service |
42
|
|
|
{ |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Modifies a list of documents |
46
|
|
|
* |
47
|
|
|
* @param Document[] $documents |
48
|
|
|
* @param array $processingConfiguration |
49
|
|
|
*/ |
50
|
82 |
|
public function processDocuments(array $documents, array $processingConfiguration) { |
51
|
82 |
|
foreach ($documents as $document) { |
52
|
82 |
|
$this->processDocument($document, $processingConfiguration); |
53
|
|
|
} |
54
|
82 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* modifies a document according to the given configuration |
58
|
|
|
* |
59
|
|
|
* @param Document $document |
60
|
|
|
* @param array $processingConfiguration |
61
|
|
|
*/ |
62
|
86 |
|
public function processDocument(Document $document, array $processingConfiguration) { |
63
|
86 |
|
foreach ($processingConfiguration as $fieldName => $instruction) { |
64
|
86 |
|
$fieldValue = $document[$fieldName] ?? false; |
65
|
86 |
|
$isSingleValueField = false; |
66
|
|
|
|
67
|
86 |
|
if ($fieldValue !== false) { |
68
|
86 |
|
if (!is_array($fieldValue)) { |
69
|
|
|
// turn single value field into multi value field |
70
|
84 |
|
$fieldValue = [$fieldValue]; |
71
|
84 |
|
$isSingleValueField = true; |
72
|
|
|
} |
73
|
|
|
|
74
|
86 |
|
switch ($instruction) { |
75
|
86 |
|
case 'timestampToUtcIsoDate': |
76
|
|
|
/** @var $processor TimestampToUtcIsoDate */ |
77
|
|
|
$processor = GeneralUtility::makeInstance(TimestampToUtcIsoDate::class); |
78
|
|
|
$fieldValue = $processor->process($fieldValue); |
79
|
|
|
break; |
80
|
86 |
|
case 'timestampToIsoDate': |
81
|
|
|
/** @var $processor TimestampToIsoDate */ |
82
|
84 |
|
$processor = GeneralUtility::makeInstance(TimestampToIsoDate::class); |
83
|
84 |
|
$fieldValue = $processor->process($fieldValue); |
84
|
84 |
|
break; |
85
|
66 |
|
case 'pathToHierarchy': |
86
|
|
|
/** @var $processor PathToHierarchy */ |
87
|
|
|
$processor = GeneralUtility::makeInstance(PathToHierarchy::class); |
88
|
|
|
$fieldValue = $processor->process($fieldValue); |
89
|
|
|
break; |
90
|
66 |
|
case 'pageUidToHierarchy': |
91
|
|
|
/** @var $processor PageUidToHierarchy */ |
92
|
64 |
|
$processor = GeneralUtility::makeInstance(PageUidToHierarchy::class); |
93
|
64 |
|
$fieldValue = $processor->process($fieldValue); |
94
|
64 |
|
break; |
95
|
33 |
|
case 'categoryUidToHierarchy': |
96
|
|
|
/** @var $processor CategoryUidToHierarchy */ |
97
|
31 |
|
$processor = GeneralUtility::makeInstance(CategoryUidToHierarchy::class); |
98
|
31 |
|
$fieldValue = $processor->process($fieldValue); |
99
|
31 |
|
break; |
100
|
2 |
|
case 'uppercase': |
101
|
2 |
|
$fieldValue = array_map('mb_strtoupper', $fieldValue); |
102
|
2 |
|
break; |
103
|
|
|
} |
104
|
|
|
|
105
|
86 |
|
if ($isSingleValueField) { |
106
|
|
|
// turn multi value field back into single value field |
107
|
84 |
|
$fieldValue = $fieldValue[0]; |
108
|
|
|
} |
109
|
|
|
|
110
|
86 |
|
$document->setField($fieldName, $fieldValue); |
111
|
|
|
} |
112
|
|
|
} |
113
|
86 |
|
} |
114
|
|
|
} |
115
|
|
|
|