|
| 1 | +=========== |
| 2 | +Transformer |
| 3 | +=========== |
| 4 | + |
| 5 | +The Transformer classes allows you to transform your data before it's stored in elasticsearch. |
| 6 | + |
| 7 | +Transformer annotations |
| 8 | +======================= |
| 9 | +This package ships several Transformer by default: |
| 10 | + |
| 11 | +* CollectionStringCastTransformer: |
| 12 | + |
| 13 | + * Iterates over an Collection/Array and casts all items to a string and returns an array with strings |
| 14 | + |
| 15 | +* DateTransformer |
| 16 | + |
| 17 | + * Converts a DateTime-Object to formatted string. Defaults to Y-m-d |
| 18 | + |
| 19 | +* ObectIdentifierTransformer |
| 20 | + |
| 21 | + * Converts an object to it's persistence identifier |
| 22 | + |
| 23 | +* StringCastTransformer |
| 24 | + |
| 25 | + * Converts a value to a string |
| 26 | + |
| 27 | +* TextCastTransformer |
| 28 | + |
| 29 | + * Converts a value to a string |
| 30 | + |
| 31 | + |
| 32 | +Usage |
| 33 | +===== |
| 34 | + |
| 35 | +To transform an objects property at index time you need to annotate your value like this: |
| 36 | + |
| 37 | +*Example: Use a Transformer* :: |
| 38 | + |
| 39 | + /** |
| 40 | + * @var \DateTime |
| 41 | + * @ElasticSearch\Transform(options={"format" = "Y-m-d H:m:s"}, type="date") |
| 42 | + */ |
| 43 | + protected $date; |
| 44 | + |
| 45 | + |
| 46 | +The `type` option is usded to determine the corresponding transformer class. Date would resolve to `Flowpack\ElasticSearch\Indexer\Object\Transform\DateTransformer`. |
| 47 | +and call it's implementation of `transformByAnnotation($source, TransformAnnotation $annotation)`. |
| 48 | +All default transformer can be used just like this. |
| 49 | + |
| 50 | + |
| 51 | +Implement a custom Transformer |
| 52 | +============================== |
| 53 | +If you need a custom transformer you need to implement the TrnasformerInterface. |
| 54 | +It declares two methods: |
| 55 | + |
| 56 | +* getTargetMappingType: return value is used as mapping type in elasticsearch (one of https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-types.html) |
| 57 | + |
| 58 | +* transformByAnnotation: actual implementation of your transform process |
| 59 | + |
| 60 | + |
| 61 | +*Example: Custom Transformer that implements TransformerInterface and crops the source length* :: |
| 62 | + |
| 63 | + <?php |
| 64 | + namespace Some\Vendor\Indexer\Object\Transform; |
| 65 | + |
| 66 | + use Flowpack\ElasticSearch\Annotations\Transform as TransformAnnotation; |
| 67 | + use Neos\Flow\Annotations as Flow; |
| 68 | + |
| 69 | + /** |
| 70 | + * @Flow\Scope("singleton") |
| 71 | + */ |
| 72 | + class CropTransformer implements TransformerInterface |
| 73 | + { |
| 74 | + |
| 75 | + /** |
| 76 | + * Returns the Elasticsearch type that is used as mappping type |
| 77 | + * |
| 78 | + * @return string |
| 79 | + */ |
| 80 | + public function getTargetMappingType() { |
| 81 | + return 'text' |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * This is actually callled to transform the $source value |
| 86 | + * |
| 87 | + * @param mixed $source |
| 88 | + * @param TransformAnnotation $annotation |
| 89 | + * @return mixed |
| 90 | + */ |
| 91 | + public function transformByAnnotation($source, TransformAnnotation $annotation) { |
| 92 | + |
| 93 | + if ($source === null) { |
| 94 | + return ''; |
| 95 | + } |
| 96 | + |
| 97 | + if ($annotation->options['length']) { |
| 98 | + return substr((string) $source, 0, $annotation->options['length']); |
| 99 | + } |
| 100 | + |
| 101 | + return (string) $source |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | +*Example: Annotation usage:* :: |
| 108 | + |
| 109 | + /** |
| 110 | + * @Flow\Entity |
| 111 | + * @ElasticSearch\Indexable("twitter", typeName="tweet") |
| 112 | + */ |
| 113 | + class Tweet { |
| 114 | + |
| 115 | + /** |
| 116 | + * @var string |
| 117 | + */ |
| 118 | + protected $username; |
| 119 | + |
| 120 | + /** |
| 121 | + * @var string |
| 122 | + * @ElasticSearch\Transform(options={"length" = 20}, type="Some\Vendor\Indexer\Object\Transform\CropTransformer") |
| 123 | + */ |
| 124 | + protected $message; |
| 125 | + |
| 126 | + /** |
| 127 | + * @var \DateTime |
| 128 | + */ |
| 129 | + protected $date; |
| 130 | + } |
| 131 | + |
| 132 | + |
| 133 | +With this configuration the message will always be cropped to 20 chars when it's indexed |
| 134 | + |
0 commit comments