-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
188 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |