Example #1
0
 /**
  * Get's the possible key used in the typemap for the type
  *
  * @param TypeInterface $type
  * @return string
  */
 protected function getTypeMapKey(TypeInterface $type)
 {
     list($prefix, $shortname) = explode(':', $type->getRdfType());
     $ns = $type->getVocabularies();
     $ns = $ns[$prefix];
     return $ns . $shortname;
 }
Example #2
0
 public function prepareObject(TypeInterface $controller, $parent = null)
 {
     $config = $controller->getConfig();
     $class = $config['storage'];
     $object = new $class();
     if (null !== $parent) {
         $baseclass = \midcom_helper_reflector::resolve_baseclass($class);
         $reflector = new \midgard_reflection_property($baseclass);
         $up_property = \midgard_object_class::get_property_up($baseclass);
         if (!empty($up_property)) {
             $target_property = $reflector->get_link_target($up_property);
             $target_class = $reflector->get_link_name($up_property);
             if (\midcom_helper_reflector::resolve_baseclass($parent) === $target_class) {
                 $object->{$up_property} = $parent->{$target_property};
             }
         }
         $parent_property = \midgard_object_class::get_property_parent($baseclass);
         if (!empty($parent_property)) {
             $target_property = $reflector->get_link_target($parent_property);
             $target_class = $reflector->get_link_name($parent_property);
             if (\midcom_helper_reflector::resolve_baseclass($parent) === $target_class) {
                 $object->{$parent_property} = $parent->{$target_property};
             } else {
                 $object->{$parent_property} = $parent->{$parent_property};
             }
         }
     }
     return $object;
 }
 /**
  * {@inheritDoc}
  *
  * Create the object if a class is defined in the typeMap. This class
  * can not know how to set the parent, so if you ever create collection
  * entries, your extending class should handle the parent - it can still
  * call this method to create the basic object, just omit the parent
  * parameter and then set the parent on the returned value. For an example,
  * see DoctrinePhpcrOdmMapper.
  *
  * Just overwrite if you use a different concept.
  */
 public function prepareObject(TypeInterface $type, $parent = null)
 {
     if ($parent !== null) {
         throw new \Exception('Parent is not null, please extend this method to configure the parent');
     }
     list($prefix, $shortname) = explode(':', $type->getRdfType());
     $ns = $type->getVocabularies();
     $ns = $ns[$prefix];
     $name = $ns.$shortname;
     if (isset($this->typeMap[$name])) {
         $class = $this->typeMap[$name];
         return new $class;
     }
     throw new \Exception('No information on ' . $name);
 }
Example #4
0
 public function test_render()
 {
     $collection = new Collection('test', $this->mockTypeFactory, array('is_child' => true));
     $collection->addTypeName('test');
     $collection->setRev('test:rev');
     $parent = array('id' => 'parent_id', 'children' => array(array('id' => 'child1')));
     $this->parentController->test = $collection;
     $entity = $this->parentController->createWithObject($parent);
     $this->assertEquals("<div xmlns:test=\"http://test.org/\" about=\"parent_id\"><div rev=\"test:rev\"><div about=\"child1\"></div>\n</div>\n</div>\n", $entity->render());
 }
Example #5
0
 /**
  * Expand a property name to use full namespace instead of short name,
  * as used in reference fields. Additionally jsonld-encodes that link.
  *
  * @param string $name the name to expand, including namespace
  * @param TypeInterface $type the type context for the vocabulary
  *
  * @return string the jsonld-encoded expanded name
  *
  * @throws \RuntimeException if the prefix is not in the vocabulary of
  *      $type
  */
 private function _expandPropertyName($name, TypeInterface $type)
 {
     $parts = explode(":", $name);
     $vocabularies = $type->getVocabularies();
     if (!isset($vocabularies[$parts[0]])) {
         throw new \RuntimeException('Undefined namespace prefix \'' . $parts[0] . "' in '{$name}'");
     }
     return $this->jsonldEncode($vocabularies[$parts[0]] . $parts[1]);
 }
Example #6
0
 /**
  * Build the attributes from the property|rel field and any custom attributes
  *
  * @param mixed $child the child element to parse
  * @param \ArrayAccess $childData the child to read field from
  * @param string $field the field to be read, property for properties, rel for collections
  * @param string $identifier to be used in case there is no property field in $child
  * @param TypeInterface $parentType the parent object, i.e. for namespaces
  * @param boolean $add_default_vocabulary flag to tell whether to add vocabulary for
  *      the default namespace.
  *
  * @return array properties
  */
 protected function parseChild($child, $childData, $identifier, TypeInterface $parentType, &$add_default_vocabulary)
 {
     if ($child instanceof PropertyDefinitionInterface) {
         /** @var $child PropertyDefinitionInterface */
         $child->setProperty($this->buildInformation($childData, $identifier, 'property', $add_default_vocabulary));
     } elseif ($child instanceof CollectionDefinitionInterface) {
         /** @var $child CollectionDefinitionInterface */
         $child->setRel($this->buildInformation($childData, $identifier, 'rel', $add_default_vocabulary));
         $child->setRev($this->buildInformation($childData, $identifier, 'rev', $add_default_vocabulary));
         if (isset($childData['childtypes'])) {
             foreach ($childData['childtypes'] as $type) {
                 $expanded = NamespaceHelper::expandNamespace($type, $parentType->getVocabularies());
                 $child->addTypeName($expanded);
             }
         }
     }
     if ($child instanceof NodeInterface) {
         $this->parseNodeInfo($child, $childData);
     }
 }