Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed Nov 2, 2023
1 parent 4ca7b65 commit 6db15c7
Show file tree
Hide file tree
Showing 19 changed files with 563 additions and 51 deletions.
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<p align="center">
<a href="https://github.com/yii2-extensions/template" target="_blank">
<a href="https://github.com/yii2-extensions/core-library" target="_blank">
<img src="https://www.yiiframework.com/image/yii_logo_light.svg" height="100px;">
</a>
<h1 align="center">Yii2-Template.</h1>
<h1 align="center">Core library.</h1>
<br>
</p>

Expand All @@ -13,20 +13,20 @@
<a href="https://github.com/yiisoft/yii2/tree/2.2" target="_blank">
<img src="https://img.shields.io/badge/Yii2%20version-2.2-blue" alt="yii2-version">
</a>
<a href="https://github.com/yii2-extensions/template/actions/workflows/build.yml" target="_blank">
<img src="https://github.com/yii2-extensions/template/actions/workflows/build.yml/badge.svg" alt="PHPUnit">
<a href="https://github.com/yii2-extensions/core-library/actions/workflows/build.yml" target="_blank">
<img src="https://github.com/yii2-extensions/core-library/actions/workflows/build.yml/badge.svg" alt="PHPUnit">
</a>
<a href="https://codecov.io/gh/yii2-extensions/template" target="_blank">
<img src="https://codecov.io/gh/yii2-extensions/template/branch/main/graph/badge.svg?token=MF0XUGVLYC" alt="Codecov">
<a href="https://codecov.io/gh/yii2-extensions/core-library" target="_blank">
<img src="https://codecov.io/gh/yii2-extensions/core-library/branch/main/graph/badge.svg?token=MF0XUGVLYC" alt="Codecov">
</a>
<a href="https://github.com/yii2-extensions/template/actions/workflows/static.yml" target="_blank">
<a href="https://github.com/yii2-extensions/core-library/actions/workflows/static.yml" target="_blank">
<img src="https://github.com/yii2-extensions/gii/actions/workflows/static.yml/badge.svg" alt="PHPStan">
</a>
<a href="https://github.com/yii2-extensions/template/actions/workflows/static.yml" target="_blank">
<img src="https://img.shields.io/badge/PHPStan%20level-2-blue" alt="PHPStan level">
</a>
<a href="https://github.styleci.io/repos/698621511?branch=main" target="_blank">
<img src="https://github.styleci.io/repos/698621511/shield?branch=main" alt="Code style">
<a href="https://github.com/yii2-extensions/core-library/actions/workflows/static.yml" target="_blank">
<img src="https://img.shields.io/badge/PHPStan%20level-5-blue" alt="PHPStan level">
</a>
<a href="https://github.styleci.io/repos/713478109?branch=main" target="_blank">
<img src="https://github.styleci.io/repos/713478109/shield?branch=main" alt="Code style">
</a>
</p>

Expand Down
13 changes: 8 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
{
"name": "yii2/template",
"name": "yii2-extensions/core-library",
"type": "library",
"description": "_____",
"description": "Central repository for shared library code.",
"keywords": [
"_____"
"central",
"library",
"shared",
"yii2"
],
"license": "mit",
"minimum-stability": "dev",
Expand All @@ -19,12 +22,12 @@
},
"autoload": {
"psr-4": {
"yii\\template\\": "src"
"Yii\\CoreLibrary\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"yii\\template\\tests\\": "tests"
"Yii\\CoreLibrary\\Tests\\": "tests"
}
},
"extra": {
Expand Down
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ parameters:
- YII_ENV_PROD
- YII_ENV_TEST

level: 2
level: 5

paths:
- src
Expand Down
4 changes: 2 additions & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.2/phpunit.xsd"
bootstrap="vendor/autoload.php"
bootstrap="tests/bootstrap.php"
cacheDirectory=".phpunit.cache"
colors="true"
executionOrder="depends,defects"
Expand All @@ -11,7 +11,7 @@
stopOnFailure="false"
>
<testsuites>
<testsuite name="Template">
<testsuite name="Yii2-CoreLibrary">
<directory>tests</directory>
</testsuite>
</testsuites>
Expand Down
13 changes: 0 additions & 13 deletions src/Example.php

This file was deleted.

26 changes: 26 additions & 0 deletions src/Repository/FinderRepository.php
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);
}
}
45 changes: 45 additions & 0 deletions src/Repository/FinderRepositoryInterface.php
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;
}
29 changes: 29 additions & 0 deletions src/Repository/PersistenceRepository.php
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,
);
}
}
43 changes: 43 additions & 0 deletions src/Repository/PersistenceRepositoryInterface.php
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;
}
42 changes: 42 additions & 0 deletions src/Repository/Repository.php
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;
}
}
41 changes: 41 additions & 0 deletions src/Validator/AjaxValidator.php
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();
}
}
}
18 changes: 0 additions & 18 deletions tests/ExampleTest.php

This file was deleted.

Loading

0 comments on commit 6db15c7

Please sign in to comment.