/** * Validate the getters and setters work as expected. * * @test */ public function validateMappingGettersAndSetters() { $mapping = new ResourceMapping(); $this->assertNull($mapping->getPrimary()); $this->assertInstanceOf(ArrayCollection::class, $mapping->getFields()); $this->assertCount(0, $mapping->getFields()); $this->assertInstanceOf(ArrayCollection::class, $mapping->getAssociations()); $this->assertCount(0, $mapping->getAssociations()); $primary = new FieldMapping(); $this->assertEquals($mapping, $mapping->setPrimary($primary)); $this->assertEquals($primary, $mapping->getPrimary()); }
/** * @param ResourceMapping $resource * @param ReflectionProperty $property * @param ArrayCollection $annotations * @return ResourceMapping * * @throws InvalidMappingException when a association is defined without a field definition. * @throws InvalidMappingException when a association is defined on a primary field. * @throws InvalidMappingException when a association target is an invalid class. */ public function processFieldAnnotationsForResourceMapping(ResourceMapping $resource, ReflectionProperty $property, ArrayCollection $annotations) { $field = $this->processFieldAnnotations($property, $annotations); if ($field->getAssociation()) { if ($field->getField()) { if ($field->getField()->primary) { throw new InvalidMappingException(sprintf(InvalidMappingException::MESSAGE_MISSING_FIELD_PRIMARY, $property->getDeclaringClass()->getName())); } if (!class_exists($field->getAssociation()->target)) { throw new InvalidMappingException(sprintf(InvalidMappingException::MESSAGE_INVALID_ASSOCIATION_TARGET, $property->getDeclaringClass()->getName(), $property->getName())); } $resource->getAssociations()->add($field); return $resource; } else { throw new InvalidMappingException(sprintf(InvalidMappingException::MESSAGE_MISSING_ASSOCIATION_FIELD, $property->getDeclaringClass()->getName(), $property->getName())); } } if ($field->getField()) { $resource->getFields()->add($field); if ($field->getField()->primary) { $resource->setPrimary($field); } } return $resource; }