/** * Returns the currently requested sort information. * @param boolean $recalculate whether to recalculate the sort directions * @return array sort directions indexed by attribute names. * Sort direction can be either `SORT_ASC` for ascending order or * `SORT_DESC` for descending order. */ public function getAttributeOrders($recalculate = false) { if ($this->_attributeOrders === null || $recalculate) { $this->_attributeOrders = []; if (($params = $this->params) === null) { $request = Yii::$app->getRequest(); $params = $request instanceof Request ? $request->getQueryParams() : []; } if (isset($params[$this->sortParam]) && is_scalar($params[$this->sortParam])) { $attributes = explode($this->separator, $params[$this->sortParam]); foreach ($attributes as $attribute) { $descending = false; if (strncmp($attribute, '-', 1) === 0) { $descending = true; $attribute = substr($attribute, 1); } $attribute = Inflector::underscore($attribute); if (isset($this->attributes[$attribute])) { $this->_attributeOrders[$attribute] = $descending ? SORT_DESC : SORT_ASC; if (!$this->enableMultiSort) { return $this->_attributeOrders; } } } } if (empty($this->_attributeOrders) && is_array($this->defaultOrder)) { $this->_attributeOrders = $this->defaultOrder; } } return $this->_attributeOrders; }
/** * @inheritdoc */ public function init() { parent::init(); $inputName = explode('_', $this->attribute); if (count($inputName) > 1) { $inputName[0] = $inputName[1]; } $this->inputOptions = array_merge(['data-braintree-name' => \yii\helpers\Inflector::underscore($inputName[0]), 'autocomplete' => 'off'], $this->inputOptions); }
/** * Composes stream context options from raw request options. * @param array $options raw request options. * @return array stream context options. */ private function composeContextOptions(array $options) { $contextOptions = []; foreach ($options as $key => $value) { $section = 'http'; if (strpos($key, 'ssl') === 0) { $section = 'ssl'; $key = substr($key, 3); } $key = Inflector::underscore($key); $contextOptions[$section][$key] = $value; } return $contextOptions; }
/** * Converts the public variables to js configuration variables * @return string */ protected function variablize() { $vars = []; $class = new \ReflectionClass(Widget::className()); foreach ($class->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) { if (!$property->isStatic()) { $name = $property->getName(); if (!empty($this->{$name})) { $vars[] = 'var hc_' . Inflector::underscore($name) . '=' . ($name == 'disableMobile' ? $this->{$name} : '"' . $this->{$name} . '"') . ';'; } } } return implode("\n\t", $vars); }
/** * 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) { $fieldName = Inflector::underscore($name); if (isset($attributes[$fieldName])) { $this->{$fieldName} = $value; } elseif (array_key_exists($name, $this->getModelRelations())) { $this->populateRelation($name, $value); } elseif ($safeOnly) { $this->onUnsafeAttribute($name, $value); } } } }
/** * Converts the model into an options array. * @return array the array representation of the object */ public function toOptions() { $options = []; foreach ($this as $prop => $value) { $key = strpos($prop, "on") !== 0 ? Inflector::underscore($prop) : $prop; if ($value instanceof Optionable) { $value = $value->toOptions(); } elseif (is_array($value)) { foreach ($value as $k => $v) { if ($v instanceof Optionable) { $value[$k] = $v->toOptions(); } } } if (!is_array($value) && $value !== null || !empty($value)) { $options[$key] = $value; } } return $options; }
/** * Get ID interface * @return string */ public function getId() { if ($this->id === null) { $this->setId(Inflector::underscore((new \ReflectionClass(__CLASS__))->getShortName()) . '-' . Inflector::underscore(Inflector::id2camel(Yii::$app->controller->action->getUniqueId(), '/')) . ($this->isUniqueId() ? '-' . uniqid() : '')); } return $this->id; }
public function actionCreate() { $module = $this->prompt('Module Name (e.g. galleryadmin):'); $modulePre = $new_str = preg_replace('/admin$/', '', $module); $modelName = $this->prompt('Model Name (e.g. Album)'); $apiEndpoint = $this->prompt('Api Endpoint (e.g. api-' . $modulePre . '-' . strtolower($modelName) . ')'); $sqlTable = $this->prompt('Database Table name (e.g. ' . strtolower($modulePre) . '_' . Inflector::underscore($modelName) . ')'); if (!$this->confirm("Create '{$modelName}' controller, api & model based on sql table '{$sqlTable}' in module '{$module}' for api endpoint '{$apiEndpoint}'?")) { return $this->outputError('Crud creation aborted.'); } $shema = Yii::$app->db->getTableSchema($sqlTable); if (!$shema) { return $this->outputError("Could not read informations from database table '{$sqlTable}', table does not exist."); } $yiiModule = Yii::$app->getModule($module); $basePath = $yiiModule->basePath; $ns = $yiiModule->getNamespace(); $modelName = ucfirst($modelName); $fileName = ucfirst(strtolower($modelName)); $modelNs = '\\' . $ns . '\\models\\' . $modelName; $data = ['api' => ['folder' => 'apis', 'ns' => $ns . '\\apis', 'file' => $fileName . 'Controller.php', 'class' => $fileName . 'Controller', 'route' => strtolower($module) . '-' . strtolower($modelName) . '-index'], 'controller' => ['folder' => 'controllers', 'ns' => $ns . '\\controllers', 'file' => $fileName . 'Controller.php', 'class' => $fileName . 'Controller'], 'model' => ['folder' => 'models', 'ns' => $ns . '\\models', 'file' => $modelName . '.php', 'class' => $modelName]]; $apiClass = null; foreach ($data as $name => $item) { $folder = $basePath . DIRECTORY_SEPARATOR . $item['folder']; if (!file_exists($folder)) { mkdir($folder); } if (file_exists($folder . DIRECTORY_SEPARATOR . $item['file'])) { echo $this->ansiFormat("Can not create {$folder}" . DIRECTORY_SEPARATOR . $item['file'] . ', file does already exists!', Console::FG_RED) . PHP_EOL; } else { $content = '<?php' . PHP_EOL . PHP_EOL; $content .= 'namespace ' . $item['ns'] . ';' . PHP_EOL . PHP_EOL; switch ($name) { case 'api': $content .= 'class ' . $item['class'] . ' extends \\admin\\ngrest\\base\\Api' . PHP_EOL; $content .= '{' . PHP_EOL; $content .= ' public $modelClass = \'' . $modelNs . '\';' . PHP_EOL; $content .= '}'; break; case 'controller': $content .= 'class ' . $item['class'] . ' extends \\admin\\ngrest\\base\\Controller' . PHP_EOL; $content .= '{' . PHP_EOL; $content .= ' public $modelClass = \'' . $modelNs . '\';' . PHP_EOL; $content .= '}'; break; case 'model': $names = []; $allfields = []; $ngrest = ['text' => [], 'textarea' => []]; foreach ($shema->columns as $k => $v) { $allfields[] = $v->name; if ($v->phpType == 'string') { $names[] = $v->name; if ($v->type == 'text') { $ngrest['textarea'][] = $v->name; } if ($v->type == 'string') { $ngrest['text'][] = $v->name; } } } $content .= 'class ' . $item['class'] . ' extends \\admin\\ngrest\\base\\Model' . PHP_EOL; $content .= '{' . PHP_EOL; $content .= ' /* yii model properties */' . PHP_EOL . PHP_EOL; $content .= ' public static function tableName()' . PHP_EOL; $content .= ' {' . PHP_EOL; $content .= ' return \'' . $sqlTable . '\';' . PHP_EOL; $content .= ' }' . PHP_EOL . PHP_EOL; $content .= ' public function rules()' . PHP_EOL; $content .= ' {' . PHP_EOL; $content .= ' return [' . PHP_EOL; $content .= ' [[\'' . implode("', '", $names) . '\'], \'required\'],' . PHP_EOL; $content .= ' ];' . PHP_EOL; $content .= ' }' . PHP_EOL . PHP_EOL; $content .= ' public function scenarios()' . PHP_EOL; $content .= ' {' . PHP_EOL; $content .= ' return [' . PHP_EOL; $content .= ' \'restcreate\' => [\'' . implode("', '", $allfields) . '\'],' . PHP_EOL; $content .= ' \'restupdate\' => [\'' . implode("', '", $allfields) . '\'],' . PHP_EOL; $content .= ' ];' . PHP_EOL; $content .= ' }' . PHP_EOL . PHP_EOL; $content .= ' /* ngrest model properties */' . PHP_EOL . PHP_EOL; $content .= ' public function genericSearchFields()' . PHP_EOL; $content .= ' {' . PHP_EOL; $content .= ' return [\'' . implode("', '", $names) . '\'];' . PHP_EOL; $content .= ' }' . PHP_EOL . PHP_EOL; $content .= ' public function ngRestApiEndpoint()' . PHP_EOL; $content .= ' {' . PHP_EOL; $content .= ' return \'' . $apiEndpoint . '\';' . PHP_EOL; $content .= ' }' . PHP_EOL . PHP_EOL; $content .= ' public function ngRestConfig($config)' . PHP_EOL; $content .= ' {' . PHP_EOL; foreach ($ngrest['text'] as $n) { $content .= ' $config->list->field(\'' . $n . '\', \'' . Inflector::humanize($n) . '\')->text();' . PHP_EOL; } foreach ($ngrest['textarea'] as $n) { $content .= ' $config->list->field(\'' . $n . '\', \'' . Inflector::humanize($n) . '\')->textarea();' . PHP_EOL; } $content .= ' $config->create->copyFrom(\'list\');' . PHP_EOL; $content .= ' $config->update->copyFrom(\'list\');' . PHP_EOL; $content .= ' return $config;' . PHP_EOL; $content .= ' }' . PHP_EOL; $content .= '}'; break; } if (file_put_contents($folder . DIRECTORY_SEPARATOR . $item['file'], $content)) { echo $this->ansiFormat('- File ' . $folder . DIRECTORY_SEPARATOR . $item['file'] . ' created.', Console::FG_GREEN) . PHP_EOL; } } } $getMenu = 'public function getMenu() { return $this->node(\'' . Inflector::humanize($modelName) . '\', \'http://materializecss.com/icons.html\') ->group(\'GROUP\') ->itemApi(\'' . Inflector::humanize($modelName) . '\', \'' . $data['api']['route'] . '\', \'http://materializecss.com/icons.html\', \'' . $apiEndpoint . '\') ->menu(); } '; $mname = $this->ansiFormat($basePath . '/Module.php', Console::BOLD); $a = $this->ansiFormat('$apis', Console::BOLD); echo PHP_EOL . 'Modify the ' . $a . ' var in ' . $mname . ' like below:' . PHP_EOL . PHP_EOL; echo $this->ansiFormat('public $apis = [ \'' . $apiEndpoint . '\' => \'' . $data['api']['ns'] . '\\' . $data['api']['class'] . '\', ];', Console::FG_YELLOW); echo PHP_EOL . PHP_EOL . 'Update the getMenu() method like below:' . PHP_EOL . PHP_EOL; echo $this->ansiFormat($getMenu, Console::FG_YELLOW); echo PHP_EOL; return 0; }
public function testUnderscore() { $this->assertEquals("me_my_self_and_i", Inflector::underscore('MeMySelfAndI')); }
public function init() { parent::init(); // Prepare Filename $this->filename = Inflector::underscore(Tools::className($this)) . '_' . date('Y-m-d'); }
public function actionDownload($id) { $model = $this->findModel($id); header("Content-type: text/plain"); header("Content-Disposition: attachment; filename=" . \yii\helpers\Inflector::underscore($model->title) . ".txt"); print $model->content; }
/** * Returns the routing key of message (or it can be a queue name) * By default this method generate routing key based on the class name. * Example: class 'TestReasonMessage' --> routing key 'mq_test_reason' * @return string */ public function routingKey() { return 'mq_' . Inflector::underscore(str_replace('Message', '', StringHelper::basename(get_class($this)))); }
/** * @inheritdoc */ public function generate() { $files = []; if (preg_match('~^((?:\\w+\\\\)*\\w+)\\\\(\\w+|\\*)$~', $this->modelClass, $match)) { $pattern = Yii::getAlias('@' . str_replace('\\', '/', $match[1])) . '/' . $match[2] . '.php'; foreach (glob($pattern) as $filename) { $ns = $match[1]; $modelName = basename($filename, '.php'); /* @var $modelClass string|\yii\boost\db\ActiveRecord */ $modelClass = $ns . '\\' . $modelName; $fixtureNs = $this->fixtureNs; $fixtureName = $modelName; $fixtureClass = $fixtureNs . '\\' . $fixtureName; $baseFixtureName = preg_replace('~^(?:\\w+\\\\)*\\w+\\\\(\\w+)$~', '$1', $this->fixtureBaseClass); $baseFixtureClass = $this->fixtureBaseClass; $dataFile = $this->dataPath . '/' . Inflector::underscore($modelName) . '.php'; /* @var $tableSchema \yii\gii\plus\db\TableSchema */ $tableSchema = $modelClass::getTableSchema(); $params = ['ns' => $ns, 'modelName' => $modelName, 'modelClass' => $modelClass, 'fixtureNs' => $fixtureNs, 'fixtureName' => $fixtureName, 'fixtureClass' => $fixtureClass, 'baseFixtureName' => $baseFixtureName, 'baseFixtureClass' => $baseFixtureClass, 'dataFile' => $dataFile, 'tableSchema' => $tableSchema]; if (!$tableSchema->isView && !$tableSchema->isStatic) { // fixture $path = Yii::getAlias('@' . str_replace('\\', '/', $fixtureNs)) . '/' . $fixtureName . '.php'; $files[] = new CodeFile($path, $this->render('fixture.php', $params)); // data-file if ($this->generateDataFile) { $path = Yii::getAlias($dataFile); $files[] = new CodeFile($path, $this->render('data-file.php', $params)); } } } } return $files; }
/** * Generate a suggestion for the database table name. * * @return string The database table suggestion. */ public function getDatabaseNameSuggestion() { return strtolower($this->getModuleNameWithoutAdminSuffix() . '_' . Inflector::underscore($this->modelName)); }