Skip to content

Commit

Permalink
forked from yii2-base, some cleanup made
Browse files Browse the repository at this point in the history
  • Loading branch information
metalagman committed Jul 6, 2017
1 parent 426d8dc commit e21c839
Show file tree
Hide file tree
Showing 13 changed files with 127 additions and 245 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
.idea/
.idea/
/vendor
/composer.lock
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# yii2-base
# yii2-tools-yarcode
Some yii2 extensions we use in our work.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "yarcode/yii2-base",
"description": "Useful Yii2 base components",
"name": "yarcode/yii2-tools-yarcode",
"description": "Useful Yii2 components",
"license": "MIT",
"type": "yii2-extension",
"keywords": ["yii2"],
Expand All @@ -24,7 +24,7 @@
},
"autoload": {
"psr-4": {
"yarcode\\base\\": "./src"
"YarCode\\Yii2\\": "./src"
}
}
}
78 changes: 0 additions & 78 deletions src/ActiveRecord.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/behaviors/TimestampBehavior.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace yarcode\base\behaviors;
namespace YarCode\Yii2\Behaviors;

use yii\db\Expression;

Expand Down
46 changes: 0 additions & 46 deletions src/helpers/DateTimeHelper.php

This file was deleted.

60 changes: 0 additions & 60 deletions src/helpers/TimeZoneHelper.php

This file was deleted.

14 changes: 10 additions & 4 deletions src/traits/CarbonModelTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@
/**
* @author Alexey Samoylov <[email protected]>
*/
namespace yarcode\base\traits;

namespace YarCode\Yii2\Traits;

use Carbon\Carbon;
use yii\base\Model;

/**
* Class CarbonModelTrait
* @package common\components\traits
* @package YarCode\Yii2\Traits
*
* @mixin Model
*/
trait CarbonModelTrait
{
/**
* Returns attribute value as a Carbon instance
* @param $attribute
* @return Carbon
*/
Expand All @@ -28,15 +30,19 @@ public function getCarbonAttribute($attribute)
}
if (is_numeric($value)) {
return Carbon::createFromTimestamp($value);
}
elseif (preg_match('/^(\d{4})-(\d{2})-(\d{2})$/', $value)) {
} elseif (preg_match('/^(\d{4})-(\d{2})-(\d{2})$/', $value)) {
return Carbon::createFromFormat('Y-m-d', $value)->startOfDay();
} else {
return Carbon::createFromFormat($this->getCarbonFormat($attribute), $value);
}
}
}

/**
* Returns datetime format for a specified attribute
* @param string $attribute
* @return string
*/
public function getCarbonFormat($attribute)
{
return 'Y-m-d H:i:s';
Expand Down
79 changes: 79 additions & 0 deletions src/traits/FragileModelTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
/**
* @author Alexey Samoylov <[email protected]>
*/

namespace YarCode\Yii2\Traits;

use yii\db\ActiveRecord;
use yii\db\Exception;
use yii\helpers\VarDumper;

/**
* Trait FragileActiveRecordTrait
* @package YarCode\Yii2\Traits
*
* @mixin ActiveRecord
*/
trait FragileModelTrait
{
/**
* @param bool $runValidation
* @param array|null $attributeNames
* @return true
* @throws Exception
* @throws \LogicException
*/
public function trySave($runValidation = true, $attributeNames = null)
{
if (false === $this->save($runValidation, $attributeNames)) {
if ($this->hasErrors()) {
throw new \LogicException('Model save failed due to validation errors: ' . VarDumper::dumpAsString($this->getErrors()));
} else {
$pdo = static::getDb()->pdo;
throw new Exception('Model save failed', $pdo->errorInfo(), $pdo->errorCode());
}
}
return true;
}

/**
* @param bool $runValidation
* @param array|null $attributeNames
* @return true
* @throws Exception
* @throws \LogicException
*/
public function tryUpdate($runValidation = true, $attributeNames = null)
{
if (false === $this->update($runValidation, $attributeNames)) {
if ($this->hasErrors()) {
throw new \LogicException('Model update failed due to validation errors: ' . VarDumper::dumpAsString($this->getErrors()));
} else {
$pdo = static::getDb()->pdo;
throw new Exception('Model update failed', $pdo->errorInfo(), $pdo->errorCode());
}
}
return true;
}

/**
* @param bool $runValidation
* @param array|null $attributeNames
* @return true
* @throws Exception
* @throws \LogicException
*/
public function tryInsert($runValidation = true, $attributeNames = null)
{
if (false === $this->insert($runValidation, $attributeNames)) {
if ($this->hasErrors()) {
throw new \LogicException('Model insert failed due to validation errors: ' . VarDumper::dumpAsString($this->getErrors()));
} else {
$pdo = static::getDb()->pdo;
throw new Exception('Model insert failed', $pdo->errorInfo(), $pdo->errorCode());
}
}
return true;
}
}
4 changes: 2 additions & 2 deletions src/traits/ListDataTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
/**
* @author Alexey Samoylov <[email protected]>
*/
namespace yarcode\base\traits;
namespace YarCode\Yii2\Traits;

use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper;

/**
* Class ListDataTrait
* @package yarcode\base\traits
* @package YarCode\Yii2\Traits
*
* @mixin ActiveRecord
*/
Expand Down
Loading

0 comments on commit e21c839

Please sign in to comment.