Skip to content
This repository was archived by the owner on Jan 1, 2020. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 deletions src/Midgard/CreatePHP/Metadata/RdfTypeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ class RdfTypeFactory
/**
* @var RdfMapperInterface
*/
private $mapper;
private $defaultMapper;

/**
* @var RdfMapperInterface[]
*/
private $mappers;

/**
* @var RdfDriverInterface
Expand All @@ -33,18 +38,21 @@ class RdfTypeFactory
private $loadedTypes = array();

/**
* @param RdfMapperInterface $mapper the mapper to use in this project
* @param RdfMapperInterface $defaultMapper the default mapper to use if there is no specific one
* @param RdfDriverInterface $driver the driver to load types from
* @param RdfMapperInterface[] $mappers rdf mappers per type name
*/
public function __construct(RdfMapperInterface $mapper, RdfDriverInterface $driver)
public function __construct(RdfMapperInterface $defaultMapper, RdfDriverInterface $driver, $mappers = array())
{
$this->mapper = $mapper;
$this->defaultMapper = $defaultMapper;
$this->driver = $driver;
$this->mappers = $mappers;
}

public function getTypeByObject($object)
{
return $this->getTypeByName(
$this->driver->objectToName($object, $this->mapper)
$this->driver->objectToName($object, $this->defaultMapper)
);
}

Expand All @@ -58,14 +66,27 @@ public function getTypeByObject($object)
public function getTypeByName($name)
{
if (!isset($this->loadedTypes[$name])) {
$this->loadedTypes[$name] = $this->driver->loadType($name, $this->mapper, $this);
$mapper = $this->getMapper($name);
$this->loadedTypes[$name] = $this->driver->loadType($name, $mapper, $this);
}

// TODO: combine types from parent models...

return $this->loadedTypes[$name];
}

/**
* Get the mapper for type $name, or the defaultMapper if there is no specific mapper.
*
* @param string $name the type name for which to get the mapper
*
* @return RdfMapperInterface
*/
protected function getMapper($name)
{
return isset($this->mappers[$name]) ? $this->mappers[$name] : $this->defaultMapper;
}

/**
* Get the type information by (full) RDF name
*
Expand Down
23 changes: 8 additions & 15 deletions src/Midgard/CreatePHP/RestService.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,6 @@ class RestService
const HTTP_PUT = 'put';
const HTTP_DELETE = 'delete';

/**
* The mapper object
*
* @var RdfMapperInterface
*/
protected $_mapper;

/**
* Custom workflows for post, put, delete or get
*
Expand Down Expand Up @@ -132,7 +125,7 @@ public function run($data, TypeInterface $type, $subject = null, $method = null)
$subject = $_GET["subject"];
}
// TODO: workflows should expect subject rather than instance
$object = $this->_mapper->getBySubject($subject);
$object = $type->getMapper()->getBySubject($subject);
return $this->_workflows[$method]->run($object);
}

Expand Down Expand Up @@ -170,12 +163,12 @@ private function _handleCreate($received_data, TypeInterface $type)
$about = $received_data[$this->jsonldEncode($rdf)];

if (! empty($about)) {
$parent = $this->_mapper->getBySubject($this->jsonldDecode(current($about)));
$parent = $type->getMapper()->getBySubject($this->jsonldDecode(current($about)));
break;
}
}

$object = $this->_mapper->prepareObject($type, $parent);
$object = $type->getMapper()->prepareObject($type, $parent);
$entity = $type->createWithObject($object);

return $this->_storeData($received_data, $entity);
Expand All @@ -189,7 +182,7 @@ private function _handleUpdate($data, TypeInterface $type, $subject = null)
if (null === $subject) {
$subject = $this->jsonldDecode($data['@subject']);
}
$object = $this->_mapper->getBySubject($subject);
$object = $type->getMapper()->getBySubject($subject);
$entity = $type->createWithObject($object);
return $this->_storeData($data, $entity);
}
Expand All @@ -208,11 +201,11 @@ private function _storeData($new_values, EntityInterface $entity)
$expanded_name = $this->_expandPropertyName($rdf_name, $entity);

if (array_key_exists($expanded_name, $new_values)) {
$object = $this->_mapper->setPropertyValue($object, $node, $new_values[$expanded_name]);
$object = $entity->getMapper()->setPropertyValue($object, $node, $new_values[$expanded_name]);
}
}

if ($this->_mapper->store($entity))
if ($entity->getMapper()->store($entity))
{

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i guess this CS issue was there already .. but the opening parenthesis should be on the previous line

return $this->_convertToJsonld($new_values, $object, $entity);
}
Expand All @@ -225,7 +218,7 @@ private function _convertToJsonld($data, $object, EntityInterface $entity)
// lazy: copy stuff from the sent json-ld to not have to rebuild everything.
$jsonld = $data;

$jsonld['@subject'] = $this->jsonldEncode($this->_mapper->createSubject($object));
$jsonld['@subject'] = $this->jsonldEncode($entity->getMapper()->createSubject($object));
foreach ($entity->getChildDefinitions() as $node) {
if (!$node instanceof PropertyInterface) {
continue;
Expand All @@ -236,7 +229,7 @@ private function _convertToJsonld($data, $object, EntityInterface $entity)
$expanded_name = $this->_expandPropertyName($rdf_name, $entity);

if (array_key_exists($expanded_name, $jsonld)) {
$jsonld[$expanded_name] = $this->_mapper->getPropertyValue($object, $node);
$jsonld[$expanded_name] = $entity->getMapper()->getPropertyValue($object, $node);
}
}

Expand Down
8 changes: 8 additions & 0 deletions tests/Test/Midgard/CreatePHP/RestServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ public function setUp()
->method('getChildDefinitions')
->will($this->returnValue(array('title' => $this->property, 'children' => $this->collection)))
;
$this->type->expects($this->any())
->method('getMapper')
->will($this->returnValue($this->mapper))
;

$this->child_type->expects($this->any())
->method('getVocabularies')
Expand All @@ -98,6 +102,10 @@ public function setUp()
->method('getVocabularies')
->will($this->returnValue(array('dcterms' => 'http://purl.org/dc/terms/')))
;
$this->entity->expects($this->any())
->method('getMapper')
->will($this->returnValue($this->mapper))
;
$this->property->expects($this->any())
->method('getProperty')
->will($this->returnValue('dcterms:title'))
Expand Down