Skip to content

Commit

Permalink
Added Model.beforeMarshal event.
Browse files Browse the repository at this point in the history
  • Loading branch information
robertpustulka committed Jan 16, 2015
1 parent c3d23f4 commit fd554a6
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 10 deletions.
35 changes: 25 additions & 10 deletions src/ORM/Marshaller.php
Expand Up @@ -104,21 +104,18 @@ public function one(array $data, array $options = [])
$propertyMap = $this->_buildPropertyMap($options);

$schema = $this->_table->schema();
$tableName = $this->_table->alias();
$entityClass = $this->_table->entityClass();
$entity = new $entityClass();
$entity->source($this->_table->alias());

if (isset($data[$tableName])) {
$data = $data[$tableName];
}

if (isset($options['accessibleFields'])) {
foreach ((array)$options['accessibleFields'] as $key => $value) {
$entity->accessible($key, $value);
}
}

$data = $this->_prepareData($data, $options);

$errors = $this->_validate($data, $options, true);
$primaryKey = $schema->primaryKey();
$properties = [];
Expand Down Expand Up @@ -185,6 +182,27 @@ protected function _validate($data, $options, $isNew)
return $options['validate']->errors($data, $isNew);
}

/**
* Returns data prepared to validate and marshall.
*
* @param array $data The data to prepare.
* @param array $options The options passed to this marshaller.
* @return array Prepared data.
*/
protected function _prepareData($data, $options)
{
$tableName = $this->_table->alias();

if (isset($data[$tableName])) {
$data = $data[$tableName];
}

$dataObject = new \ArrayObject($data);
$this->_table->dispatchEvent('Model.beforeMarshal', compact('dataObject', 'options'));

return (array)$dataObject;
}

/**
* Create a new sub-marshaller and marshal the associated data.
*
Expand Down Expand Up @@ -326,18 +344,15 @@ public function merge(EntityInterface $entity, array $data, array $options = [])
{
$options += ['validate' => true];
$propertyMap = $this->_buildPropertyMap($options);
$tableName = $this->_table->alias();
$isNew = $entity->isNew();
$keys = [];

if (isset($data[$tableName])) {
$data = $data[$tableName];
}

if (!$isNew) {
$keys = $entity->extract((array)$this->_table->primaryKey());
}

$data = $this->_prepareData($data, $options);

$errors = $this->_validate($data + $keys, $options, $isNew);
$schema = $this->_table->schema();
$properties = [];
Expand Down
23 changes: 23 additions & 0 deletions tests/TestCase/ORM/MarshallerTest.php
Expand Up @@ -1745,4 +1745,27 @@ public function testMergeWithCreate()
$this->assertNotEmpty($result->errors('author_id'));
$this->assertNotEmpty($result->errors('thing'));
}

/**
* Test Model.beforeMarshal event.
*
* @return void
*/
public function testBeforeMarshalEvent()
{
$data = [
'title' => 'My title',
'body' => 'My content'
];

$marshall = new Marshaller($this->articles);

$this->articles->eventManager()->attach(function ($e, $data) {
$data['title'] = 'Modified title';
}, 'Model.beforeMarshal');

$entity = $marshall->one($data);

$this->assertEquals('Modified title', $entity->title);
}
}

0 comments on commit fd554a6

Please sign in to comment.