public function testCamelizeLower() { // most common scenarios (underscore => camelize) $this->assertEquals('derek', Mad_Support_Inflector::camelize('derek', 'lower')); $this->assertEquals('dereksTest', Mad_Support_Inflector::camelize('dereks_test', 'lower')); $this->assertEquals('dereks/test', Mad_Support_Inflector::camelize('dereks/test', 'lower')); $this->assertEquals('dereksName/test', Mad_Support_Inflector::camelize('dereks_name/test', 'lower')); // not as common (already camelized) $this->assertEquals('derek', Mad_Support_Inflector::camelize('Derek', 'lower')); $this->assertEquals('dereksTest', Mad_Support_Inflector::camelize('dereksTest', 'lower')); $this->assertEquals('dereksTest', Mad_Support_Inflector::camelize('DereksTest', 'lower')); $this->assertEquals('dereks/test', Mad_Support_Inflector::camelize('Dereks_Test', 'lower')); }
/** * Construct association object * * @param string $assocName * @param array $options * @param object $model */ public function __construct($assocName, $options, Mad_Model_Base $model) { $valid = array('className', 'foreignKey', 'primaryKey', 'include', 'order', 'dependent' => 'nullify'); $this->_options = Mad_Support_Base::assertValidKeys($options, $valid); $this->_assocName = $assocName; $this->_model = $model; $this->_conn = $model->connection(); // get inflections $toMethod = Mad_Support_Inflector::camelize($this->_assocName, 'lower'); $toMethod = str_replace('/', '_', $toMethod); $toClass = ucfirst($toMethod); $this->_methods = array($toMethod => 'getObject', $toMethod . '=' => 'setObject', 'build' . $toClass => 'buildObject', 'create' . $toClass => 'createObject'); }
/** * Construct association object * * @param string $assocName * @param array $options * @param object $model */ public function __construct($assocName, $options, Mad_Model_Base $model) { $valid = array('className', 'conditions', 'order', 'foreignKey', 'primaryKey', 'associationForeignKey', 'associationPrimaryKey', 'joinTable', 'uniq', 'include', 'finderSql', 'deleteSql', 'insertSql'); $this->_options = Mad_Support_Base::assertValidKeys($options, $valid); $this->_assocName = $assocName; $this->_model = $model; $this->_conn = $model->connection(); // get inflections $toMethod = Mad_Support_Inflector::camelize($this->_assocName, 'lower'); $toMethod = str_replace('/', '_', $toMethod); $singular = Mad_Support_Inflector::singularize($toMethod); $toClass = ucfirst($singular); $this->_methods = array($toMethod => 'getObjects', $toMethod . '=' => 'setObjects', $singular . 'Ids' => 'getObjectIds', $singular . 'Ids=' => 'setObjectIds', $singular . 'Count' => 'getObjectCount', 'add' . $toClass => 'addObject', Mad_Support_Inflector::pluralize('replace' . $toClass) => 'replaceObjects', Mad_Support_Inflector::pluralize('delete' . $toClass) => 'deleteObjects', Mad_Support_Inflector::pluralize('clear' . $toClass) => 'clearObjects', Mad_Support_Inflector::pluralize('find' . $toClass) => 'findObjects'); }
/** * Construct association object * * @param string $assocName * @param array $options */ public function __construct($assocName, $options, Mad_Model_Base $model) { $valid = array('className', 'foreignKey', 'associationForeignKey', 'primaryKey', 'associationPrimaryKey', 'include', 'select', 'conditions', 'order', 'finderSql', 'through', 'dependent' => 'nullify'); $this->_options = Mad_Support_Base::assertValidKeys($options, $valid); $this->_assocName = $assocName; $this->_model = $model; $this->_conn = $model->connection(); // throw fatal error if through option is invalid $this->_throughClass = Mad_Support_Inflector::classify($this->_options['through']); class_exists($this->_throughClass); // get inflections $toMethod = Mad_Support_Inflector::camelize($this->_assocName, 'lower'); $toMethod = str_replace('/', '_', $toMethod); $singular = Mad_Support_Inflector::singularize($toMethod); $toClass = ucfirst($singular); $this->_methods = array($toMethod => 'getObjects', $singular . 'Ids' => 'getObjectIds', $singular . 'Count' => 'getObjectCount', 'add' . $toClass => 'addObject', Mad_Support_Inflector::pluralize('delete' . $toClass) => 'deleteObjects', Mad_Support_Inflector::pluralize('clear' . $toClass) => 'clearObjects', Mad_Support_Inflector::pluralize('find' . $toClass) => 'findObjects'); }
/** * Given a status parameter, determine whether it needs to be converted * to a string. If it is an integer, use the $statusCodes hash to lookup * the default message. If it is a string, build $symbolToStatusCode * and convert it. * * interpret(404) => "404 Not Found" * interpret("notFound") => "404 Not Found" * * Differences from Rails: * - $status is camelized, not underscored. * - an unknown status raises an exception * * @param string|integer Status code or "symbol" * @return string Header */ public static function interpret($status) { // Status from integer or numeric string if (is_numeric($status)) { if (isset(self::$statusCodes[$status])) { return "{$status} " . self::$statusCodes[$status]; } else { $msg = "Unknown status code: {$status}"; throw new InvalidArgumentException($msg); } // Status from string } else { if (is_string($status)) { // Build a string-to-integer lookup for converting a symbol (like // 'created' or 'notImplemented') into its corresponding HTTP status // code (like 200 or 501). static $symbolToStatusCode = array(); if (empty($symbolToStatusCode)) { foreach (self::$statusCodes as $code => $message) { $symbol = Mad_Support_Inflector::camelize($message, $first = 'lower'); $symbolToStatusCode[$symbol] = $code; } } // Convert status symbol to integer code, return header if (isset($symbolToStatusCode[$status])) { return self::interpret($symbolToStatusCode[$status]); } // Error: Status symbol could not be converted to an integer code // Try to help if the developer mixed up underscore/camel $msg = "Unknown status: '{$status}'"; if (strpos($status, '_')) { $status = Mad_Support_Inflector::camelize($status, $first = 'lower'); if (isset($symbolToStatusCode[$status])) { $msg .= " (underscore), did you mean '{$status}' (camel)?"; } } throw new InvalidArgumentException($msg); // Status is an unknown type } else { $msg = '$status must be numeric or string, got ' . gettype($status); throw new InvalidArgumentException($msg); } } }
/** * Construct association object - simple factory * @param string $macro (format|length|numericality|presence|uniqueness) * @param string $attributes * @param array $options */ public static function factory($macro, $attribute, $options) { $className = "Mad_Model_Validation_" . Mad_Support_Inflector::camelize($macro); return new $className($attribute, $options); }
/** * Take the $matchdata returned by a Horde_Routes_Mapper match and add * in :controller and :action that are used by the rest of the framework. * * Format controller names: my_stuff => MyStuffController * Format action names: action_name => actionName * * @param array $matchdata * @return mixed false | array */ public function formatMatchdata($matchdata) { $ret = array(); foreach ($matchdata as $key => $val) { if ($key == 'controller') { $ret['controller'] = $val; $ret[':controller'] = Mad_Support_Inflector::camelize($val) . 'Controller'; } elseif ($key == 'action') { // Horde_Routes_Mapper->resource() and Python Routes are inconsistent // with Rails by using "delete" instead of "destroy". if ($val == 'delete') { $val = 'destroy'; } $ret['action'] = $val; $ret[':action'] = Mad_Support_Inflector::camelize($val, 'lower'); } else { $ret[$key] = $val; } } return !empty($ret) && isset($ret['controller']) ? $ret : false; }
/** * Generate mailer class stubs */ private function _generateMailerStubs() { $name = !empty($this->_args) ? array_shift($this->_args) : null; if (!$name) { $this->_exit("You did not specify the name of the Mailer to generate"); } $name = str_replace('Mailer', '', $name); $mailerName = Mad_Support_Inflector::camelize($name) . 'Mailer'; $this->_tpl->mailerName = $mailerName; $content = $this->_tpl->render('mailer.php'); $this->_createFile(MAD_ROOT . "/app/models/{$mailerName}.php", $content); // CREATE DIRECTORIES $this->_createDir(MAD_ROOT . '/app/views/' . $mailerName); }
/** * Get the method used to declare the of this association * * Example: * <code> * $this->hasMany('Documents'); * // returns 'hasMany' * </code> * * @return string */ public function getMacro() { if (!isset($this->_macro)) { $this->_macro = Mad_Support_Inflector::camelize(str_replace('Mad_Model_Association_', '', get_class($this)), 'lower'); } return $this->_macro; }
/** * Add associations specified via the <tt>:includes</tt> option. * Expects a block that takes as arguments: * +association+ - name of the association * +records+ - the association record(s) to be serialized * +opts+ - options for the association records */ public function addIncludes() { if (isset($this->_options['include'])) { $includeAssociations = (array) $this->_options['include']; unset($this->_options['include']); } if (empty($includeAssociations)) { return; } $baseOnlyOrExcept = array('except' => $this->_options['except'], 'only' => $this->_options['only']); // associative array includes have additional options $includeHasOptions = !is_int(key($includeAssociations)); $associations = $includeHasOptions ? array_keys($includeAssociations) : $includeAssociations; // find records for each association foreach ($associations as $association) { $assoc = $this->_record->reflectOnAssociation($association); $type = $assoc->getMacro(); $method = Mad_Support_Inflector::camelize($association, 'lower'); if ($type == 'hasMany' || $type == 'hasAndBelongsToMany') { $records = $this->_record->{$method}()->getCollection(); } elseif ($type == 'hasOne' || $type == 'belongsTo') { $records = $this->_record->{$method}(); } if ($records === null) { continue; } // options if ($includeHasOptions) { $associationOptions = $includeAssociations[$association]; } else { $associationOptions = $baseOnlyOrExcept; } // sub-records $opts = array_merge($this->_options, $associationOptions); $this->yieldRecords($association, $records, $opts); } $this->_options['include'] = $includeAssociations; }
/** * Construct associations for model from record/row * * @param object $record * @param object $join * @param array $row */ protected function _constructAssociation(Mad_Model_Base $record, Mad_Model_Join_Base $join, $row) { // set that we've loaded this association $record->setAssociationLoaded($join->reflection()->getAssocName()); if ($record->id != $join->parent()->recordId($row) || empty($row[$join->aliasedPrimaryKey()])) { return; } $association = $join->instantiate($row); $macro = $join->reflection()->getMacro(); $singular = Mad_Support_Inflector::singularize($join->reflection()->getAssocName()); if ($macro == 'hasAndBelongsToMany' || $macro == 'hasMany' || $macro == 'hasManyThrough') { $addMethod = Mad_Support_Inflector::camelize('add' . ucfirst($singular), 'lower'); $addMethod = str_replace('/', '_', $addMethod); // make sure object isn't already included $getter = Mad_Support_Inflector::camelize($join->reflection()->getAssocName(), 'lower'); $getter = str_replace('/', '_', $getter); $exists = array(); foreach ($record->{$getter} as $val) { $exists[] = $val->id; } if (!in_array($association->id, $exists)) { $record->{$addMethod}($association); } } elseif ($macro == 'belongsTo' || $macro == 'hasOne') { $assignMethod = Mad_Support_Inflector::camelize($singular, 'lower'); $record->{$assignMethod} = $association; } return $association; }
/** * Set the value for an attribute in this object. * * @param string $name * @param mixed $value */ protected function _setAttribute($name, $value) { // check for writer proxy method $underscore = Mad_Support_Inflector::underscore("set_{$name}"); $methodName = Mad_Support_Inflector::camelize($underscore, 'lower'); if (method_exists($this, $methodName)) { $this->{$methodName}($value); } else { $property = "_{$name}"; if (property_exists($this, $property)) { $this->{$property} = $value; } else { $this->_attrValues[$name] = $value; } } }
/** * Actually return object, and not class * * @param string $migrationName * @param int $version * @return Mad_Model_Migration_Base */ protected function _getMigrationClass($migrationName, $version) { $className = Mad_Support_Inflector::camelize($migrationName); $migration = new $className(); $migration->version = $version; return $migration; }