Skip to content

Commit

Permalink
Merge e2e7b37 into b69022b
Browse files Browse the repository at this point in the history
  • Loading branch information
albertborsos committed Jun 28, 2019
2 parents b69022b + e2e7b37 commit f0e097c
Show file tree
Hide file tree
Showing 4 changed files with 188 additions and 2 deletions.
12 changes: 12 additions & 0 deletions src/interfaces/RepositoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace albertborsos\ddd\interfaces;

interface RepositoryInterface
{
public static function findOne($condition);

public static function findAll($condition);

public function save(BusinessObject $model, $runValidation = true, $attributeNames = null);
}
100 changes: 100 additions & 0 deletions src/models/AbstractRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

namespace albertborsos\ddd\models;

use albertborsos\ddd\interfaces\BusinessObject;
use albertborsos\ddd\interfaces\RepositoryInterface;
use yii\base\Component;
use yii\base\Exception;
use yii\db\ActiveQueryInterface;
use yii\db\ActiveRecordInterface;

abstract class AbstractRepository extends Component implements RepositoryInterface
{
/**
* @return string
*/
abstract protected static function businessModelClass(): string;

/**
* @return string
*/
abstract protected static function dataModelClass(): string;

/**
* @throws Exception
* @throws \yii\base\InvalidConfigException
*/
public function init()
{
parent::init();
if (!\Yii::createObject(static::dataModelClass()) instanceof ActiveRecordInterface) {
throw new Exception(get_called_class() . '::dataModelClass() must implements `yii\db\ActiveRecordInterface`');
}
if (!\Yii::createObject(static::businessModelClass()) instanceof BusinessObject) {
throw new Exception(get_called_class() . '::businessModelClass() must implements `albertborsos\ddd\interfaces\BusinessObject`');
}
}

/**
* @return ActiveQueryInterface the newly created [[ActiveQueryInterface]] instance.
*/
protected static function find()
{
return call_user_func([static::dataModelClass(), 'find']);
}

/**
* @param $condition
* @return BusinessObject|null
*/
public static function findOne($condition)
{
$model = call_user_func_array([static::dataModelClass(), 'findOne'], [$condition]);

return EntityFactory::create(static::businessModelClass(), $model);
}

/**
* @param $condition
* @return BusinessObject[]|array
*/
public static function findAll($condition)
{
$models = call_user_func_array([static::dataModelClass(), 'findAll'], [$condition]);

return EntityFactory::createCollection(static::businessModelClass(), $models);
}

/**
* @param BusinessObject $model
* @param bool $runValidation
* @param null $attributeNames
* @return bool|mixed
* @throws \yii\base\InvalidConfigException
*/
public function save(BusinessObject $model, $runValidation = true, $attributeNames = null)
{
/** @var ActiveRecordInterface $activerecord */
$activerecord = \Yii::createObject(static::dataModelClass(), [$model->attributes]);

if ($activerecord->save($runValidation, $attributeNames)) {
return $activerecord->getPrimaryKey();
}

return false;
}

/**
* @param ActiveRecordInterface $model
*/
/**
* @param BusinessObject $model
* @return int|bool the number of rows deleted, or `false` if the deletion is unsuccessful for some reason.
* Note that it is possible that the number of rows deleted is 0, even though the deletion execution is successful.
*/
public function delete(BusinessObject $model)
{
return $model->delete();
}
}
28 changes: 26 additions & 2 deletions src/models/AbstractService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use albertborsos\ddd\interfaces\BusinessObject;
use albertborsos\ddd\interfaces\FormObject;
use albertborsos\ddd\interfaces\RepositoryInterface;
use yii\base\Component;
use yii\helpers\ArrayHelper;
use yii\web\Link;
Expand Down Expand Up @@ -31,14 +32,22 @@ abstract class AbstractService extends Component
*/
private $_model;

public function __construct(FormObject $form = null, BusinessObject $model = null)
/**
* @var RepositoryInterface
*/
private $_repository;

public function __construct(FormObject $form = null, BusinessObject $model = null, RepositoryInterface $repository = null)
{
if ($form) {
$this->setForm($form);
}
if ($model) {
$this->setModel($model);
}
if ($repository) {
$this->setRepository($repository);
}
parent::__construct([]);
}

Expand All @@ -55,7 +64,6 @@ protected function getForm()
return $this->_form;
}


/**
* @return BusinessObject|ActiveRecord
*/
Expand All @@ -64,6 +72,14 @@ protected function getModel()
return $this->_model;
}

/**
* @return RepositoryInterface
*/
protected function getRepository()
{
return $this->_repository;
}

/**
* @param $id
*/
Expand Down Expand Up @@ -95,4 +111,12 @@ private function setModel(BusinessObject $model)
{
$this->_model = $model;
}

/**
* @param RepositoryInterface $repository
*/
private function setRepository(RepositoryInterface $repository)
{
$this->_repository = $repository;
}
}
50 changes: 50 additions & 0 deletions src/models/EntityFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace albertborsos\ddd\models;

use albertborsos\ddd\interfaces\BusinessObject;
use yii\base\Component;
use yii\db\ActiveRecordInterface;
use yii\helpers\Inflector;

class EntityFactory extends Component
{
public static function create($className, ActiveRecordInterface $model)
{
return self::fill(\Yii::createObject($className), $model->attributes);
}

public static function createCollection($className, array $models): array
{
return array_map(function ($model) use ($className) {
$entity = \Yii::createObject($className);

return self::fill($entity, $model->attributes);
}, $models);
}

private static function fill(BusinessObject $entity, array $attributes)
{
$attributes = self::normalizeIdAttributes($attributes);
foreach ($attributes as $attribute => $value) {
if (!property_exists($entity, $attribute)) {
continue;
}

$entity->$attribute = $value;
}

return $entity;
}

private static function normalizeIdAttributes(array $attributes)
{
if (!isset($attributes['_id'])) {
return $attributes;
}

$attributes['id'] = strval($attributes['_id']);

return $attributes;
}
}

0 comments on commit f0e097c

Please sign in to comment.