예제 #1
0
 /**
  * @param AttributeTypeInterface|OptionAttributeTypeInterface $type
  * @param array $expected
  * @param Attribute|null $attribute
  * @param array $normalizationData
  * @dataProvider attributeTypeDataProvider
  */
 public function testAttributeTypes(AttributeTypeInterface $type, array $expected, Attribute $attribute = null, array $normalizationData = [])
 {
     if (!$attribute) {
         $attribute = new Attribute();
     }
     $this->assertEquals($expected['name'], $type->getName());
     $this->assertEquals($expected['typeField'], $type->getDataTypeField());
     $this->assertEquals($expected['isContainHtml'], $type->isContainHtml());
     $this->assertEquals($expected['isUsedForSearch'], $type->isUsedForSearch());
     $this->assertEquals($expected['isUsedInFilters'], $type->isUsedInFilters());
     $this->assertEquals($expected['formParameters'], $type->getFormParameters($attribute));
     $this->assertEquals($expected['requiredConstraints'], $type->getRequiredConstraints());
     $this->assertEquals($expected['optionalConstraints'], $type->getOptionalConstraints());
     $this->assertEquals($expected['canBeUnique'], $type->canBeUnique());
     $this->assertEquals($expected['canBeRequired'], $type->canBeRequired());
     if (isset($expected['defaultFormParameters'])) {
         $this->assertEquals($expected['defaultFormParameters'], $type->getDefaultValueFormParameters($attribute));
     }
     $testValue = 'test';
     if (!empty($normalizationData['normalize'])) {
         foreach ($normalizationData['normalize'] as $value) {
             $this->assertSame($value['to'], $type->normalize($value['from']));
         }
     } else {
         $this->assertSame($testValue, $type->normalize($testValue));
     }
     if (!empty($normalizationData['denormalize'])) {
         foreach ($normalizationData['denormalize'] as $value) {
             $this->assertSame($value['to'], $type->denormalize($value['from']));
         }
     } else {
         $this->assertSame($testValue, $type->denormalize($testValue));
     }
 }
 /**
  * @param Attribute $attribute
  * @param AttributeTypeInterface $attributeType
  * @return mixed
  */
 public function getDefaultValue(Attribute $attribute, AttributeTypeInterface $attributeType)
 {
     $dataField = $attributeType->getDataTypeField();
     if ($attribute->isLocalized()) {
         $result = [];
         foreach ($attribute->getDefaultValues() as $defaultValue) {
             $localeId = $defaultValue->getLocale() ? $defaultValue->getLocale()->getId() : null;
             $fallback = $defaultValue->getFallback();
             if ($fallback) {
                 $result[$localeId] = new FallbackType($fallback);
             } else {
                 $result[$localeId] = $attributeType->normalize($this->getDefaultValueByField($defaultValue, $dataField));
             }
         }
     } else {
         $defaultValue = $attribute->getDefaultValueByLocaleId(null);
         $result = $attributeType->normalize($this->getDefaultValueByField($defaultValue, $dataField));
     }
     return $result;
 }