Skip to content

Commit

Permalink
Add documentation and interface for marshalling behaviors.
Browse files Browse the repository at this point in the history
- Fix rebase mistake.
- Add missing docs
- Add interface.
  • Loading branch information
markstory committed Aug 7, 2016
1 parent ba06adb commit 4de2acb
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 6 deletions.
12 changes: 10 additions & 2 deletions src/ORM/Behavior/TranslateBehavior.php
Expand Up @@ -22,6 +22,7 @@
use Cake\ORM\Behavior;
use Cake\ORM\Entity;
use Cake\ORM\Locator\LocatorAwareTrait;
use Cake\ORM\MarshalParticipantInterface;
use Cake\ORM\Query;
use Cake\ORM\Table;
use Cake\Utility\Inflector;
Expand All @@ -38,7 +39,7 @@
* If you want to bring all or certain languages for each of the fetched records,
* you can use the custom `translations` finders that is exposed to the table.
*/
class TranslateBehavior extends Behavior
class TranslateBehavior extends Behavior implements MarshalParticipantInterface
{

use LocatorAwareTrait;
Expand Down Expand Up @@ -327,7 +328,14 @@ public function afterSave(Event $event, EntityInterface $entity)
$entity->unsetProperty('_i18n');
}

public function marshalPropertyMap($marshaller, $map, $options)
/**
* Add in _translations marshalling handlers if translation marshalling is
* enabled. You need to specifically enable translation marshalling by adding
* `'translations' => true` to the options provided to `Table::newEntity()` or `Table::patchEntity()`.
*
* {@inheritDoc}
*/
public function buildMarhshalMap($marshaller, $map, $options)
{
if (isset($options['translations']) && !$options['translations']) {
return [];
Expand Down
34 changes: 34 additions & 0 deletions src/ORM/MarshalParticipantInterface.php
@@ -0,0 +1,34 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 3.4.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\ORM;

/**
* Behaviors implementing this interface can participate in entity marshalling.
*
* This enables behaviors to define behavior for how the properties they provide/manage
* should be marshalled.
*/
interface MarshalParticipantInterface
{
/**
* Build a set of properties that should be included in the marshalling process.
*
* @param \Cake\ORM\Marhshaller $marshaller The marhshaller of the table the behavior is attached to.
* @param array $map The property map being built.
* @param array $options The options array used in the marshalling call.
* @return array A map of `[property => callable]` of additional properties to marshal.
*/
public function buildMarhshalMap($marshaller, $map, $options);
}
8 changes: 4 additions & 4 deletions src/ORM/Marshaller.php
Expand Up @@ -20,6 +20,7 @@
use Cake\Database\Type;
use Cake\Datasource\EntityInterface;
use Cake\Datasource\InvalidPropertyInterface;
use Cake\ORM\MarshalParticipantInterface;
use RuntimeException;

/**
Expand Down Expand Up @@ -88,7 +89,7 @@ protected function _buildPropertyMap($data, $options)
$assoc = $this->_table->association($key);
// If the key is not a special field like _ids or _joinData
// it is a missing association that we should error on.
if (!$assoc)
if (!$assoc) {
if (substr($key, 0, 1) !== '_') {
throw new \InvalidArgumentException(sprintf(
'Cannot marshal data for "%s" association. It is not associated with "%s".',
Expand Down Expand Up @@ -117,9 +118,8 @@ protected function _buildPropertyMap($data, $options)
$behaviors = $this->_table->behaviors();
foreach ($behaviors->loaded() as $name) {
$behavior = $behaviors->get($name);
// TODO use an interface here.
if (method_exists($behavior, 'marshalPropertyMap')) {
$map = $behavior->marshalPropertyMap($this, $map, $options);
if ($behavior instanceof MarshalParticipantInterface) {
$map = $behavior->buildMarhshalMap($this, $map, $options);
}
}

Expand Down

0 comments on commit 4de2acb

Please sign in to comment.