| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace TreeHouse\IoBundle\Item\Modifier\Item\Transformer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Symfony\Component\HttpFoundation\ParameterBag; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use TreeHouse\Feeder\Exception\UnexpectedTypeException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use TreeHouse\Feeder\Modifier\Item\Transformer\TransformerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * Transforms serialized (XML) node into a text field:. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * Example: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * ``` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * link => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  *   rel => external | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  *   #   => http://example.org | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * ``` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * becomes: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * ``` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * link => http://example.org | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * ``` | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 26 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  | class NodeToTextTransformer implements TransformerInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |      * @var string | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |     protected $field; | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |      * @param string $field Transform a specific field, if omitted, all root-level fields are transformed | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |      * @throws UnexpectedTypeException | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 8 |  |     public function __construct($field = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 8 |  |         if (!is_string($field) && !is_null($field)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 2 |  |             throw new UnexpectedTypeException($field, 'string or null'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 6 |  |         $this->field = $field; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 46 | 6 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |      * @inheritdoc | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 6 |  |     public function transform(ParameterBag $item) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 6 |  |         $keys = $this->field ? [$this->field] : $item->keys(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 6 |  |         $this->replace($item, $keys); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 56 | 6 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |      * @param ParameterBag $item | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |      * @param array        $keys | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 6 |  |     protected function replace(ParameterBag $item, array $keys) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 6 |  |         foreach ($keys as $key) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 6 |  |             if (!$item->has($key)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 2 |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 4 |  |             $value = $item->get($key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             // if value is an array with a hash, that's a serialized node's text value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 4 |  |             if (is_array($value) && array_key_exists('#', $value)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 4 |  |                 $item->set($key, $value['#']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 76 | 6 |  |     } | 
            
                                                        
            
                                    
            
            
                | 77 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 78 |  |  |  |