-
Notifications
You must be signed in to change notification settings - Fork 15
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
igor-chepurnoi
committed
Nov 22, 2016
1 parent
32cdfa6
commit b224a3e
Showing
9 changed files
with
83 additions
and
44 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
$finder = Symfony\CS\Finder::create() | ||
->exclude('vendor') | ||
->in([__DIR__]); | ||
|
||
$config = Symfony\CS\Config::create() | ||
->fixers([ | ||
'-phpdoc_params', | ||
'-phpdoc_short_description', | ||
'-phpdoc_inline_tag', | ||
'-pre_increment', | ||
'-heredoc_to_nowdoc', | ||
'-spaces_cast', | ||
'-include', | ||
'-phpdoc_no_package', | ||
'concat_with_spaces', | ||
'ordered_use', | ||
'short_array_syntax', | ||
]) | ||
->finder($finder); | ||
|
||
return $config; |
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 |
---|---|---|
|
@@ -9,7 +9,9 @@ | |
|
||
/** | ||
* Class BaseEnum | ||
* | ||
* @author Dmitry Semenov <[email protected]> | ||
* | ||
* @package yii2mod\enum\helpers | ||
*/ | ||
abstract class BaseEnum | ||
|
@@ -48,14 +50,14 @@ abstract class BaseEnum | |
/** | ||
* Sets the value that will be managed by this type instance. | ||
* | ||
* @param mixed $value The value to be managed. | ||
* @param mixed $value The value to be managed | ||
* | ||
* @throws BadRequestHttpException If the value is not valid. | ||
* @throws BadRequestHttpException If the value is not valid | ||
*/ | ||
public function __construct($value) | ||
{ | ||
if (!self::isValidValue($value)) { | ||
throw new BadRequestHttpException; | ||
throw new BadRequestHttpException(); | ||
} | ||
|
||
$this->value = $value; | ||
|
@@ -64,10 +66,10 @@ public function __construct($value) | |
/** | ||
* Creates a new type instance for a called name. | ||
* | ||
* @param string $name The name of the value. | ||
* @param array $arguments An ignored list of arguments. | ||
* @param string $name The name of the value | ||
* @param array $arguments An ignored list of arguments | ||
* | ||
* @return $this The new type instance. | ||
* @return $this The new type instance | ||
*/ | ||
public static function __callStatic($name, array $arguments = []) | ||
{ | ||
|
@@ -77,57 +79,62 @@ public static function __callStatic($name, array $arguments = []) | |
/** | ||
* Creates a new type instance using the name of a value. | ||
* | ||
* @param string $name The name of a value. | ||
* @param string $name The name of a value | ||
* | ||
* @throws \yii\web\BadRequestHttpException | ||
* @return $this The new type instance. | ||
* | ||
* @return $this The new type instance | ||
*/ | ||
public static function createByName($name) | ||
{ | ||
$constants = self::getConstantsByName(); | ||
|
||
if (!array_key_exists($name, $constants)) { | ||
throw new BadRequestHttpException; | ||
throw new BadRequestHttpException(); | ||
} | ||
|
||
return new static($constants[$name]); | ||
} | ||
|
||
/** | ||
* get constant key by value(label) | ||
* | ||
* @param $value | ||
* | ||
* @return mixed | ||
*/ | ||
public static function getValueByName($value) | ||
{ | ||
$list = self::listData(); | ||
|
||
return array_search($value, $list); | ||
} | ||
|
||
/** | ||
* Creates a new type instance using the value. | ||
* | ||
* @param mixed $value The value. | ||
* @param mixed $value The value | ||
* | ||
* @throws \yii\web\BadRequestHttpException | ||
* @return $this The new type instance. | ||
* | ||
* @return $this The new type instance | ||
*/ | ||
public static function createByValue($value) | ||
{ | ||
$constants = self::getConstantsByValue(); | ||
|
||
if (!array_key_exists($value, $constants)) { | ||
throw new BadRequestHttpException; | ||
throw new BadRequestHttpException(); | ||
} | ||
|
||
return new static($value); | ||
} | ||
|
||
/** | ||
* Get list data | ||
* | ||
* @static | ||
* | ||
* @return mixed | ||
*/ | ||
public static function listData() | ||
|
@@ -140,12 +147,15 @@ public static function listData() | |
$result = ArrayHelper::getColumn(self::$list[$class], function ($value) { | ||
return Yii::t(self::$messageCategory, $value); | ||
}); | ||
|
||
return $result; | ||
} | ||
|
||
/** | ||
/** | ||
* Get label by value | ||
* | ||
* @var string value | ||
* | ||
* @return string label | ||
*/ | ||
public static function getLabel($value) | ||
|
@@ -154,13 +164,14 @@ public static function getLabel($value) | |
if (isset($list[$value])) { | ||
return Yii::t(static::$messageCategory, $list[$value]); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* Returns the list of constants (by name) for this type. | ||
* | ||
* @return array The list of constants by name. | ||
* @return array The list of constants by name | ||
*/ | ||
public static function getConstantsByName() | ||
{ | ||
|
@@ -187,7 +198,7 @@ public static function getConstantsByName() | |
/** | ||
* Returns the list of constants (by value) for this type. | ||
* | ||
* @return array The list of constants by value. | ||
* @return array The list of constants by value | ||
*/ | ||
public static function getConstantsByValue() | ||
{ | ||
|
@@ -202,10 +213,10 @@ public static function getConstantsByValue() | |
if (array_key_exists($value, self::$byValue[$class])) { | ||
if (!is_array(self::$byValue[$class][$value])) { | ||
self::$byValue[$class][$value] = [ | ||
self::$byValue[$class][$value] | ||
self::$byValue[$class][$value], | ||
]; | ||
} | ||
self::$byValue[$class][$value][] = $name;; | ||
self::$byValue[$class][$value][] = $name; | ||
} else { | ||
self::$byValue[$class][$value] = $name; | ||
} | ||
|
@@ -218,7 +229,7 @@ public static function getConstantsByValue() | |
/** | ||
* Returns the name of the value. | ||
* | ||
* @return array|string The name, or names, of the value. | ||
* @return array|string The name, or names, of the value | ||
*/ | ||
public function getName() | ||
{ | ||
|
@@ -230,7 +241,7 @@ public function getName() | |
/** | ||
* Unwraps the type and returns the raw value. | ||
* | ||
* @return mixed The raw value managed by the type instance. | ||
* @return mixed The raw value managed by the type instance | ||
*/ | ||
public function getValue() | ||
{ | ||
|
@@ -240,10 +251,10 @@ public function getValue() | |
/** | ||
* Checks if a name is valid for this type. | ||
* | ||
* @param string $name The name of the value. | ||
* @param string $name The name of the value | ||
* | ||
* @return boolean If the name is valid for this type, `true` is returned. | ||
* Otherwise, the name is not valid and `false` is returned. | ||
* @return bool If the name is valid for this type, `true` is returned. | ||
* Otherwise, the name is not valid and `false` is returned | ||
*/ | ||
public static function isValidName($name) | ||
{ | ||
|
@@ -255,10 +266,10 @@ public static function isValidName($name) | |
/** | ||
* Checks if a value is valid for this type. | ||
* | ||
* @param string $value The value. | ||
* @param string $value The value | ||
* | ||
* @return boolean If the value is valid for this type, `true` is returned. | ||
* Otherwise, the value is not valid and `false` is returned. | ||
* @return bool If the value is valid for this type, `true` is returned. | ||
* Otherwise, the value is not valid and `false` is returned | ||
*/ | ||
public static function isValidValue($value) | ||
{ | ||
|
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
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