/** * Parse the given map description obtained from the configuration into * a real Map instance. * * @param array $map Associative array describing the map, from config * @return Map */ private function parseMap($map) { $builder = new MapBuilder(); if (!isset($map['fields'])) { return $builder->build(); } foreach ($map['fields'] as $field) { $map = null; if (isset($field['map'])) { $map = $this->parseMap($field['map']); } $types = null; if (isset($field['types'])) { $types = $field['types']; } else { if (isset($field['type'])) { $types = array($field['type']); } } $transformation = null; if (isset($field['transformation'])) { $transformation = $this->getTransformationFromConfig($field['transformation']); } $builder->field($field['from'], $field['to'], $types, $map, $transformation); } return $builder->build(); }
/** * Map using the @MapTo() annotations found in the object * @param object $modelOrClass A class or object to map from * @param object $entityOrClass A class or object to map to */ public function mapFromModel($modelOrClass, $entityOrClass) { $class = new \ReflectionClass($modelOrClass); $annotationName = 'Rezonant\\MapperBundle\\Annotations\\MapTo'; $transformationAnnotationName = 'Rezonant\\MapperBundle\\Annotations\\Transformation'; $excludeAnnotationName = 'Rezonant\\MapperBundle\\Annotations\\Exclude'; $map = new MapBuilder(); $destinationClass = new \ReflectionClass($entityOrClass); foreach ($class->getProperties() as $property) { $annotation = $this->annotationReader->getPropertyAnnotation($property, $annotationName); $transformationAnnotation = $this->annotationReader->getPropertyAnnotation($property, $transformationAnnotationName); $excludeAnnotation = $this->annotationReader->getPropertyAnnotation($property, $excludeAnnotationName); // Resolve the type of this property for submapping later. $subSourceType = $this->reflector->getTypeFromProperty($property); $subDestType = null; $fieldValue = null; $submap = null; $destinationField = null; $exclude = false; // Use the annoted destination field, or assume that the mapping // is 1-to-1. if ($annotation) { $destinationField = $annotation->value; } else { if ($destinationClass->hasProperty($property->name)) { $destinationField = $property->name; } } //Get transformation if there is one $transformation = $this->getTransformationFromAnnotation($transformationAnnotation); if ($excludeAnnotation) { $exclude = true; } // Resolve the destination field's type for generating a submap. if ($destinationField) { $destReference = new Reference($destinationField, $destinationClass); $subDestTypes = $destReference->getTypes(); $subDestType = $subDestTypes[count($subDestTypes) - 1]; if ($subSourceType && $subDestType) { $submap = $this->mapFromModel($subSourceType, $subDestType); } $map->field(new Reference($property->name, $class), $destReference, $submap, $transformation, $exclude); } } return $map->build(); }