From e21c839b104848a4eb311bab743cc81037571585 Mon Sep 17 00:00:00 2001 From: Alexey Date: Thu, 6 Jul 2017 18:38:51 +0700 Subject: [PATCH] forked from yii2-base, some cleanup made --- .gitignore | 4 +- README.md | 2 +- composer.json | 6 +- src/ActiveRecord.php | 78 ------------------ src/behaviors/TimestampBehavior.php | 2 +- src/helpers/DateTimeHelper.php | 46 ----------- src/helpers/TimeZoneHelper.php | 60 -------------- src/traits/CarbonModelTrait.php | 14 +++- src/traits/FragileModelTrait.php | 79 +++++++++++++++++++ src/traits/ListDataTrait.php | 4 +- src/traits/SpoolTrait.php | 37 --------- .../{StatusTrait.php => StatusLabelTrait.php} | 20 +++-- .../{TypeTrait.php => TypeLabelTrait.php} | 20 +++-- 13 files changed, 127 insertions(+), 245 deletions(-) delete mode 100644 src/ActiveRecord.php delete mode 100644 src/helpers/DateTimeHelper.php delete mode 100644 src/helpers/TimeZoneHelper.php create mode 100644 src/traits/FragileModelTrait.php delete mode 100644 src/traits/SpoolTrait.php rename src/traits/{StatusTrait.php => StatusLabelTrait.php} (60%) rename src/traits/{TypeTrait.php => TypeLabelTrait.php} (53%) diff --git a/.gitignore b/.gitignore index 62c8935..3ea75e6 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -.idea/ \ No newline at end of file +.idea/ +/vendor +/composer.lock \ No newline at end of file diff --git a/README.md b/README.md index 3e27d50..3c71afd 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,2 @@ -# yii2-base +# yii2-tools-yarcode Some yii2 extensions we use in our work. diff --git a/composer.json b/composer.json index cddbb6d..ffc7701 100644 --- a/composer.json +++ b/composer.json @@ -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"], @@ -24,7 +24,7 @@ }, "autoload": { "psr-4": { - "yarcode\\base\\": "./src" + "YarCode\\Yii2\\": "./src" } } } \ No newline at end of file diff --git a/src/ActiveRecord.php b/src/ActiveRecord.php deleted file mode 100644 index e98318d..0000000 --- a/src/ActiveRecord.php +++ /dev/null @@ -1,78 +0,0 @@ -save($runValidation, $attributeNames)) { - throw new \LogicException("Saving error: " . VarDumper::dumpAsString($this->getErrors())); - } - return true; - } - - /** - * @param bool $runValidation - * @param array|null $attributeNames - * @return true - */ - public function tryUpdate($runValidation = true, $attributeNames = null) - { - if (false === $this->update($runValidation, $attributeNames)) { - throw new \LogicException("Saving error: " . VarDumper::dumpAsString($this->getErrors())); - } - return true; - } - - /** - * @param bool $runValidation - * @param array|null $attributeNames - * @return true - */ - public function tryInsert($runValidation = true, $attributeNames = null) - { - if (false === $this->insert($runValidation, $attributeNames)) { - throw new \LogicException("Saving error: " . VarDumper::dumpAsString($this->getErrors())); - } - return true; - } - - /** - * Converts model data to array of the params suitable for use in templates - * - * @return array - */ - public function getTemplateParams() { - return ['model' => $this]; - } - - /** - * @param string $attribute Attribute name - * @param \DateTimeZone $tz Timezone of the stored value - * @throws \BadMethodCallException - * @return \DateTime - */ - public function getAttributeDateTime($attribute, \DateTimeZone $tz = null) - { - if (empty($tz)) { - $tz = DateTimeHelper::getUtc(); - } - $dt = \DateTime::createFromFormat(DateTimeHelper::FORMAT_SQL_DATETIME, $this->getAttribute($attribute), $tz); - if ($dt == false) { - throw new \BadMethodCallException('Cannot create datetime from attribute "' . $attribute . '"'); - } - return $dt; - } -} diff --git a/src/behaviors/TimestampBehavior.php b/src/behaviors/TimestampBehavior.php index c396805..5866172 100644 --- a/src/behaviors/TimestampBehavior.php +++ b/src/behaviors/TimestampBehavior.php @@ -1,5 +1,5 @@ setTimezone($tz); - } - - return $result; - } - - - /** - * Return DateTime in Utc timezone for "now" time - * @return \DateTime - */ - public function getUtcDateTime($date = 'now') - { - return new \DateTime($date, TimeZoneHelper::getUtc()); - } -} diff --git a/src/helpers/TimeZoneHelper.php b/src/helpers/TimeZoneHelper.php deleted file mode 100644 index 1792ef2..0000000 --- a/src/helpers/TimeZoneHelper.php +++ /dev/null @@ -1,60 +0,0 @@ - - */ - -namespace yarcode\base\helpers; - -use yii\base\Component; - -/** - * Class TimeZoneHelper - * @package yarcode\base\helpers - */ -class TimeZoneHelper extends Component -{ - /** - * Return UTC timezone object - * - * @return \DateTimeZone - */ - public static function getUtc() - { - return new \DateTimeZone('UTC'); - } - - /** - * Return current application timezone object - * - * @return \DateTimeZone - */ - public static function getAppTz() - { - return new \DateTimeZone(\Yii::$app->timeZone); - } - - /** - * Return new datetime object, converted to UTC timezone - * - * @param \DateTime $date - * @return $this - */ - public static function toUtc(\DateTime $date) - { - $newDate = clone $date; - return $newDate->setTimezone(static::getUtc()); - } - - /** - * Return new datetime object, converted to application timezone - * - * @param \DateTime $date - * @return $this - */ - public static function toApp(\DateTime $date) - { - $newDate = clone $date; - return $newDate->setTimezone(static::getAppTz()); - } - -} \ No newline at end of file diff --git a/src/traits/CarbonModelTrait.php b/src/traits/CarbonModelTrait.php index 22b8be6..979e432 100644 --- a/src/traits/CarbonModelTrait.php +++ b/src/traits/CarbonModelTrait.php @@ -2,20 +2,22 @@ /** * @author Alexey Samoylov */ -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 */ @@ -28,8 +30,7 @@ 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); @@ -37,6 +38,11 @@ public function getCarbonAttribute($attribute) } } + /** + * Returns datetime format for a specified attribute + * @param string $attribute + * @return string + */ public function getCarbonFormat($attribute) { return 'Y-m-d H:i:s'; diff --git a/src/traits/FragileModelTrait.php b/src/traits/FragileModelTrait.php new file mode 100644 index 0000000..a560ff1 --- /dev/null +++ b/src/traits/FragileModelTrait.php @@ -0,0 +1,79 @@ + + */ + +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; + } +} \ No newline at end of file diff --git a/src/traits/ListDataTrait.php b/src/traits/ListDataTrait.php index 389c93f..83cb3b9 100644 --- a/src/traits/ListDataTrait.php +++ b/src/traits/ListDataTrait.php @@ -2,14 +2,14 @@ /** * @author Alexey Samoylov */ -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 */ diff --git a/src/traits/SpoolTrait.php b/src/traits/SpoolTrait.php deleted file mode 100644 index 433c355..0000000 --- a/src/traits/SpoolTrait.php +++ /dev/null @@ -1,37 +0,0 @@ -status, $default); + return ArrayHelper::getValue(static::getStatusLabels(), static::getStatusAttributeName(), $default); } /** @@ -30,6 +30,14 @@ public function getStatusLabel($default = null) */ public static function getStatusLabels() { - throw new InvalidConfigException('Please define static getStatusLabels() in your class ' . get_called_class()); + throw new InvalidConfigException('Please override ' . __METHOD__); + } + + /** + * @return string + */ + public static function getStatusAttributeName() + { + return 'status'; } } \ No newline at end of file diff --git a/src/traits/TypeTrait.php b/src/traits/TypeLabelTrait.php similarity index 53% rename from src/traits/TypeTrait.php rename to src/traits/TypeLabelTrait.php index 061042e..17e3e01 100644 --- a/src/traits/TypeTrait.php +++ b/src/traits/TypeLabelTrait.php @@ -1,5 +1,6 @@ type, $default); + return ArrayHelper::getValue(static::getTypeLabels(), static::getTypeAttributeName(), $default); } /** @@ -30,6 +30,14 @@ public function getTypeLabel($default = null) */ public static function getTypeLabels() { - throw new InvalidConfigException('Please define static getTypeLabels() in your class ' . get_called_class()); + throw new InvalidConfigException('Please override ' . __METHOD__); + } + + /** + * @return string + */ + public static function getTypeAttributeName() + { + return 'type'; } }