| @@ 20-58 (lines=39) @@ | ||
| 17 | /** |
|
| 18 | * Maps IdentifierField document field values to something Elasticsearch can index. |
|
| 19 | */ |
|
| 20 | class IdentifierMapper extends FieldValueMapper |
|
| 21 | { |
|
| 22 | /** |
|
| 23 | * Check if field can be mapped. |
|
| 24 | * |
|
| 25 | * @param \eZ\Publish\SPI\Search\Field $field |
|
| 26 | * |
|
| 27 | * @return bool |
|
| 28 | */ |
|
| 29 | public function canMap(Field $field) |
|
| 30 | { |
|
| 31 | return $field->type instanceof IdentifierField; |
|
| 32 | } |
|
| 33 | ||
| 34 | /** |
|
| 35 | * Map field value to a proper Elasticsearch representation. |
|
| 36 | * |
|
| 37 | * @param Field $field |
|
| 38 | * |
|
| 39 | * @return mixed |
|
| 40 | */ |
|
| 41 | public function map(Field $field) |
|
| 42 | { |
|
| 43 | return $this->convert($field->value); |
|
| 44 | } |
|
| 45 | ||
| 46 | /** |
|
| 47 | * Convert to a proper Elasticsearch representation. |
|
| 48 | * |
|
| 49 | * @param mixed $value |
|
| 50 | * |
|
| 51 | * @return string |
|
| 52 | */ |
|
| 53 | protected function convert($value) |
|
| 54 | { |
|
| 55 | // Remove non-printable characters |
|
| 56 | return preg_replace('([^A-Za-z0-9/]+)', '', $value); |
|
| 57 | } |
|
| 58 | } |
|
| 59 | ||
| @@ 20-60 (lines=41) @@ | ||
| 17 | /** |
|
| 18 | * Maps raw document field values to something Elasticsearch can index. |
|
| 19 | */ |
|
| 20 | class StringMapper extends FieldValueMapper |
|
| 21 | { |
|
| 22 | /** |
|
| 23 | * Check if field can be mapped. |
|
| 24 | * |
|
| 25 | * @param \eZ\Publish\SPI\Search\Field $field |
|
| 26 | * |
|
| 27 | * @return bool |
|
| 28 | */ |
|
| 29 | public function canMap(Field $field) |
|
| 30 | { |
|
| 31 | return |
|
| 32 | $field->type instanceof FieldType\StringField || |
|
| 33 | $field->type instanceof FieldType\TextField; |
|
| 34 | } |
|
| 35 | ||
| 36 | /** |
|
| 37 | * Map field value to a proper Elasticsearch representation. |
|
| 38 | * |
|
| 39 | * @param \eZ\Publish\SPI\Search\Field $field |
|
| 40 | * |
|
| 41 | * @return mixed |
|
| 42 | */ |
|
| 43 | public function map(Field $field) |
|
| 44 | { |
|
| 45 | return $this->convert($field->value); |
|
| 46 | } |
|
| 47 | ||
| 48 | /** |
|
| 49 | * Convert to a proper Elasticsearch representation. |
|
| 50 | * |
|
| 51 | * @param mixed $value |
|
| 52 | * |
|
| 53 | * @return string |
|
| 54 | */ |
|
| 55 | protected function convert($value) |
|
| 56 | { |
|
| 57 | // Remove non-printable characters |
|
| 58 | return preg_replace('([\x00-\x09\x0B\x0C\x1E\x1F]+)', '', (string)$value); |
|
| 59 | } |
|
| 60 | } |
|
| 61 | ||