/** * Constructor. * The default implementation does two things: * * - Initializes the object with the given configuration `$config`. * - Call [[init()]]. * * If this method is overridden in a child class, it is recommended that * * - the last parameter of the constructor is a configuration array, like `$config` here. * - call the parent implementation at the end of the constructor. * * @param array $config name-value pairs that will be used to initialize the object properties */ public function __construct($config = []) { if (!empty($config)) { foreach ($config as $name => $value) { $name = Inflector::variablize($name); $this->{$name} = $value; } } }
/** * Constructor override. Required to transform the parameter names to variable names. * * From [[yii\base\Object]]: * The default implementation does two things: * * - Initializes the object with the given configuration `$config`. * - Call [[init()]]. * * If this method is overridden in a child class, it is recommended that * * - the last parameter of the constructor is a configuration array, like `$config` here. * - call the parent implementation at the end of the constructor. * @param array $config */ public function __construct($config = []) { if (!empty($config)) { $parsedConfig = []; foreach ($config as $k => $value) { $parsedConfig[Inflector::variablize($k)] = $value; } Yii::configure($this, $parsedConfig); } $this->init(); }
/** * Returns the name of the object that is going to be used as a js variable of the renderer service. * * @param bool $autoGenerate whether to auto-generate the name or not * * @return string */ public function getName($autoGenerate = true) { if (!empty($this->_name)) { return $this->_name; } if ($autoGenerate) { $reflection = new \ReflectionClass($this); $this->_name = self::$autoNamePrefix . Inflector::variablize($reflection->getShortName()) . self::$counter++; } return $this->_name; }
/** * @param $data * @return mixed */ private static function reformatKeys($data) { foreach ($data as $key => $value) { ArrayHelper::remove($data, $key); if (is_array($value)) { $data[Inflector::variablize($key)] = self::reformatKeys($value); } else { $data[Inflector::variablize($key)] = $value; } } return $data; }
/** * Sets the attribute values in a massive way. * @param array $values attribute values (name => value) to be assigned to the model. * @param boolean $safeOnly whether the assignments should only be done to the safe attributes. * A safe attribute is one that is associated with a validation rule in the current [[scenario]]. * @see safeAttributes() * @see attributes() */ public function setAttributes($values, $safeOnly = true) { if (is_array($values)) { $attributes = array_flip($safeOnly ? $this->safeAttributes() : $this->attributes()); foreach ($values as $name => $value) { $name = Inflector::variablize($name); if (isset($attributes[$name])) { $this->{$name} = $value; } elseif ($safeOnly) { $this->onUnsafeAttribute($name, $value); } } } }
/** * @param $keys */ public function setKeys($keys) { $variablized = $values = []; foreach ($keys as $key => $data) { $variablizedKey = Inflector::variablize($key); $this->map[$variablizedKey] = $key; $values[$variablizedKey] = $this->getKeyStorage()->get($key, null, false); $variablized[$variablizedKey] = $data; } $this->keys = $variablized; foreach ($values as $k => $v) { $this->setAttribute($k, $v); } parent::init(); }
/** * @inheritdoc */ public function run() { $id = $this->getId(); $hiddenId = $id . '-hidden'; echo Html::hiddenInput($this->name, null, ['id' => $hiddenId]); echo Html::tag('div', null, ['id' => $id]); $var = Inflector::variablize($id); $view = $this->getView(); RangeFilterAsset::register($view); $options = Json::encode($this->pluginOptions); $view->registerJs("var {$var} = jQuery('#{$id}');"); $view->registerJs("{$var}.rangeFilter({$options});"); $view->registerJs("jQuery('#{$hiddenId}').val(JSON.stringify({$var}.rangeFilter('getFilter')));"); $view->registerJs("{$var}.on('rangefilter.change', function(){ jQuery('#{$hiddenId}').val(JSON.stringify({$var}.rangeFilter('getFilter'))); });"); }
public function init() { parent::init(); if (!isset($this->siteKey)) { $this->siteKey = \Yii::$app->params['reCaptcha']['siteKey']; if (empty($this->siteKey)) { throw new InvalidConfigException("Required `siteKey` is not found."); } } Html::addCssClass($this->captchaOptions, 'g-recaptcha'); $this->captchaOptions['data']['sitekey'] = $this->siteKey; $this->captchaOptions['id'] = $this->options['id'] . '_captcha'; $this->_callback = Inflector::variablize($this->options['id'] . '_callback'); $this->captchaOptions['data']['callback'] = $this->_callback; }
/** * @inheritdoc */ public function run() { $hiddenId = ArrayHelper::remove($this->options, 'id'); if ($this->hasModel()) { $value = Html::getAttributeValue($this->model, $this->attribute); echo Html::activeHiddenInput($this->model, $this->attribute, ['id' => $hiddenId]); } else { $value = $value = $this->value; echo Html::hiddenInput($this->name, $value, ['id' => $hiddenId]); } $id = $this->getId() . '-editor'; $this->options['id'] = $id; $var = Inflector::variablize($id); echo Html::tag('textarea', Html::encode($value), $this->options); $view = $this->getView(); CodeMirrorAsset::register($view); $options = Json::encode($this->pluginOptions); $view->registerJs("var {$var} = CodeMirror.fromTextArea(document.getElementById('{$id}'), {$options});"); $view->registerJs("{$var}.on('change', function(editor){jQuery('#{$hiddenId}').val(editor.getValue());});"); }
/** * */ public function registerClientOptions() { $options = empty($this->clientOptions) ? '{}' : Json::htmlEncode($this->clientOptions); if (empty($this->options['id'])) { $this->options['id'] = $this->getId(); } if ($this->varName === null) { $this->varName = Inflector::variablize($this->options['id']); } $view = $this->getView(); $var = empty($this->varName) ? '' : "var {$this->varName} = "; $id = json_encode($this->options['id']); if ($this->position !== null && $this->position <= View::POS_BEGIN) { $view->registerAssetBundle(HandsontableAsset::className(), View::POS_HEAD); } else { $view->registerAssetBundle(HandsontableAsset::className()); } $position = $this->position ?: View::POS_READY; $view->registerJs("{$var}new Handsontable(document.getElementById({$id}),{$options});", $position); }
/** * Creates a JavaScript variable name for the object * * @param string Prefix for JavaScript variable name * @return string JavaScript variable name for the object */ public function jsVar($prefix = '') { return Inflector::variablize($prefix . ' ' . basename(get_class()) . self::$_counter++); }
/** * @inheritdoc */ public function init() { static::$instance = $this; $this->_varName = Inflector::variablize($this->name); $this->requires[] = 'ui.router'; $this->requires = array_unique($this->requires); $view = $this->getView(); // Asset Dependency $am = Yii::$app->getAssetManager(); $key = md5(serialize([__CLASS__, Yii::$app->controller->route, $this->name])); $bundle = ['basePath' => '', 'depends' => [AngularAsset::className()], 'js' => []]; foreach ($this->requires as $module) { if (isset(AngularAsset::$assetMap[$module])) { $bundle['depends'][] = AngularAsset::$assetMap[$module]; } } $js = $this->generate(); if ($this->remote) { $path = sprintf('%x', crc32($key)); $jsName = Inflector::camel2id($this->name) . '.js'; $bundle['js'][] = $jsName; $bundle['basePath'] = $am->basePath . '/' . $path; $bundle['baseUrl'] = $am->baseUrl . '/' . $path; FileHelper::createDirectory(Yii::getAlias($bundle['basePath'])); file_put_contents(Yii::getAlias($bundle['basePath'] . '/' . $jsName), $js); } else { $view->registerJs($js, WebView::POS_END); } $am->bundles[$key] = new AssetBundle($bundle); $view->registerAssetBundle($key); $options = $this->options; if ($this->tag !== 'ui-view' && !isset($options['ui-view'])) { $options['ui-view'] = true; } if ($this->ngApp && !isset($options['ng-app'])) { $options['ng-app'] = $this->name; } echo Html::beginTag($this->tag, $options); }
/** * @inheritdoc */ public function init() { static::$instance = $this; $this->_varName = Inflector::variablize($this->name); }
public function generateRelationTo($relation) { $class = new \ReflectionClass($relation->modelClass); $route = Inflector::variablize($class->getShortName()); return $route; }
<?php use yii\helpers\StringHelper; use yii\helpers\Inflector; $modelClassName = StringHelper::basename($generator->modelClass); $idModelClassName = Inflector::camel2id($modelClassName); $varModelClassName = Inflector::variablize($modelClassName); echo "<?php\n"; ?> /** * @link http://www.yiiframework.com/ * @copyright Copyright (c) 2008 Yii Software LLC * @license http://www.yiiframework.com/license/ */ namespace app\assets; use yii\web\AssetBundle; /** * @author Qiang Xue <*****@*****.**> * @since 2.0 */ class <?php echo $modelClassName; ?> NewAsset extends AssetBundle { public $sourcePath = '@app/views/<?php echo $idModelClassName; ?>
/** * @inheritdoc */ public function getSearchableAttributes($recursive = true) { /** @var \im\base\types\EntityTypesRegister $typesRegister */ $typesRegister = Yii::$app->get('typesRegister'); /** @var \im\search\components\SearchManager $searchManager */ $searchManager = Yii::$app->get('searchManager'); $model = $this->getModel(); if ($model instanceof AttributeValue) { return []; } $entityType = $typesRegister->getEntityType($model); $attributes = $model->attributes(); $labels = $model->attributeLabels(); $searchableAttributes = []; $key = 0; // Attributes foreach ($attributes as $attribute) { $searchableAttributes[$key] = new AttributeDescriptor(['entity_type' => $entityType, 'name' => $attribute, 'label' => isset($labels[$attribute]) ? $labels[$attribute] : $model->generateAttributeLabel($attribute)]); $key++; } // EAV $eavAttributes = Attribute::findByEntityType($entityType); foreach ($eavAttributes as $attribute) { $searchableAttributes[$key] = new AttributeDescriptor(['entity_type' => $entityType, 'name' => $attribute->getName() . '_attr', 'label' => $attribute->getPresentation()]); $key++; } // Relations $class = new \ReflectionClass($model); foreach ($class->getMethods() as $method) { if ($method->isPublic()) { $methodName = $method->getName(); if (strpos($methodName, 'get') === 0) { $math = false; $name = substr($methodName, 3); if (substr($name, -8) === 'Relation') { $name = substr($name, 0, -8); $math = true; } elseif (preg_match('/@return.*ActiveQuery/i', $method->getDocComment())) { $math = true; } if ($math && $name) { /** @var ActiveQuery $relation */ $relation = $model->{$methodName}(); $modelClass = $relation->modelClass; $searchableType = $searchManager->getSearchableTypeByClass($modelClass); if (!$searchableType) { $reflector = new ReflectionClass($modelClass); /** @var SearchableInterface $searchableType */ $searchableType = Yii::createObject(['class' => 'im\\search\\components\\service\\db\\SearchableType', 'type' => Inflector::camel2id($reflector->getShortName(), '_'), 'modelClass' => $modelClass]); } $searchableAttributes[$key] = new AttributeDescriptor(['entity_type' => $entityType, 'name' => Inflector::variablize(substr($methodName, 3)), 'label' => Inflector::titleize($name), 'value' => function ($model) use($methodName) { return $model->{$methodName}(); }, 'type' => $searchableType->getType()]); $key++; if ($recursive) { foreach ($searchableType->getSearchableAttributes(false) as $attribute) { $attribute->label = Inflector::titleize($name) . ' >> ' . $attribute->label; $attribute->name = Inflector::variablize(substr($methodName, 3)) . '.' . $attribute->name; $searchableAttributes[$key] = $attribute; $key++; } } } } } } return $searchableAttributes; }
public function variablize($classPath) { $className = StringHelper::basename($classPath); return Inflector::variablize($className); }
/** * @param ActiveQuery $relation * @param string $name * @param string $label * @param bool $recursive * @return AttributeDescriptor[] */ protected function getRelationAttributes(ActiveQuery $relation, $name, $label = '', $recursive = true) { $searchableAttributes = []; $label = $label ?: $name; /** @var \im\search\components\SearchManager $searchManager */ $searchManager = Yii::$app->get('searchManager'); $modelClass = $relation->modelClass; $searchableType = $searchManager->getSearchableTypeByClass($modelClass); if (!$searchableType) { $reflector = new ReflectionClass($modelClass); /** @var SearchableInterface $searchableType */ $searchableType = Yii::createObject(['class' => 'im\\search\\components\\service\\db\\SearchableType', 'type' => Inflector::camel2id($reflector->getShortName(), '_'), 'modelClass' => $modelClass]); } $searchableAttributes[] = new AttributeDescriptor(['name' => Inflector::variablize($name), 'label' => Inflector::titleize($label), 'value' => function ($model) use($relation) { return $relation; }, 'type' => $searchableType->getType()]); if ($recursive) { foreach ($searchableType->getSearchableAttributes(false) as $attribute) { $attribute->label = Inflector::titleize($name) . ' >> ' . $attribute->label; $attribute->name = Inflector::variablize($label) . '.' . $attribute->name; $searchableAttributes[] = $attribute; } } return $searchableAttributes; }
/** * Get import data * * @return array */ protected function getImportData() { $result = []; $files = new \DirectoryIterator($this->folderPath); // Collect files data from `folderPath` foreach ($files as $fileInfo) { if ($fileInfo->isDot()) { continue; } // If class name not null, get only one file from `folderPath` if (!empty($this->table)) { $fileName = Inflector::variablize($this->table) . '.' . $this->fileExt; if ($fileInfo->getBasename() === $fileName) { $fileContent = file_get_contents($fileInfo->getRealPath()); $result[] = Json::decode($fileContent); break; } } else { $fileContent = file_get_contents($fileInfo->getRealPath()); $result[] = Json::decode($fileContent); } } return $result; }
/** * Get file name with folder path * * @return string */ public function getFullFileName() { $model = $this->owner; return $this->folderPath . DIRECTORY_SEPARATOR . Inflector::variablize($model::tableName()) . '.' . $this->fileExt; }
<?php use yii\helpers\Inflector; use yii\helpers\StringHelper; /* @var $this yii\web\View */ /* @var $generator app\modules\gii\giiTemplates\crud\Generator */ $urlParams = $generator->generateUrlParams(); $modelVariable = Inflector::variablize(StringHelper::basename($generator->modelClass)); echo "<?php\n"; ?> use yii\helpers\Html; use yii\widgets\DetailView; /* @var $this yii\web\View */ /* @var $<?php echo $modelVariable; ?> <?php echo ltrim($generator->modelClass, '\\'); ?> */ $this->title = $<?php echo $modelVariable; ?> -><?php echo $generator->getNameAttribute(); ?> ; $this->params['breadcrumbs'][] = ['label' => <?php
echo 'condition' == $column->name ? Inflector::camelize('My ' . $column->name) : Inflector::camelize($column->name); ?> ($<?php echo Inflector::variablize($column->name); ?> ) { $this->andWhere([ 'like', <?php echo $modelFullClassName; ?> ::tableName() . '.[[<?php echo $column->name; ?> ]]', $<?php echo Inflector::variablize($column->name); ?> . '%', false ]); return $this; } <?php } } ?> /** * @inheritdoc * @return <?php echo $modelFullClassName; ?>
* @param int|bool $', $attributeArg, ' * @return $this */ public function ', $methodName, '($', $attributeArg, ' = true) { return $this->andWhere([$this->a(\'', $attribute, '\') => $', $attributeArg, ' ? 1 : 0]); } '; } } } // ...expires_at foreach ($tableSchema->columns as $column) { $attribute = $column->name; if (preg_match('~(?:^|_)expires_at$~', $attribute) && ($column->getIsDate() || $column->getIsDatetime())) { $attributeArg = Inflector::variablize(str_replace('expires_at', 'not_expired', $attribute)); $methodName = $attributeArg; if (!in_array($methodName, $methods)) { $methods[] = $methodName; echo ' /** * @param bool $', $attributeArg, ' * @return $this */ public function ', $methodName, '($', $attributeArg, ' = true) { '; $functionName = $column->getIsDate() ? 'CURDATE' : 'NOW'; if ($column->allowNull) { echo ' $columnName = $this->a(\'', $attribute, '\'); if ($', $attributeArg, ') {
($value) { $this->attributes['<?php echo $fieldName; ?> '] = $value; return $this; } /** * @return <?php echo $fieldName; ?> attribute value. */ public function <?php echo Inflector::variablize("get" . ucfirst($fieldName)); ?> () { return $this->attributes['<?php echo $fieldName; ?> ']; } <?php } ?> }
/** * @inheritdoc */ public function run() { echo Html::endForm(); $id = $this->options['id']; $builderId = $this->builder->getId(); $view = $this->getView(); if ($this->rules) { $rules = Json::encode($this->rules); $view->registerJs("\$('#{$builderId}').queryBuilder('setRules', {$rules});"); } $frm = Inflector::variablize("frm-{$id}-querybuilder"); $btn = Inflector::variablize("btn-{$id}-querybuilder-reset"); $view->registerJs("var {$frm} = \$('#{$id}');"); $view->registerJs(<<<JS var {$btn} = {$frm}.find('button:reset:first'); if ({$btn}.length){ {$btn}.on('click', function(){ \$('#{$builderId}').queryBuilder('reset'); }); } JS ); $view->registerJs(<<<JS {$frm}.on('submit', function(){ var rules = \$('#{$builderId}').queryBuilder('getRules'); if (\$.isEmptyObject(rules)) { return false; } else { var input = \$(this).find("input[name='{$this->rulesParam}']:first"); input.val(JSON.stringify(rules)); } }); JS ); }
public function getVarName() { $string = implode('_', ['chart', $this->type, $this->id]); return Inflector::variablize($string); }
public function viewHelper($context, $name = null) { if ($name !== null && isset($context['this'])) { $this->call($context['this'], Inflector::variablize($name)); } }
/** * @inheritdoc */ public function init() { static::$instance = $this; $this->_varName = Inflector::variablize($this->name); $this->requires[] = 'ngRoute'; if (!empty($this->resources)) { $this->requires[] = 'ngResource'; } $this->requires = array_unique($this->requires); }
/** * Create a variable based of user input. * * @param string $prefix * @param string $typeCast 'var', 'cfg' * @return array */ private function varCreator($prefix, $typeCast) { $this->output(PHP_EOL . '-> Create new ' . $prefix, Console::FG_YELLOW); $name = $this->prompt('Variable Name:', ['required' => true]); $label = $this->prompt('End-User Label:', ['required' => true]); $type = $this->select('Variable Type:', $this->getVariableTypes()); $v = ['var' => Inflector::variablize($name), 'label' => $label, 'type' => 'zaa-' . $type]; if ($this->hasVariableTypeOption($type)) { $v['options'] = $this->getVariableTypeOption($type); } if ($typeCast == 'var') { $func = 'getVarValue'; } else { $func = 'getCfgValue'; } $extra = $this->getExtraVarDef($type, $v['var'], $func); if ($extra !== false) { $this->phpdoc[] = '{{extras.' . $v['var'] . '}}'; $this->viewFileDoc[] = '$this->extraValue(\'' . $v['var'] . '\');'; $this->extras[] = $extra; } $this->output('Added ' . $prefix . PHP_EOL, Console::FG_GREEN); return $v; }
public function testVariablize() { $this->assertEquals("customerTable", Inflector::variablize('customer_table')); }