Skip to content

Commit

Permalink
embeds properties
Browse files Browse the repository at this point in the history
  • Loading branch information
dracony committed Apr 9, 2014
1 parent 0c61056 commit c5c0e86
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 59 deletions.
5 changes: 0 additions & 5 deletions src/PHPixie/ORM/Relationship/Type/Property.php
Expand Up @@ -13,10 +13,5 @@ public function __construct($handler, $side)
$this->side = $side;
}

public function query()
{
return $this->handler->query($this->side, $this->propertyOwner());
}

abstract protected function propertyOwner();
}
29 changes: 25 additions & 4 deletions src/PHPixie/ORM/Relationship/Types/Embeds/Handler.php
Expand Up @@ -4,12 +4,33 @@

class Handler extends \PHPixie\ORM\Relationship\Type\Handler
{
public function setItemsOwner($embedConfig, $owner, $item)
public function setOwnerProperty($embedConfig, $item, $owner)
{
$ownerPropertyName = $embedConfig->ownerProperty;
if($item->$ownerPropertyName() !==
$itemPropertyName = $embedConfig->ownerProperty;
$ownerPropertyName = $embedConfig->name;

$itemProperty = $item->$itemPropertyName;
$oldOwner = $itemProperty->value();

if ($oldOwner !== null) {
$oldOwnerProperty = $oldOwner->$ownerPropertyName;
if ($oldOwnerProperty instanceof Embeds\Property\Model\EmbeddedArray) {
$oldOwner->$ownerPropertyName->remove($item);
}else {
$this->unsetOwnerProperty($embedConfig, $item);
}
}

$itemProperty->setValue($owner);
}


public function unsetOwnerProperty($embedConfig, $item)
{
$itemPropertyName = $embedConfig->ownerProperty;
$item->$itemPropertyName->setValue(null);
}


public function mapRelationship($side, $group, $query, $plan)
{
$config = $side->config();
Expand Down
17 changes: 7 additions & 10 deletions src/PHPixie/ORM/Relationship/Types/Embeds/Property/Model.php
Expand Up @@ -5,14 +5,11 @@
class Embedded extends \PHPixie\ORM\Properties\Property\Model
{
protected $handler;
protected $path;
protected $embedConfig;

public function __construct($handler, $side, $model, $embedConfig)
{
parent::construct($handler, $side, $model);
$this->embedConfig = $embedConfig;
}


protected $embedConfig;

public function __construct($handler, $side, $model, $embedConfig)
{
parent::construct($handler, null, $model);
$this->embedConfig = $embedConfig;
}
}
Expand Up @@ -32,18 +32,22 @@ public function offsetGet($key)
public function offsetSet($key, $embeddedModel)
{
$this->handler->arraySetEmbedded($this->model, $this->embedConfig, $key, $embeddedModel);
$this->handler->setOwnerProperty($this->embedConfig, $embeddedModel, $this->model);
$this->models[$key] = $embeddedModel;
}

public function offsetUnset($key)
{
$this->handler->arrayUnsetEmbedded($this->model, $this->embedConfig, $key);
$this->handler->unsetOwnerProperty($this->embedConfig, $this->models[$key], null);
unset($this->models[$key]);
}

public function add($key = null)
{
$embeddedModel = $this->handler->arrayAddEmbedded($this->model, $this->embedConfig, $key);
$this->handler->setOwnerProperty($this->embedConfig, $embeddedModel, $this->model);

if($key === null){
$this->models[$key] = $embeddedModel;
}else {
Expand All @@ -52,6 +56,20 @@ public function add($key = null)
return $embeddedModel;
}

public function remove($models)
{
if (!is_array($models))
$models = array($models);

while (!empty($models)) {
$model = array_pop($models);
$key = array_search($model, $this->models, true);
if ($id === false)
throw new \PHPixie\ORM\Exception\Model("The model to be removed was not found.");
$this->offsetUnset($key);
}
}

public function count()
{
return $this->handler->arrayCountEmbedded($this->model, $this->embedConfig);
Expand Down
Expand Up @@ -17,23 +17,26 @@ public function __invoke($createMissing = false)

protected function load()
{
return $this->handler->getEmbedded($this->model, $this->embedConfig);
return $this->handler->getEmbedded($this->embedConfig, $this->model);
}

public function create()
{
$this->loaded = true;
return $this->handler->createEmbedded($this->model, $this->embedConfig);
$this->value = $this->handler->createEmbeddedModel($this->embedConfig, $this->model);
$this->handler->setOwnerProperty($this->embedConfig, $this->value, null);
}

public function remove()
{
return $this->handler->removeEmbedded($this->model, $this->embedConfig);
$this->handler->removeEmbeddedModel($this->embedConfig, $this->model);
$this->handler->setOwnerProperty($this->embedConfig, $this->value);
}

public function set($model)
{
$this->loaded = true;
return $this->handler->setEmbedded($this->model, $this->embedConfig, $model);
return $this->handler->setEmbeddedModel($this->embedConfig, $this->model, $model);
$this->handler->setOwnerProperty($this->embedConfig, $model, $this->model);
}
}
24 changes: 10 additions & 14 deletions src/PHPixie/ORM/Relationship/Types/Embeds/Property/Model/Owner.php
Expand Up @@ -4,19 +4,15 @@

class Owner extends PHPixie\ORM\Relationship\Types\Embeds\Property\Model
{
public function __invoke($createMissing = false)
{
return $this->value;
}

protected function load()
{
}

public function set($owner)
{

$this->handler->setEmbedded($this->embedConfig, $owner, $this->model);

}
public function load()
{
throw new \PHPixie\ORM\Exception\Model("Owner property can not be loaded, only set");
}

public function set($owner)
{
$this->handler->embedModel($this->embedConfig, $owner, $this->model);
$this->handler->setOwnerProperty($this->embedConfig, $this->model, $owner);
}
}
6 changes: 3 additions & 3 deletions src/PHPixie/ORM/Relationship/Types/Embeds/Side.php
Expand Up @@ -7,16 +7,16 @@ class Side extends PHPixie\ORM\Relationship\Side

protected $propertyName;

public function __construct($relationship, $propertyName, $config)
public function __construct($propertyName, $config)
{
parent::__construct($relationship, 'embeds', $config);
parent::__construct('embeds', $config);
$this->propertyName = $propertyName;

}

public function modelName()
{
return $this->config->model;
return $this->config->modelName;
}

public function propertyName()
Expand Down
8 changes: 4 additions & 4 deletions src/PHPixie/ORM/Relationship/Types/Embeds/Side/Embedded.php
Expand Up @@ -11,13 +11,13 @@ public function __construct($inflector)
$this->inflector = $inflector;
}

public function map($config)
public function map($config, $defaultOwnerProperty)
{
return new Embedded\Map($this, $config);
return new Embedded\Map($this, $config, $defaultOwnerProperty);
}

public function config($propertyName, $config)
public function config($propertyName, $config, $defaultOwnerProperty)
{
return new Embedded\Config($this, $this->inflector, $propertyName, $config);
return new Embedded\Config($this, $this->inflector, $propertyName, $config, $defaultOwnerProperty);
}
}
27 changes: 13 additions & 14 deletions src/PHPixie/ORM/Relationship/Types/Embeds/Side/Embedded/Config.php
Expand Up @@ -5,30 +5,29 @@
class Config {

public $name;
public $type;
public $ownerProperty;
public $type;
public $ownerProperty;
public $modelName;
public $map;

public function __construct($embeddedConfig, $inflector, $name, $defaultOwnerProperty, $config)
public function __construct($embedded, $inflector, $name, $config, $defaultOwnerProperty)
{
$this->name = $name;
$this->ownerProperty = $config->get('owner_property', $defaultOwnerProperty);

$this->type = $config->get('type', 'document');
$this->ownerProperty = $config->get('ownerProperty', $defaultOwnerProperty);

if ($this->type !== 'document' && $this->type !== 'array')
throw new \PHPixie\ORM\Exception\Mapper("Embed type must be either 'document' or 'array'");
$this->type = $config->get('type', 'one');

if(($modelName = $config->get('model')) === null) {
if($this->type === 'document'){
$modelName = $propertyName;
if ($this->type !== 'one' && $this->type !== 'many')
throw new \PHPixie\ORM\Exception\Mapper("Embed type must be either 'one' or 'many'");

if(($this->modelName = $config->get('model')) === null) {
if($this->type === 'one'){
$this->modelName = $name;
}else{
$modelName = $inflector->singular($propertyName);
$this->modelName = $inflector->singular($name);
}
}

$this->modelName = $modelName;
$this->map = $embeddedConfig->map($config->slice('embeds'), $this->name);
$this->map = $embedded->map($config->slice('embeds'), $this->name);
}
}
Expand Up @@ -11,7 +11,7 @@ public function __construct($embedded, $config, $defaultOwnerProperty)
$properties = array_keys($config->get(null, array()));

foreach($properties as $property) {
$this->map[$property] = $embedded->config($config->slice($property), $property, $defaultOwnerProperty);
$this->map[$property] = $embedded->config($property, $config->slice($property), $defaultOwnerProperty);
}
}
}

0 comments on commit c5c0e86

Please sign in to comment.