public function testUnderscore() { $result = Inflector::underscore('BankAccount'); $this->assertEquals('bank_account', $result); $result = Inflector::underscore('PHPDeveloper'); $this->assertEquals('php_developer', $result); $result = Inflector::underscore('ICountable'); $this->assertEquals('i_countable', $result); }
/** * Handles Model::findBy* Model::findAll* magic * * @param string $method * @param string $args * @return mixed * @throws \mongo_model\Exception */ public static function __callStatic($method, $args) { if (strpos($method, 'findBy') === 0) { $key = Inflector::underscore(substr($method, 6)); return static::findFirst(array('conditions' => array($key => $args[0]))); } if (strpos($method, 'findAllBy') === 0) { $key = Inflector::underscore(substr($method, 9)); return static::findAll(array('conditions' => array($key => $args[0]))); } throw new Exception('Undefined method ' . get_called_class() . '::' . $method . '()'); }
public function __call($method, $args) { if (strpos($method, 'set') === 0) { $key = Inflector::underscore(substr($method, 3)); return $this->setReference($key, $args[0]); } $key = Inflector::underscore($method); if (in_array($key, static::$_references)) { return $this->getReference($key); } throw new Exception('Undefined method ' . get_called_class() . '::' . $method . '()'); }
public function generate($name, $colDefs = array()) { $name = Inflector::underscore($name); $version = gmdate('YmdHis'); foreach (glob(ROX_APP_PATH . '/config/migrations/*.php') as $file) { if (preg_match("/([0-9]+)_{$name}.php/", $file) == 1) { throw new Exception("A migration named {$name} already exists"); } } $type = static::inferMigrationType($name); $class = Inflector::camelize($name); $table = static::inferTableName($name); $columns = static::parseColumnDefinitions($colDefs); $indexes = static::extractIndexes($columns); $vars = compact('type', 'class', 'table', 'columns', 'indexes'); $data = $this->_renderTemplate('migration', $vars, true); $this->_writeFile("/config/migrations/{$version}_{$name}.php", $data); }
public function generate($name) { $vars = array('controller_name' => Inflector::tableize($name), 'controller_class' => Inflector::camelize(Inflector::tableize($name) . '_controller'), 'model_name' => Inflector::underscore(Inflector::singularize($name)), 'model_class' => Inflector::classify($name), 'model_var_name' => Inflector::lowerCamelize(Inflector::classify($name)), 'model_var_plural_name' => Inflector::lowerCamelize(Inflector::tableize($name)), 'friendly_model_name' => Inflector::humanize(Inflector::singularize($name)), 'friendly_controller_name' => Inflector::humanize(Inflector::tableize($name)), 'package_name' => 'App', 'year' => date('Y')); $data = $this->_renderTemplate('controller', $vars); $this->_writeFile('/controllers/' . $vars['controller_class'] . '.php', $data); }
/** * Returns the controller name for a given ActiveModel instance. * * @param ActiveModel $object * @return string */ protected static function _controllerNameFromModel(ActiveModel $object) { static $results = array(); $class = get_class($object); if (!isset($results[$class])) { $results[$class] = Inflector::underscore(Inflector::pluralize($class)); } return $results[$class]; }
/** * undocumented function * * @return array */ protected static function _normalizeAssociations() { $associations = array('belongs_to' => array(), 'has_many' => array(), 'has_one' => array()); $rels = array('belongs_to' => '_belongsTo', 'has_many' => '_hasMany', 'has_one' => '_hasOne'); foreach ($rels as $type => $property) { foreach (static::$$property as $name => $options) { if (is_int($name) && is_string($options)) { $name = $options; $options = array(); } $keyClass = ($type == 'belongs_to') ? $name : get_called_class(); $defaults = array( 'class' => Inflector::classify($name), 'key' => Inflector::underscore($keyClass) . '_id' ); $options += $defaults; $associations[$type][$name] = $options; } } return $associations; }
protected function _generateMigration($name) { $name = Inflector::underscore($name); foreach (glob(ROX_APP_PATH . '/config/migrations/*.php') as $file) { if (preg_match("/([0-9]+)_{$name}.php/", $file) == 1) { throw new Exception("A migration named {$name} already exists"); } } $version = gmdate('YmdHis'); $data = $this->_renderTemplate('migration', array( 'class_name' => Inflector::camelize($name), 'year' => date('Y') )); $this->_writeFile("/config/migrations/{$version}_{$name}.php", $data); }
public function setModel($model) { $validModel = is_string($model) || is_object($model) && $model instanceof ActiveModel; if (!$validModel) { throw new Exception('Model should be string or a \\rox\\ActiveModel'); } $modelName = Inflector::underscore(is_object($model) ? get_class($model) : $model); $this->_currentModel = $modelName; if (is_object($model)) { $this->_data = array_merge($this->_data, array($modelName => $model->getData())); $this->_validationErrors[$modelName] = $model->getValidationErrors(); } }