Exemplo n.º 1
0
 /**
  * Assigns all properties to object. Could be a document or a (nested) object.
  *
  * @param array  $array
  * @param object $object
  * @param array  $propertiesMetadata
  *
  * @return object
  */
 public function assignArrayToObject(array $array, $object, array $propertiesMetadata)
 {
     foreach ($propertiesMetadata as $esField => $propertyMetadata) {
         // Skip fields from the mapping that have no value set, unless they are multilanguage fields
         if (empty($propertyMetadata['multilanguage']) && !isset($array[$esField])) {
             continue;
         }
         if ($propertyMetadata['type'] === 'string' && !empty($propertyMetadata['multilanguage'])) {
             $objectValue = new MLProperty();
             foreach ($array as $fieldName => $value) {
                 $prefixLength = strlen($esField . $this->languageSeparator);
                 if (substr($fieldName, 0, $prefixLength) === $esField . $this->languageSeparator) {
                     $language = substr($fieldName, $prefixLength);
                     $objectValue->setValue($value, $language);
                 }
             }
         } elseif ($propertyMetadata['type'] === 'date') {
             $objectValue = \DateTime::createFromFormat(isset($propertyMetadata['format']) ? $propertyMetadata['format'] : \DateTime::ISO8601, $array[$esField]) ?: $array[$esField];
         } elseif (in_array($propertyMetadata['type'], ['object', 'nested'])) {
             if ($propertyMetadata['multiple']) {
                 $objectValue = new ObjectIterator($this, $array[$esField], $propertyMetadata);
             } else {
                 $objectValue = $this->assignArrayToObject($array[$esField], new $propertyMetadata['className'](), $propertyMetadata['propertiesMetadata']);
             }
         } else {
             $objectValue = $array[$esField];
         }
         $this->getPropertyAccessor()->setValue($object, $propertyMetadata['propertyName'], $objectValue);
     }
     return $object;
 }
 /**
  * Assigns all properties to object.
  *
  * @param array           $array              Flat array with fields and their value
  * @param ObjectInterface $object             A document or a (nested) object
  * @param array           $propertiesMetadata
  *
  * @return ObjectInterface
  */
 public function assignArrayToObject(array $array, ObjectInterface $object, array $propertiesMetadata)
 {
     foreach ($propertiesMetadata as $esField => $propertyMetadata) {
         // Skip fields from the mapping that have no value set, unless they are multilanguage fields
         if (empty($propertyMetadata['multilanguage']) && !isset($array[$esField])) {
             continue;
         }
         if ($propertyMetadata['type'] === 'string' && !empty($propertyMetadata['multilanguage'])) {
             $objectValue = null;
             foreach ($array as $fieldName => $value) {
                 $prefixLength = strlen($esField . $this->languageSeparator);
                 if (substr($fieldName, 0, $prefixLength) === $esField . $this->languageSeparator) {
                     if (!$objectValue) {
                         $objectValue = new MLProperty();
                     }
                     $language = substr($fieldName, $prefixLength);
                     $objectValue->setValue($value, $language);
                 }
             }
         } elseif (in_array($propertyMetadata['type'], ['object', 'nested'])) {
             if ($propertyMetadata['multiple']) {
                 $objectValue = new ObjectIterator($this, $array[$esField], $propertyMetadata);
             } else {
                 $objectValue = $this->assignArrayToObject($array[$esField], new $propertyMetadata['className'](), $propertyMetadata['propertiesMetadata']);
             }
         } else {
             $objectValue = $array[$esField];
         }
         if ($propertyMetadata['propertyAccess'] == DocumentMetadata::PROPERTY_ACCESS_PRIVATE) {
             $object->{$propertyMetadata['methods']['setter']}($objectValue);
         } else {
             $object->{$propertyMetadata['propertyName']} = $objectValue;
         }
     }
     return $object;
 }
 /**
  * Tests if construct set all values properly
  */
 public function testConstruct()
 {
     $mlProperty = new MLProperty(['default' => 'test default', 'en' => 'test en', 'bg' => 'test bg']);
     $this->assertEquals(['default' => 'test default', 'en' => 'test en', 'bg' => 'test bg'], $mlProperty->getValues(), 'MLProperty construct does not set all values correctly.');
 }