public function testHideFields() { $bar = new Foo('Some'); $bar->model = UnknownValue::create(); $result = $this->serialize($bar); $this->assertArrayNotHasKey('model', $result); }
private function isPropertyDTO($type, $value) { if ($value === UnknownValue::create()) { return false; } if ($type['name'] == 'DTO') { return is_object($value); } elseif ($type['name'] == 'array' && $type['params'] && $type['params'][0]['name'] == 'DTO') { return count($value) && is_object($value[0]); } else { return false; } }
public function shouldSkipProperty(PropertyMetadata $property, Context $context) { if ($context instanceof DeserializationContext) { return false; } $data = $context->getObject(); if ($data instanceof \Staffim\DTOBundle\Hateoas\CollectionRepresentation) { return false; } if ($property->class == 'Hateoas\\Configuration\\Relation') { return false; } return is_object($data) && $property->getValue($data) === UnknownValue::create(); }
/** * @param mixed $model * @param \Staffim\DTOBundle\DTO\Model\DTOInterface $dto * @param string $propertyName * @param array $fullPropertyPath */ private function updateProperty($model, DTOInterface $dto, $propertyName, array $fullPropertyPath = []) { $fullPropertyPath[] = $propertyName; if (!$this->isPropertyVisible($fullPropertyPath)) { $modelValue = UnknownValue::create(); } else { try { $modelValue = $this->propertyAccessor->getValue($model, $propertyName); } catch (NoSuchPropertyException $e) { return; } } $this->propertyAccessor->setValue($dto, $propertyName, $this->convertValue($modelValue, $fullPropertyPath)); }
function it_should_hide_fields_in_embedded_collections($propertyAccessor, $mappingConfigurator, $factory, ModelInterface $model, ModelDTO $modelDto, ModelInterface $user, UserDTO $userDTO, ModelInterface $parent, ParentDTO $parentDTO, ModelCollection $collection) { $mappingConfigurator->hasRelation(['a'])->willReturn(true); $mappingConfigurator->hasRelation(['a', 'parent'])->willReturn(true); $mappingConfigurator->isPropertyVisible(['name'])->willReturn(false); $mappingConfigurator->isPropertyVisible(['a', 'id'])->willReturn(true); $mappingConfigurator->isPropertyVisible(['a', 'name'])->willReturn(false); $mappingConfigurator->isPropertyVisible(['a', 'parent'])->willReturn(true); $mappingConfigurator->isPropertyVisible(['a', 'parent', 'id'])->willReturn(true); $mappingConfigurator->isPropertyVisible(['a', 'parent', 'name'])->willReturn(true); $factory->create($model)->willReturn($modelDto); $factory->create($user)->willReturn($userDTO); $factory->create($parent)->willReturn($parentDTO); //get $propertyAccessor->getValue($model, 'a')->willReturn($collection); $this->stubIterator($collection, new \ArrayIterator([$user->getWrappedObject()])); $propertyAccessor->getValue($user, 'id')->willReturn('abel_id'); $propertyAccessor->getValue($user, 'name')->willReturn('Abel'); $propertyAccessor->getValue($user, 'parent')->willReturn($parent); $propertyAccessor->getValue($parent, 'id')->willReturn('adam_id'); $propertyAccessor->getValue($parent, 'name')->willReturn('Adam'); //set $propertyAccessor->setValue($modelDto, 'a', [$userDTO])->shouldBeCalled(); $propertyAccessor->setValue($userDTO, 'id', 'abel_id')->shouldBeCalled(); $propertyAccessor->setValue($userDTO, 'name', UnknownValue::create())->shouldBeCalled(); $propertyAccessor->setValue($userDTO, 'parent', $parentDTO)->shouldBeCalled(); $propertyAccessor->setValue($parentDTO, 'id', 'adam_id')->shouldBeCalled(); $propertyAccessor->setValue($parentDTO, 'name', 'Adam')->shouldBeCalled(); //configurator calls $mappingConfigurator->isPropertyVisible(['a'])->shouldBeCalled(); $this->map($model)->shouldReturn($modelDto); }