generated from yii2-extensions/template
-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
4ca7b65
commit 6db15c7
Showing
19 changed files
with
563 additions
and
51 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
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 |
---|---|---|
|
@@ -8,7 +8,7 @@ parameters: | |
- YII_ENV_PROD | ||
- YII_ENV_TEST | ||
|
||
level: 2 | ||
level: 5 | ||
|
||
paths: | ||
- src | ||
|
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 was deleted.
Oops, something went wrong.
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,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yii\CoreLibrary\Repository; | ||
|
||
use yii\db\ActiveQueryInterface; | ||
use yii\db\ActiveRecordInterface; | ||
|
||
final class FinderRepository implements FinderRepositoryInterface | ||
{ | ||
public function findById(ActiveRecordInterface $ar, int $id, string $key = 'id'): ActiveRecordInterface|array|null | ||
{ | ||
return $this->findByOneCondition($ar, [$key => $id]); | ||
} | ||
|
||
public function findByOneCondition(ActiveRecordInterface $ar, array $condition): ActiveRecordInterface|array|null | ||
{ | ||
return $ar->findOne($condition); | ||
} | ||
|
||
public function findByWhereCondition(ActiveRecordInterface $ar, array $condition): ActiveQueryInterface | ||
{ | ||
return $ar->find()->where($condition); | ||
} | ||
} |
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,45 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yii\CoreLibrary\Repository; | ||
|
||
use yii\db\ActiveQueryInterface; | ||
use yii\db\ActiveRecordInterface; | ||
|
||
/** | ||
* Provide methods to find and retrieve data. | ||
*/ | ||
interface FinderRepositoryInterface | ||
{ | ||
/** | ||
* Find a record by its primary key. | ||
* | ||
* @param ActiveRecordInterface $ar The ActiveRecord model class. | ||
* @param int $id The primary key value. | ||
* @param string $key The name of the primary key attribute (default is 'id'). | ||
* | ||
* @return ActiveRecordInterface|array|null The found record, or null if not found. | ||
*/ | ||
public function findById(ActiveRecordInterface $ar, int $id, string $key = 'id'): ActiveRecordInterface|array|null; | ||
|
||
/** | ||
* Find a single record by a specific condition. | ||
* | ||
* @param ActiveRecordInterface $ar The ActiveRecord model class. | ||
* @param array $condition The condition to search by. | ||
* | ||
* @return ActiveRecordInterface|array|null The found record, or null if not found. | ||
*/ | ||
public function findByOneCondition(ActiveRecordInterface $ar, array $condition): ActiveRecordInterface|array|null; | ||
|
||
/** | ||
* Find records that match a set of conditions. | ||
* | ||
* @param ActiveRecordInterface $ar The ActiveRecord model class. | ||
* @param array $condition The conditions to search by. | ||
* | ||
* @return ActiveQueryInterface A query object for further refinement or execution. | ||
*/ | ||
public function findByWhereCondition(ActiveRecordInterface $ar, array $condition): ActiveQueryInterface; | ||
} |
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,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yii\CoreLibrary\Repository; | ||
|
||
use yii\db\ActiveRecord; | ||
use yii\db\ActiveRecordInterface; | ||
|
||
final class PersistenceRepository extends Repository implements PersistenceRepositoryInterface | ||
{ | ||
public function deleteAll(ActiveRecordInterface $ar, array $condition): bool | ||
{ | ||
return $this->execute($ar->getDb(), static fn (): bool => $ar->deleteAll($condition) > 0); | ||
} | ||
|
||
public function save(ActiveRecordInterface $ar): bool | ||
{ | ||
return $this->execute($ar->getDb(), static fn (): bool => $ar->save()); | ||
} | ||
|
||
public function updateAtttributes(ActiveRecord $ar, array $attributes): bool | ||
{ | ||
return $this->execute( | ||
$ar->getDb(), | ||
static fn (): bool => $ar->updateAttributes($attributes) > 0, | ||
); | ||
} | ||
} |
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,43 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yii\CoreLibrary\Repository; | ||
|
||
use yii\db\ActiveRecord; | ||
use yii\db\ActiveRecordInterface; | ||
|
||
/** | ||
* Pprovide methods to persist data. | ||
*/ | ||
interface PersistenceRepositoryInterface | ||
{ | ||
/** | ||
* Delete records based on a set of conditions. | ||
* | ||
* @param ActiveRecordInterface $ar The ActiveRecord model class. | ||
* @param array $condition The conditions to determine which records to delete. | ||
* | ||
* @return bool Whether the deletion was successful. | ||
*/ | ||
public function deleteAll(ActiveRecordInterface $ar, array $condition): bool; | ||
|
||
/** | ||
* Save a record to the data store. | ||
* | ||
* @param ActiveRecordInterface $ar The ActiveRecord model instance to be saved. | ||
* | ||
* @return bool Whether the save operation was successful. | ||
*/ | ||
public function save(ActiveRecordInterface $ar): bool; | ||
|
||
/** | ||
* Update records based on a set of conditions. | ||
* | ||
* @param ActiveRecord $ar The ActiveRecord model class. | ||
* @param array $attributes The attribute values (name-value pairs) to be saved. | ||
* | ||
* @return bool Whether the update was successful. | ||
*/ | ||
public function updateAtttributes(ActiveRecord $ar, array $attributes): bool; | ||
} |
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,42 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yii\CoreLibrary\Repository; | ||
|
||
use Closure; | ||
use Yii; | ||
use yii\db\Connection; | ||
use yii\db\Exception; | ||
|
||
/** | ||
* Represents a base class for repositories providing common operations. | ||
*/ | ||
abstract class Repository | ||
{ | ||
/** | ||
* Execute a database operation within a transaction. | ||
* | ||
* @param Connection $db The database connection to use. | ||
* @param Closure $operation The operation to perform within the transaction. | ||
* | ||
* @return bool Whether the operation was successful. | ||
*/ | ||
protected function execute(Connection $db, Closure $operation): bool | ||
{ | ||
$transaction = $db->beginTransaction(); | ||
|
||
try { | ||
/** @var bool $result */ | ||
$result = $operation(); | ||
$transaction->commit(); | ||
|
||
return $result; | ||
} catch (Exception $e) { | ||
$transaction->rollBack(); | ||
Yii::error($e->getMessage(), __METHOD__); | ||
} | ||
|
||
return false; | ||
} | ||
} |
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,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yii\CoreLibrary\Validator; | ||
|
||
use Yii; | ||
use yii\base\ExitException; | ||
use yii\base\Model; | ||
use yii\web\Request; | ||
use yii\web\Response; | ||
use yii\widgets\ActiveForm; | ||
|
||
/** | ||
* Ajax validation in controllers. | ||
*/ | ||
trait AjaxValidator | ||
{ | ||
/** | ||
* Perform Ajax validation for a given model. | ||
* | ||
* @param Model $model The model to be validated. | ||
* | ||
* @throws ExitException | ||
*/ | ||
protected function performAjaxValidation(Model $model): void | ||
{ | ||
if ( | ||
$this->request instanceof Request && | ||
$this->response instanceof Response && | ||
$this->request->getIsAjax() && | ||
$model->load($this->request->post()) | ||
) { | ||
$this->response->format = Response::FORMAT_JSON; | ||
$this->response->data = ActiveForm::validate($model); | ||
$this->response->send(); | ||
|
||
Yii::$app->end(); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.