/**
  * Converts the given string to `spinal-case`
  * @param string $string
  * @return string
  */
 public static function spinalCase($string)
 {
     /** RegExp source http://stackoverflow.com/a/1993772 */
     preg_match_all('/([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)/', $string, $matches);
     $matches = $matches[0];
     foreach ($matches as &$match) {
         $match = $match == Strings::upper($match) ? Strings::lower($match) : Strings::firstLower($match);
     }
     return implode('-', $matches);
 }
Exemple #2
0
 /**
  * Converts the given string to "camelCase".
  * @param string $string
  * @return string
  */
 public static function toCamelCase($string)
 {
     static $canUse = null;
     if (is_null($canUse)) {
         $canUse = method_exists(Strings::class, 'firstLower');
         // Nette/Utils >= 2.3 only
     }
     $pascal = self::toPascalCase($string);
     return $canUse ? Strings::firstLower($pascal) : Strings::lower(Strings::substring($pascal, 0, 1)) . Strings::substring($pascal, 1);
 }
Exemple #3
0
 /**
  * @param string $name String with * in it
  * @param string $substitution
  * @param string $suffix
  * @return string
  */
 public static function substituteMethodWildcard($name, $substitution, $suffix = '')
 {
     $substitution = self::underscoreToCamel($substitution);
     if (Strings::startsWith($name, '*')) {
         $substitution = Strings::firstLower($substitution);
     }
     // Plural
     if (Strings::contains($name, '*s')) {
         switch (Strings::substring($substitution, -1)) {
             case 's':
                 // nothing
                 break;
             case 'y':
                 $substitution = Strings::replace($substitution, '#y$#', 'ies');
                 break;
             default:
                 $substitution .= 's';
                 break;
         }
         return Strings::replace($name, '#\\*s#', $substitution . $suffix);
     } else {
         return Strings::replace($name, '#\\*#', $substitution . $suffix);
     }
 }
Exemple #4
0
 /**
  * @param string $tableName
  * @param array $columns
  * @return void
  */
 protected function generateTableGeneratedHyperRow($tableName, $columns)
 {
     $classFqn = $this->config['classes']['row']['generated'];
     $classFqn = Helpers::substituteClassWildcard($classFqn, $tableName);
     $className = Helpers::extractClassName($classFqn);
     $classNamespace = Helpers::extractNamespace($classFqn);
     $extendsFqn = $this->config['classes']['row']['base'];
     $extends = Helpers::formatClassName($extendsFqn, $classNamespace);
     $class = new ClassType($className);
     $class->setExtends($extends);
     // Add property annotations based on columns
     foreach ($columns as $column => $type) {
         if (in_array($type, [IStructure::FIELD_DATETIME, IStructure::FIELD_TIME, IStructure::FIELD_DATE, IStructure::FIELD_UNIX_TIMESTAMP])) {
             $type = '\\Nette\\Utils\\DateTime';
         }
         $class->addComment("@property-read {$type} \${$column}");
     }
     // Generate methods.row.getter
     foreach ((array) $this->config['methods']['row']['getter'] as $methodTemplate) {
         // Generate column getters
         foreach ($columns as $column => $type) {
             if (in_array($type, [IStructure::FIELD_DATETIME, IStructure::FIELD_TIME, IStructure::FIELD_DATE, IStructure::FIELD_UNIX_TIMESTAMP])) {
                 $type = '\\Nette\\Utils\\DateTime';
             }
             $methodName = Helpers::substituteMethodWildcard($methodTemplate, $column);
             $returnType = $type;
             $class->addMethod($methodName)->addBody('return $this->activeRow->?;', [$column])->addComment("@return {$returnType}");
             // Add property annotation
             if (Strings::startsWith($methodName, 'get')) {
                 $property = Strings::firstLower(Strings::substring($methodName, 3));
                 if ($property != $column) {
                     $class->addComment("@property-read {$type} \${$property}");
                 }
             }
         }
     }
     // Generate methods.row.ref
     foreach ((array) $this->config['methods']['row']['ref'] as $methodTemplate) {
         // Generate 'ref' methods
         foreach ($this->structure->getBelongsToReference($tableName) as $referencingColumn => $referencedTable) {
             if (is_array($this->config['tables']) && !in_array($referencedTable, $this->config['tables'])) {
                 continue;
             }
             $result = Helpers::underscoreToCamelWithoutPrefix(Strings::replace($referencingColumn, '~_id$~'), $tableName);
             $methodName = Helpers::substituteMethodWildcard($methodTemplate, $result);
             $returnType = $this->getTableClass('row', $referencedTable, $classNamespace);
             $class->addMethod($methodName)->addBody('return $this->ref(?, ?);', [$referencedTable, $referencingColumn])->addComment("@return {$returnType}");
             // Add property annotations
             if (Strings::startsWith($methodName, 'get')) {
                 $property = Strings::firstLower(Strings::substring($methodName, 3));
                 $class->addComment("@property-read {$returnType} \${$property}");
             }
         }
     }
     // Generate methods.row.related
     foreach ((array) $this->config['methods']['row']['related'] as $methodTemplate) {
         // Generate 'related' methods
         foreach ($this->structure->getHasManyReference($tableName) as $relatedTable => $referencingColumns) {
             if (is_array($this->config['tables']) && !in_array($relatedTable, $this->config['tables'])) {
                 continue;
             }
             foreach ($referencingColumns as $referencingColumn) {
                 // Omit longest common prefix between $relatedTable and (this) $tableName
                 $result = Helpers::underscoreToCamelWithoutPrefix($relatedTable, $tableName);
                 if (count($referencingColumns) > 1) {
                     $suffix = 'As' . Helpers::underscoreToCamel(Strings::replace($referencingColumn, '~_id$~'));
                 } else {
                     $suffix = NULL;
                 }
                 $methodName = Helpers::substituteMethodWildcard($methodTemplate, $result, $suffix);
                 $returnType = $this->getTableClass('selection', $relatedTable, $classNamespace);
                 $class->addMethod($methodName)->addBody('return $this->related(?, ?);', [$relatedTable, $referencingColumn])->addComment("@return {$returnType}");
                 // Add property annotations
                 if (Strings::startsWith($methodName, 'get')) {
                     $property = Strings::firstLower(Strings::substring($methodName, 3));
                     $class->addComment("@property-read {$returnType} \${$property}");
                 }
             }
         }
     }
     $code = implode("\n\n", ['<?php', "/**\n * This is a generated file. DO NOT EDIT. It will be overwritten.\n */", "namespace {$classNamespace};", $class]);
     $dir = $this->config['dir'] . '/' . 'tables' . '/' . $tableName;
     $file = $dir . '/' . $className . '.php';
     $this->writeIfChanged($file, $code);
 }
Exemple #5
0
 public static function getModuleFromRequest(Nette\Application\Request $request, $outputModuleDelimiter = ':')
 {
     $presenterArr = explode(':', $request->getPresenterName());
     array_pop($presenterArr);
     foreach ($presenterArr as $k => $v) {
         $presenterArr[$k] = Nette\Utils\Strings::firstLower($v);
     }
     $module = implode($outputModuleDelimiter, $presenterArr);
     return $module;
 }