function classify($class_name, $singularize = false) { if ($singularize) { $class_name = Utils::singularize($class_name); } $class_name = Inflector::instance()->camelize($class_name); return ucfirst($class_name); }
public function test_singularize() { $this->assert_equals('order_status', AR\Utils::singularize('order_status')); $this->assert_equals('order_status', AR\Utils::singularize('order_statuses')); $this->assert_equals('os_type', AR\Utils::singularize('os_type')); $this->assert_equals('os_type', AR\Utils::singularize('os_types')); $this->assert_equals('photo', AR\Utils::singularize('photos')); $this->assert_equals('pass', AR\Utils::singularize('pass')); $this->assert_equals('pass', AR\Utils::singularize('passes')); }
function classify($class_name, $singularize = false) { if ($singularize) { $parts = explode('_', Inflector::instance()->uncamelize($class_name)); $class_name = ''; foreach ($parts as $name) { $class_name .= '_' . Utils::singularize($name); } $class_name = ltrim($class_name, '_'); } $class_name = Inflector::instance()->camelize($class_name); return ucfirst($class_name); }
public function error_messages_for($obj, $obj_name = "", $obj_prefix = "") { if ($obj->errors && $obj->errors->size()) { if ($obj_name == "") { $obj_name = \ActiveRecord\Utils::singularize(strtolower(get_class($obj))); } if ($obj_prefix == "") { if (\ActiveRecord\Utils::pluralize_if(2, $obj_name) == "2 " . $obj_name) { $obj_prefix = "these"; } else { $obj_prefix = "this"; } } $html = "<p>" . "<div class=\"flash-error\">" . "<strong>" . $obj->errors->size() . " " . h(\ActiveRecord\Utils::pluralize_if($obj->errors->size(), "error")) . " prohibited " . raw_or_h($obj_prefix) . " " . raw_or_h($obj_name) . " from being " . ($obj->is_new_record() ? "created" : "saved") . ":</strong><br />\n"; foreach ($obj->errors as $err) { $html .= raw_or_h($err) . "<br />"; } $html .= "</div>" . "</p>"; return $html; } }
/** * Get the name of the model that fits AR relational convention * * @param Table $table Optionally injected Table instance * @param boolean $quoted Whether or not to quote escape the string with SQL-style quotes * @static * @access public * @return string */ public static function getConventionalRelationName(Table $table = null, $quoted = false) { $table = $table ?: static::table(); $table_name = $table->get_fully_qualified_table_name($quoted); return ARUtils::singularize($table_name); }
public function render($template, $vars = array()) { /* render(array("partial" => "somedir/file"), array("v" => $v)) */ if (!is_array($template)) { $template = array("action" => $template); } if (isset($template["status"])) { Request::send_status_header($template["status"]); } $collection = array(); if (isset($template["collection"]) && is_array($template["collection"])) { if (isset($template["as"])) { $collection = array($template["as"] => $template["collection"]); } else { /* figure out the type of things in the collection */ $cl = strtolower(get_class($template["collection"][0])); if ($cl != "") { $cl = \ActiveRecord\Utils::singularize($cl); } if ($cl == "") { throw new HalfMoonException("could not figure out type of " . "collection"); } $collection = array($cl => $template["collection"]); } } /* just render text with no layout */ if (is_array($template) && array_key_exists("text", $template)) { if (!$this->content_type_set()) { $this->content_type = "text/plain"; } if (Config::log_level_at_least("full")) { Log::info("Rendering text"); } print $template["text"]; } elseif (is_array($template) && array_key_exists("html", $template)) { if (!$this->content_type_set()) { $this->content_type = "text/html"; } if (Config::log_level_at_least("full")) { Log::info("Rendering HTML"); } print $template["html"]; } elseif (is_array($template) && array_key_exists("json", $template)) { if (!$this->content_type_set()) { $this->content_type = "application/json"; } if (Config::log_level_at_least("full")) { Log::info("Rendering json"); } /* there's no way to know if we were passed a json-encoded string, * or a string that needs to be encoded, so just encode everything * and hope the user figures it out */ print json_encode($template["json"]); } elseif (is_array($template) && array_key_exists("js", $template)) { if (!$this->content_type_set()) { $this->content_type = "text/javascript"; } if (Config::log_level_at_least("full")) { Log::info("Rendering javascript"); } print $template["js"]; } else { $tf = ""; /* render a partial template */ if (is_array($template) && isset($template["partial"])) { $tf = $template["partial"]; } elseif (is_array($template) && isset($template["action"])) { $tf = $template["action"]; } elseif (is_array($template)) { $tf = join("", array_values($template)); } else { $tf = $template; } if (substr($tf, 0, 1) == "/") { /* full path, just use it */ } elseif (strpos($tf, "/") !== false) { /* path relative to base view path */ $tf = HALFMOON_ROOT . "/views/" . $tf; } else { /* just a file in this controller's directory * (AdminSomethingController -> admin_something) */ $tf = $this->view_template_path() . $tf; } /* partial template files start with _ */ if (is_array($template) && isset($template["partial"])) { $tf = dirname($tf) . "/_" . basename($tf); } /* do the actual renders */ $filename = null; /* regular php/html */ if (file_exists($filename = $tf . ".phtml")) { if (!$this->content_type_set()) { $this->content_type = "text/html"; } } elseif (file_exists($filename = $tf . ".pxml")) { if (!$this->content_type_set()) { $this->content_type = "application/xml"; } } elseif (file_exists($filename = $tf . ".pjs")) { if (!$this->content_type_set()) { $this->content_type = "text/javascript"; } } else { throw new MissingTemplate("no template file " . $tf . ".p{html,xml,js}"); } if (count($collection)) { $ck = Utils::A(array_keys($collection), 0); /* it would be nice to be able to just read the template * into a string and eval() it each time to save on i/o, * but php won't let us catch parse errors properly and * there may be some other fallout */ foreach ($collection[$ck] as $cobj) { $vars[$ck] = $cobj; $this->_really_render_file($filename, $vars); } } else { $this->_really_render_file($filename, $vars); } } if (!$this->in_view) { if (is_array($template) && array_key_exists("layout", $template)) { $this::$layout = $template["layout"]; } elseif ($this->content_type_set() && $this->content_type != static::$DEFAULT_CONTENT_TYPE) { /* if we were called from the controller, we're not outputting * html, and no layout was explicitly specified, we probably * don't want a layout */ $this::$layout = false; } } $this->did_render = true; }
public function testSingularize() { $utils = new Utils(); $this->assertEquals('order_status', $utils->singularize('order_status')); $this->assertEquals('order_status', $utils->singularize('order_statuses')); $this->assertEquals('os_type', $utils->singularize('os_type')); $this->assertEquals('os_type', $utils->singularize('os_types')); $this->assertEquals('photo', $utils->singularize('photos')); $this->assertEquals('pass', $utils->singularize('pass')); $this->assertEquals('pass', $utils->singularize('passes')); }