Example #1
0
 /**
  * "Decode" PHPCR property to MongoDB property
  *
  * @param $property
  * @return array|null
  *
  * @throws \Exception
  */
 private function decodeProperty(PropertyInterface $property)
 {
     $path = $property->getPath();
     $path = $this->validatePath($path);
     $name = explode('/', $path);
     $name = end($name);
     if ($name == 'jcr:uuid' || $name == 'jcr:primaryType') {
         return null;
     }
     if (!$property->isModified() && !$property->isNew()) {
         return null;
     }
     $propType = $property->getType();
     $propNode = $property->getNode();
     if (($propType == PropertyType::REFERENCE || $propType == PropertyType::WEAKREFERENCE) && ($propNode instanceof NodeInterface && !$propNode->isNodeType('mix:referenceable'))) {
         throw new ValueFormatException('Node ' . $property->getNode()->getPath() . ' is not referenceable.');
     }
     $isMultiple = $property->isMultiple();
     $typeId = $property->getType();
     $type = PropertyType::nameFromValue($typeId);
     $data = array('multi' => $isMultiple, 'name' => $property->getName(), 'type' => $type);
     $binaryData = null;
     switch ($typeId) {
         case PropertyType::NAME:
         case PropertyType::URI:
         case PropertyType::WEAKREFERENCE:
         case PropertyType::REFERENCE:
         case PropertyType::PATH:
             $values = $property->getString();
             break;
         case PropertyType::DECIMAL:
             $values = $property->getDecimal();
             break;
         case PropertyType::STRING:
             $values = $property->getString();
             break;
         case PropertyType::BOOLEAN:
             $values = $property->getBoolean();
             break;
         case PropertyType::LONG:
             $values = $property->getLong();
             break;
         case PropertyType::BINARY:
             if ($property->isMultiple()) {
                 foreach ((array) $property->getBinary() as $binary) {
                     $binary = stream_get_contents($binary);
                     $binaryData[] = $binary;
                     $values[] = strlen($binary);
                 }
             } else {
                 $binary = stream_get_contents($property->getBinary());
                 $binaryData[] = $binary;
                 $values = strlen($binary);
             }
             break;
         case PropertyType::DATE:
             if ($property->isMultiple()) {
                 $dates = $property->getDate() ?: new \DateTime('now');
                 foreach ((array) $dates as $date) {
                     $value = array('date' => new \MongoDate($date->getTimestamp()), 'timezone' => $date->getTimezone()->getName());
                     $values[] = $value;
                 }
             } else {
                 $date = $property->getDate() ?: new \DateTime('now');
                 $values = array('date' => new \MongoDate($date->getTimestamp()), 'timezone' => $date->getTimezone()->getName());
             }
             break;
         case PropertyType::DOUBLE:
             $values = $property->getDouble();
             break;
     }
     if ($isMultiple) {
         $data['value'] = array();
         foreach ((array) $values as $value) {
             $this->assertValidPropertyValue($data['type'], $value, $path);
             $data['value'][] = $value;
         }
     } else {
         $this->assertValidPropertyValue($data['type'], $values, $path);
         $data['value'] = $values;
     }
     if ($binaryData) {
         try {
             foreach ($binaryData as $idx => $binary) {
                 $grid = $this->db->getGridFS();
                 $grid->getMongoCollection()->storeBytes($binary, array('path' => $path, 'w_id' => $this->workspaceId, 'idx' => $idx));
             }
         } catch (\Exception $e) {
             throw $e;
         }
     }
     return $data;
 }