Skip to content

Commit

Permalink
add Cycle ORM support
Browse files Browse the repository at this point in the history
  • Loading branch information
albertborsos committed Sep 23, 2019
1 parent 1566ae9 commit a0890d1
Show file tree
Hide file tree
Showing 56 changed files with 1,156 additions and 158 deletions.
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
"require": {
"php": ">=7.1.0",
"yiisoft/yii2": "~2.0.14",
"samdark/hydrator": "^1.0.2"
"zendframework/zend-hydrator": "~2.4|~3.0",
"samdark/hydrator": "^1.0.2",
"albertborsos/yii2-cycle": "dev-master"
},
"require-dev": {
"codeception/codeception": "~3.0",
Expand Down
4 changes: 2 additions & 2 deletions src/behaviors/AbstractUniqueSluggableBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ protected function checkRepositoryIsConfigured(): void

protected function validateSlug($slug)
{
foreach ($this->uniqueValidators() as $repositoryInterface => $validator) {
$this->uniqueValidator = array_merge($validator, ['targetClass' => $this->getRepository($repositoryInterface)->getDataModelClass()]);
foreach ($this->uniqueValidators() as $validator) {
$this->uniqueValidator = $validator;
$isUnique = parent::validateSlug($slug);
if (!$isUnique) {
return false;
Expand Down
37 changes: 14 additions & 23 deletions src/behaviors/SluggableBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,19 @@
namespace albertborsos\ddd\behaviors;

use albertborsos\ddd\base\EntityEvent;
use albertborsos\ddd\interfaces\ActiveRepositoryInterface;
use albertborsos\ddd\interfaces\EntityInterface;
use albertborsos\ddd\interfaces\RepositoryInterface;
use albertborsos\ddd\models\AbstractEntity;
use albertborsos\ddd\traits\EvaluateAttributesTrait;
use Yii;
use yii\base\InvalidConfigException;
use yii\db\BaseActiveRecord;
use yii\helpers\ArrayHelper;
use yii\validators\UniqueValidator;

class SluggableBehavior extends \yii\behaviors\SluggableBehavior
{
use EvaluateAttributesTrait;

/** @var ActiveRepositoryInterface */
/** @var RepositoryInterface */
public $repository;

/**
Expand All @@ -30,10 +29,11 @@ public function init()
}

/**
* @return ActiveRepositoryInterface
* @param null $interface
* @return RepositoryInterface
* @throws InvalidConfigException
*/
protected function getRepository($interface = null): ActiveRepositoryInterface
protected function getRepository($interface = null): RepositoryInterface
{
if (!empty($interface)) {
return Yii::createObject($interface);
Expand All @@ -42,14 +42,6 @@ protected function getRepository($interface = null): ActiveRepositoryInterface
return \Yii::createObject($this->repository);
}

/**
* @param ActiveRepositoryInterface $repository
*/
protected function setRepository(ActiveRepositoryInterface $repository): void
{
$this->repository = $repository;
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -107,21 +99,20 @@ protected function isNewSlugNeededByEvent(EntityEvent $event)
*/
protected function validateSlug($slug)
{
/* @var $validator UniqueValidator */
/* @var $model BaseActiveRecord */
/* @var $validator \albertborsos\ddd\validators\UniqueValidator */
$validator = Yii::createObject(array_merge(
[
'class' => UniqueValidator::className(),
'class' => \albertborsos\ddd\validators\UniqueValidator::class,
'targetRepository' => $this->repository,
],
$this->uniqueValidator
));

$model = Yii::createObject($validator->targetClass ?? $this->getRepository()->getDataModelClass());
$model->clearErrors();
$model->{$this->slugAttribute} = $slug;

$validator->validateAttribute($model, $this->slugAttribute);
return !$model->hasErrors();
/* @var $entity AbstractEntity|EntityInterface */
$entity = clone $this->owner;
$entity->{$this->slugAttribute} = $slug;
$validator->validateAttribute($entity, $this->slugAttribute);
return !$entity->hasErrors();
}

protected function setDefaultAttributes(): void
Expand Down
1 change: 0 additions & 1 deletion src/hydrators/ActiveHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use albertborsos\ddd\interfaces\EntityInterface;
use albertborsos\ddd\interfaces\HydratorInterface;
use albertborsos\ddd\models\AbstractEntity;
use yii\base\Component;
use yii\base\Model;

Expand Down
41 changes: 41 additions & 0 deletions src/hydrators/ZendHydrator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace albertborsos\ddd\hydrators;

use albertborsos\ddd\interfaces\HydratorInterface;
use yii\base\Component;
use Zend\Hydrator\ReflectionHydrator;

class ZendHydrator extends Component implements HydratorInterface
{
/** @var ReflectionHydrator */
protected $hydrator;

public function __construct($map, $config = [])
{
parent::__construct($config);
$this->hydrator = new ReflectionHydrator();
}

public function hydrate($className, $data)
{
return $this->hydrator->hydrate(is_object($data) ? $this->extract($data) : $data, \Yii::createObject($className));
}

public function hydrateAll($className, array $data): array
{
return array_map(function ($object) use ($className) {
return $this->hydrate($className, $object);
}, $data);
}

public function hydrateInto($object, array $data)
{
return $this->hydrator->hydrate($data, $object);
}

public function extract($object): array
{
return $this->hydrator->extract($object);
}
}
19 changes: 19 additions & 0 deletions src/interfaces/HydratorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,30 @@
*/
interface HydratorInterface
{
/**
* @param $className
* @param $data
* @return mixed
*/
public function hydrate($className, $data);

/**
* @param $className
* @param array $data
* @return array
*/
public function hydrateAll($className, array $data): array;

/**
* @param object $object
* @param array $data
* @return mixed
*/
public function hydrateInto($object, array $data);

/**
* @param object $object
* @return array
*/
public function extract($object): array;
}
9 changes: 6 additions & 3 deletions src/interfaces/RepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,19 @@ public function findById($id): ?EntityInterface;
/**
* @param EntityInterface $entity
* @param array $attributes
* @param $addNotConditionForPrimaryKeys
* @param array $filter
* @return bool
*/
public function exists(EntityInterface $entity, $attributes = [], $addNotConditionForPrimaryKeys = false): bool;
public function exists(EntityInterface $entity, $attributes = [], $filter = []): bool;

/**
* @param EntityInterface $entity
* @param bool $runValidation
* @param null $attributeNames
* @param bool $checkIsNewRecord
* @return bool
*/
public function insert(EntityInterface $entity, $runValidation = true, $attributeNames = null): bool;
public function insert(EntityInterface $entity, $runValidation = true, $attributeNames = null, $checkIsNewRecord = true): bool;

/**
* @param EntityInterface $entity
Expand Down Expand Up @@ -84,4 +85,6 @@ public function search($params, $formName = null): BaseDataProvider;
* @return string
*/
public function getEntityClass(): string;

public function beginTransaction();
}
15 changes: 0 additions & 15 deletions src/models/AbstractActiveService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace albertborsos\ddd\models;

use albertborsos\ddd\interfaces\ActiveRepositoryInterface;
use albertborsos\ddd\interfaces\EntityInterface;
use albertborsos\ddd\interfaces\FormObject;

/**
Expand All @@ -13,19 +11,6 @@
*/
abstract class AbstractActiveService extends AbstractService
{
public function __construct(FormObject $form = null, EntityInterface $entity = null, $config = [])
{
parent::__construct($form, $entity, $config);
}

/**
* @return ActiveRepositoryInterface
*/
protected function getRepository(): ActiveRepositoryInterface
{
return $this->repository;
}

/**
* @return bool
*/
Expand Down
1 change: 0 additions & 1 deletion src/models/AbstractEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use albertborsos\ddd\interfaces\EntityInterface;
use yii\base\InvalidConfigException;
use yii\base\Model;
use yii\db\ActiveRecordInterface;
use yii\helpers\Inflector;

/**
Expand Down
4 changes: 2 additions & 2 deletions src/models/AbstractService.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ protected function getEntity(): ?EntityInterface
}

/**
* @return RepositoryInterface
* @return RepositoryInterface|null
* @since 2.0.0
*/
protected function getRepository()
protected function getRepository(): ?RepositoryInterface
{
return $this->repository;
}
Expand Down
20 changes: 17 additions & 3 deletions src/repositories/AbstractActiveRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,26 @@ public function delete(EntityInterface $entity): bool
/**
* @param EntityInterface $entity
* @param array $attributes
* @param bool $addNotConditionForPrimaryKeys
* @param array $filter
* @return bool
*/
public function exists(EntityInterface $entity, $attributes = [], $addNotConditionForPrimaryKeys = false): bool
public function exists(EntityInterface $entity, $attributes = [], $filter = []): bool
{
return $this->find()->andWhere($this->createFindConditionByEntityKeys($entity, $attributes, true, $addNotConditionForPrimaryKeys))->exists();
$query = $this->find();

if (empty($attributes)) {
$attributes = is_array($entity->getPrimaryKey()) ? $entity->getPrimaryKey() : [$entity->getPrimaryKey()];
}

foreach ($attributes as $attribute) {
$query->andWhere([$attribute => $entity->{$attribute}]);
}

foreach ($filter as $condition) {
$query->andWhere($condition);
}

return $query->exists();
}

/**
Expand Down

0 comments on commit a0890d1

Please sign in to comment.