function cast() { $this->model_name = AkInflector::camelize($this->model_name); $this->model_file_path = AkInflector::toModelFilename($this->model_name); $this->controller_name = empty($this->controller_name) ? $this->model_name : (AkInflector::camelize($this->controller_name)); $this->controller_file_path = AkInflector::toControllerFilename($this->controller_name); $this->controller_class_name = $this->controller_name.'Controller'; $this->controller_human_name = AkInflector::humanize($this->controller_name); $this->helper_var_name = '$'.AkInflector::underscore($this->controller_name).'_helper'; $this->singular_name = AkInflector::underscore($this->model_name); $this->plural_name = AkInflector::pluralize($this->singular_name); $this->singular_controller_name = AkInflector::underscore($this->controller_name); $this->files = array( 'controller.php' => $this->controller_file_path, /** * @todo Implement generic functional tests */ // 'functional_test.php' => AK_TEST_DIR.DS.'functional'.DS.'test_'.$this->controller_class_name.'.php', 'helper.php' => AK_HELPERS_DIR.DS.trim($this->helper_var_name,'$').'.php', 'layout' => AK_VIEWS_DIR.DS.'layouts'.DS.$this->singular_controller_name.'.tpl', 'view_add' => AK_VIEWS_DIR.DS.$this->singular_controller_name.DS.'add.tpl', 'view_destroy' => AK_VIEWS_DIR.DS.$this->singular_controller_name.DS.'destroy.tpl', 'view_edit' => AK_VIEWS_DIR.DS.$this->singular_controller_name.DS.'edit.tpl', 'view_listing' => AK_VIEWS_DIR.DS.$this->singular_controller_name.DS.'listing.tpl', 'view_show' => AK_VIEWS_DIR.DS.$this->singular_controller_name.DS.'show.tpl', 'form' => AK_VIEWS_DIR.DS.$this->singular_controller_name.DS.'_form.tpl', ); $this->user_actions = array(); foreach ((array)@$this->actions as $action){ $this->user_actions[$action] = AK_VIEWS_DIR.DS.$this->singular_controller_name.DS.$action.'.tpl'; } }
function Test_of_pluralize_singular() { foreach ($this->SingularToPlural as $singular => $plural) { $this->assertEqual($plural, AkInflector::pluralize($singular)); $this->assertEqual(ucfirst($plural), AkInflector::pluralize(ucfirst($singular))); } }
function cast() { $this->model_name = AkInflector::camelize($this->model_name); $this->model_file_path = AkInflector::toModelFilename($this->model_name); if (empty($this->actions) && !empty($this->controller_name) && strstr($this->controller_name, ',')) { $this->controller_name = ''; } $this->controller_name = empty($this->controller_name) ? AkInflector::pluralize($this->model_name) : AkInflector::camelize($this->controller_name); $this->controller_file_path = AkInflector::toControllerFilename($this->controller_name); $this->controller_class_name = str_replace(array('/', '::'), '_', $this->controller_name . 'Controller'); $this->controller_name = AkInflector::demodulize($this->controller_name); $this->controller_human_name = AkInflector::humanize($this->controller_name); $this->helper_name = (AkInflector::is_plural($this->controller_name) ? AkInflector::singularize($this->controller_name) : $this->controller_name) . 'Helper'; $this->helper_var_name = '$' . AkInflector::underscore($this->helper_name); $this->singular_name = AkInflector::underscore($this->model_name); $this->plural_name = AkInflector::pluralize($this->singular_name); $this->singular_controller_name = AkInflector::underscore($this->controller_name); $this->module_preffix = AkInflector::underscore(substr($this->controller_class_name, 0, strrpos($this->controller_class_name, '_'))); $this->module_preffix = empty($this->module_preffix) ? '' : DS . $this->module_preffix; $this->files = array('controller.php' => $this->controller_file_path, 'helper.php' => AK_HELPERS_DIR . $this->module_preffix . DS . trim($this->helper_var_name, '$') . '.php', 'layout' => AK_VIEWS_DIR . DS . 'layouts' . DS . $this->singular_controller_name . '.tpl', 'view_add' => AK_VIEWS_DIR . $this->module_preffix . DS . $this->singular_controller_name . DS . 'add.tpl', 'view_destroy' => AK_VIEWS_DIR . $this->module_preffix . DS . $this->singular_controller_name . DS . 'destroy.tpl', 'view_edit' => AK_VIEWS_DIR . $this->module_preffix . DS . $this->singular_controller_name . DS . 'edit.tpl', 'view_listing' => AK_VIEWS_DIR . $this->module_preffix . DS . $this->singular_controller_name . DS . 'listing.tpl', 'view_show' => AK_VIEWS_DIR . $this->module_preffix . DS . $this->singular_controller_name . DS . 'show.tpl', 'form' => AK_VIEWS_DIR . $this->module_preffix . DS . $this->singular_controller_name . DS . '_form.tpl'); $this->user_actions = array(); foreach ((array) @$this->actions as $action) { $this->user_actions[$action] = AK_VIEWS_DIR . $this->module_preffix . DS . $this->singular_controller_name . DS . $action . '.tpl'; } }
function _setupCloner() { $this->clone_setup_done = true; $this->class_to_clone = AkInflector::underscore($this->class_to_clone); $this->class_name = AkInflector::underscore($this->class_name); $this->clone_replacements = array(AkInflector::camelize($this->class_to_clone) . 'Controller' => AkInflector::camelize($this->class_name) . 'Controller', AkInflector::humanize(AkInflector::pluralize($this->class_to_clone)) => AkInflector::humanize(AkInflector::pluralize($this->class_name)), AkInflector::titleize(AkInflector::pluralize($this->class_to_clone)) => AkInflector::titleize(AkInflector::pluralize($this->class_name)), AkInflector::humanize($this->class_to_clone) => AkInflector::humanize($this->class_name), AkInflector::titleize($this->class_to_clone) => AkInflector::titleize($this->class_name), AkInflector::camelize(AkInflector::pluralize($this->class_to_clone)) => AkInflector::camelize(AkInflector::pluralize($this->class_name)), AkInflector::pluralize($this->class_to_clone) => AkInflector::pluralize($this->class_name), AkInflector::camelize($this->class_to_clone) => AkInflector::camelize($this->class_name), $this->class_to_clone => $this->class_name); //AK_VIEWS_DIR.DS.AkInflector::underscore($this->class_name).DS.$action.'.tpl' $this->files_to_clone = array(AkInflector::toModelFilename($this->class_to_clone) => AkInflector::toModelFilename($this->class_name), AK_TEST_DIR . DS . 'unit' . DS . 'test_' . $this->class_to_clone . '.php' => AK_TEST_DIR . DS . 'unit' . DS . 'test_' . $this->class_name . '.php', AK_TEST_DIR . DS . 'fixtures' . DS . AkInflector::tableize($this->class_to_clone) . '.yml' => AK_TEST_DIR . DS . 'fixtures' . DS . AkInflector::tableize($this->class_name) . '.yml', AkInflector::toControllerFilename($this->class_to_clone) => AkInflector::toControllerFilename($this->class_name), AK_TEST_DIR . DS . 'functional' . DS . 'test_' . AkInflector::camelize($this->class_to_clone . '_controller') . '.php' => AK_TEST_DIR . DS . 'functional' . DS . 'test_' . AkInflector::camelize($this->class_name . '_controller') . '.php', AK_HELPERS_DIR . DS . AkInflector::underscore("{$this->class_to_clone}_helper") . '.php' => AK_HELPERS_DIR . DS . AkInflector::underscore("{$this->class_name}_helper") . '.php'); foreach ($this->_getControllerViews() as $view_file) { $this->files_to_clone[AK_VIEWS_DIR . DS . $this->class_to_clone . DS . $view_file . '.tpl'] = AK_VIEWS_DIR . DS . $this->class_name . DS . $view_file . '.tpl'; } $this->files_to_clone[AK_VIEWS_DIR . DS . 'layouts' . DS . $this->class_to_clone . '.tpl'] = AK_VIEWS_DIR . DS . 'layouts' . DS . $this->class_name . '.tpl'; foreach (Ak::dir(AK_APP_DIR . DS . 'locales' . DS . $this->class_to_clone, array('dirs' => false)) as $locale_file) { $this->files_to_clone[AK_APP_DIR . DS . 'locales' . DS . $this->class_to_clone . DS . $locale_file] = AK_APP_DIR . DS . 'locales' . DS . $this->class_name . DS . $locale_file; } }
private function _setDefaults() { $this->class_name = AkInflector::camelize($this->class_name); $this->table_name = AkInflector::tableize($this->class_name); $this->file_name = AkInflector::underscore($this->class_name); $this->controller_class_name = AkInflector::pluralize($this->class_name); $this->controller_file_name = AkInflector::toControllerFilename($this->controller_class_name); $this->table_columns = trim(join(' ', (array) @$this->table_columns)); $this->assignVarToTemplate('attributes', $this->_getModelAttributesForViews()); $this->assignVarToTemplate('class_name', $this->class_name); $this->assignVarToTemplate('table_name', $this->table_name); $this->assignVarToTemplate('plural_name', $this->table_name); $this->assignVarToTemplate('singular_name', AkInflector::singularize($this->table_name)); $this->assignVarToTemplate('file_name', $this->file_name); $this->assignVarToTemplate('table_columns', $this->table_columns); $this->assignVarToTemplate('controller_class_name', $this->controller_class_name); }
public function __construct($entity, $options) { $this->singular = $this->plural = $entity; $this->options['controller'] = $this->controller = isset($options['controller']) ? $options['controller'] : AkInflector::pluralize($this->singular); parent::__construct($entity, $options); }
function beforeDestroy(&$object) { $association_ids = $object->getAssociatedIds(); foreach ($association_ids as $association_id) { if (!empty($object->{$association_id}) && is_object($object->{$association_id}) && method_exists($object->{$association_id}, 'getType') && strtolower($object->belongsTo->getOption($association_id, 'class_name')) == strtolower($object->{$association_id}->getType())) { $primary_key_name = $this->Owner->{$association_id}->getAssociationOption('primary_key_name'); if ($this->Owner->{$association_id}->getAssociationOption('counter_cache')) { $object->{$association_id}->decrementCounter(AkInflector::pluralize($association_id) . '_count', $object->get($primary_key_name)); } } } return true; }
function renderPartialCollection($partial_name, $collection, $partial_spacer_template = null, $local_assigns = null, $status = null) { $this->_addVariablesToAssigns(); $collection_name = AkInflector::pluralize($partial_name) . '_collection'; $result = $this->Template->renderPartialCollection($partial_name, $collection, $partial_spacer_template, $local_assigns); if (empty($this->{$collection_name})) { $this->{$collection_name} = $result; } $this->variables_added = false; $this->performed_render = false; return $result; }
public function quantify($item) { if ($this->value == 1) { return $this->value . ' ' . Ak::t(AkInflector::singularize($item)); } else { return $this->value . ' ' . Ak::t(AkInflector::pluralize($item)); } }
function is_plural($plural) { return AkInflector::pluralize(AkInflector::singularize($plural)) == $plural; }
public function pluralize($dictionary = null) { return AkInflector::pluralize($this->value, null, $dictionary); }
public function assertSingletonNamedRoutesFor($singleton_name, $options = array()) { $options['options'] = empty($options['options']) ? array() : $options['options']; $options['options']['controller'] = !empty($options['controller']) ? Ak::deleteAndGetValue($options, 'controller') : AkInflector::pluralize($singleton_name); $controller_class_name = AkInflector::camelize($options['options']['controller']) . 'Controller'; $this->recognizeRouteForPath($singleton_name); $this->Controller = new $controller_class_name(); $this->Request = new AkTestRequest(); $this->Response = new AkTestResponse(); Ak::deleteAndGetValue($options['options'], 'action'); $path = isset($options['as']) ? $options['as'] : $singleton_name; $name_prefix = @$options['path_prefix']; $full_path = '/' . $name_prefix . $path; $this->assertNamedRoute($full_path, $name_prefix . $singleton_name . '_path', $options['options']); $this->assertNamedRoute($full_path . '.xml', $name_prefix . $singleton_name . '_path', array_merge($options['options'], array('format' => 'xml'))); $this->assertNamedRoute($full_path . '/add', 'add_' . $name_prefix . $singleton_name . '_path', $options['options']); $this->assertNamedRoute($full_path . '/add.xml', 'add_' . $name_prefix . $singleton_name . '_path', array_merge($options['options'], array('format' => 'xml'))); $this->assertNamedRoute($full_path . '/edit', 'edit_' . $name_prefix . $singleton_name . '_path', $options['options']); $this->assertNamedRoute($full_path . '/edit.xml', 'edit_' . $name_prefix . $singleton_name . '_path', array_merge($options['options'], array('format' => 'xml'))); }
public function test_spanish_dictionary() { $this->assertNotEqual('tijeras',AkInflector::singularize('tijeras')); $this->assertEqual('tijeras',AkInflector::singularize('tijeras',null,'es')); $this->assertEqual('ingleses',AkInflector::pluralize('inglés',null,'es')); }
function loadFixture($model_name) { if (!($fixture_file_name = $this->findFixtureForModel($model_name))) { return false; } $Model = new $model_name(); $Fixture = array(); $items = Ak::convert('yaml', 'array', file_get_contents($fixture_file_name)); foreach ($items as $id => $item) { $Record = $Model->create($item); #we replace the 'id' with the returned value from the db $item['id'] = $Record->getId(); $Fixture[$id] = new FixedActiveRecord($item, $Model); } return $this->{AkInflector::pluralize($model_name)} = $Fixture; }
/** * Generate a xml representation of the model record. * * Example result: * * <?xml version="1.0" encoding="UTF-8"?> * <person> * <id>1</id> * <first-name>Hansi</first-name> * <last-name>Müller</last-name> * <email>hans@mueller.com</email> * <created-at type="datetime">2008-01-01 13:01:23</created-at> * </person> * * parameters: * * @param array $options * * option parameters: * array( * 'collection' => array($Person1,$Person), // array of ActiveRecords * 'include' => array('association1','association2'), // include the associations when exporting * 'exclude' => array('id','name'), // exclude the attribtues * 'only' => array('email','last_name') // only export these attributes * ) * @return string in Xml Format */ public function toXml($options = array()) { $options['padding'] = empty($options['padding']) ? 0 : $options['padding'] + 1; $current_padding = str_repeat(' ', $options['padding']); if (is_array($options) && isset($options[0]) && $options[0] instanceof ArrayAccess) { $options = array('collection' => $options); } if (isset($options['collection']) && (is_array($options['collection']) || $options['collection'] instanceof ArrayAccess) && $options['collection'][0]->_modelName == $this->_Model->getModelName()) { $root = AkInflector::underscore(AkInflector::pluralize($this->_Model->getModelName())); $root = $this->_convertColumnToXmlElement($root); $xml = ''; if (!(isset($options['skip_instruct']) && $options['skip_instruct'] == true)) { $xml .= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; } $xml .= $current_padding . '<' . $root . " type=\"array\">\n"; $collection = $options['collection']; unset($options['collection']); $options['skip_instruct'] = true; $options['padding']++; foreach ($collection as $element) { $xml .= $element->toXml($options); } $xml .= $current_padding . '</' . $root . ">\n"; return $xml; } /** * see if we need to include associations */ $associatedIds = array(); if (isset($options['include']) && !empty($options['include'])) { $options['include'] = is_array($options['include']) ? $options['include'] : preg_split('/,\\s*/', $options['include']); foreach ($this->_Model->getAssociations() as $key => $obj) { if (in_array($key, $options['include'])) { if ($obj->getType() != 'hasAndBelongsToMany') { $associatedIds[$obj->getAssociationIds() . '_id'] = array('name' => $key, 'type' => $obj->getType()); } else { $associatedIds[$key] = array('name' => $key, 'type' => $obj->getType()); } } } } if (isset($options['only'])) { $options['only'] = is_array($options['only']) ? $options['only'] : preg_split('/,\\s*/', $options['only']); } if (isset($options['except'])) { $options['except'] = is_array($options['except']) ? $options['except'] : preg_split('/,\\s*/', $options['except']); } $xml = ''; if (!(isset($options['skip_instruct']) && $options['skip_instruct'] == true)) { $xml .= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; } $root = $this->_convertColumnToXmlElement(AkInflector::underscore($this->_Model->getModelName())); $xml .= $current_padding . '<' . $root . ">\n"; foreach ($this->_Model->getColumns() as $key => $def) { if (isset($options['except']) && in_array($key, $options['except'])) { continue; } else { if (isset($options['only']) && !in_array($key, $options['only'])) { continue; } else { $columnType = $def['type']; $elementName = $this->_convertColumnToXmlElement($key); $xml .= $current_padding . ' <' . $elementName; $val = $this->_Model->{$key}; if (!in_array($columnType, array('string', 'text', 'serial'))) { $xml .= ' type="' . $columnType . '"'; if ($columnType == 'boolean') { $val = $val ? 1 : 0; } } elseif ($columnType == 'serial' && is_numeric($val)) { $xml .= ' type="integer"'; } if (!empty($val) && in_array($columnType, array('datetime'))) { // UTC (Coordinated Universal Time) http://www.w3.org/TR/NOTE-datetime $val = gmdate('Y-m-d\\TH:i:s\\Z', Ak::getTimestamp($val)); } if (is_null($val)) { $xml .= ' nil="true"'; } $xml .= '>' . Ak::utf8($val) . '</' . $elementName . ">\n"; } } } if (isset($options['include'])) { foreach ($this->_Model->getAssociations() as $key => $val) { if (in_array($key, $options['include']) || in_array($val, $options['include'])) { if (is_array($this->_Model->{$key})) { $associationElement = $key; $associationElement = AkInflector::underscore(AkInflector::pluralize($associationElement)); $associationElement = $this->_convertColumnToXmlElement($associationElement); $xml .= $current_padding . '<' . $associationElement . " type=\"array\">\n"; $options['padding']++; foreach ($this->_Model->{$key} as $el) { if ($el instanceof AkBaseModel) { $xml .= $el->toXml(array('skip_instruct' => true)); } } $xml .= $current_padding . '</' . $associationElement . ">\n"; } else { $el = $this->_Model->{$key}->load(); if ($el instanceof AkBaseModel) { $xml .= $el->toXml(array('skip_instruct' => true)); } } } } } $xml .= $current_padding . '</' . $root . ">\n"; return $xml; }
/** * Converts a class name to its table name according to rails * naming conventions. * * Converts "Person" to "people" * * @access public * @static * @see classify * @param string $class_name Class name for getting related table_name. * @return string plural_table_name */ function tableize($class_name) { return AkInflector::pluralize(AkInflector::underscore($class_name)); }
function Loop($match, $state) { if(AK_LEXER_SPECIAL === $state){ $sintags_var = trim(preg_replace('/[\s|?]+/',' ', substr($match, 6,-1))); if(strstr($sintags_var,' as ')){ $new_sintags_var = substr($sintags_var,0, strpos($sintags_var,' ')); $termination = $this->_getTerminationName(AkInflector::pluralize(str_replace($new_sintags_var.' as ','', $sintags_var))); $sintags_var = $new_sintags_var; } $php_variable = $this->_convertSintagsVarToPhp($sintags_var); if($php_variable){ $php_variable = $php_variable; $termination = empty($termination) ? $this->_getTerminationName($sintags_var) : $termination; $singular_variable = '$'.AkInflector::singularize($termination); $plural_variable = '$'.$termination; $this->output .= "<?php ". "\n empty({$php_variable}) ? null : {$singular_variable}_loop_counter = 0;". "\n empty({$php_variable}) ? null : {$plural_variable}_available = count({$php_variable});". "\n if(!empty({$php_variable}))". "\n foreach ({$php_variable} as {$singular_variable}_loop_key=>{$singular_variable}){". "\n {$singular_variable}_loop_counter++;". "\n {$singular_variable}_is_first = {$singular_variable}_loop_counter === 1;". "\n {$singular_variable}_is_last = {$singular_variable}_loop_counter === {$plural_variable}_available;". "\n {$singular_variable}_odd_position = {$singular_variable}_loop_counter%2;". "\n?>"; }else{ $this->output .= $match; } } return true; }
function Test_for_setting_custom_plurals() { AkInflector::pluralize('camión', 'camiones'); $this->assertEqual(AkInflector::pluralize('camión'), 'camiones'); }
/** * Simply checks if $collection_name is the plural of $child_name * * @param string $child_name * @param string $collection_name * @return boolean */ public static function isCollectionOf($child_name, $collection_name) { return AkInflector::pluralize($child_name) == $collection_name; }
/** * Generate a xml representation of the model record. * * Example result: * * <?xml version="1.0" encoding="UTF-8"?> * <person> * <id>1</id> * <first-name>Hansi</first-name> * <last-name>Müller</last-name> * <email>hans@mueller.com</email> * <created-at type="datetime">2008-01-01 13:01:23</created-at> * </person> * * parameters: * * @param array $options * * option parameters: * array( * 'collection' => array($Person1,$Person), // array of ActiveRecords * 'include' => array('association1','association2'), // include the associations when exporting * 'exclude' => array('id','name'), // exclude the attribtues * 'only' => array('email','last_name') // only export these attributes * ) * @return string in Xml Format */ function toXml($options = array()) { if (is_array($options) && isset($options[0]) && is_a($options[0], 'AkActiveRecord')) { $options = array('collection'=>$options); } if (isset($options['collection']) && is_array($options['collection']) && $options['collection'][0]->_modelName == $this->_modelName) { $root = strtolower(AkInflector::pluralize($this->_modelName)); $root = $this->_convert_column_to_xml_element($root); $xml = ''; if (!(isset($options['skip_instruct']) && $options['skip_instruct'] == true)) { $xml .= '<?xml version="1.0" encoding="UTF-8"?>'; } $xml .= '<' . $root . '>'; $collection = $options['collection']; unset($options['collection']); $options['skip_instruct'] = true; foreach ($collection as $element) { $xml .= $element->toXml($options); } $xml .= '</' . $root .'>'; return $xml; } /** * see if we need to include associations */ $associatedIds = array(); if (isset($options['include']) && !empty($options['include'])) { $options['include'] = is_array($options['include'])?$options['include']:preg_split('/,\s*/',$options['include']); foreach ($this->_associations as $key => $obj) { if (in_array($key,$options['include'])) { if ($obj->getType()!='hasAndBelongsToMany') { $associatedIds[$obj->getAssociationId() . '_id'] = array('name'=>$key,'type'=>$obj->getType()); } else { $associatedIds[$key] = array('name'=>$key,'type'=>$obj->getType()); } } } } if (isset($options['only'])) { $options['only'] = is_array($options['only'])?$options['only']:preg_split('/,\s*/',$options['only']); } if (isset($options['except'])) { $options['except'] = is_array($options['except'])?$options['except']:preg_split('/,\s*/',$options['except']); } $xml = ''; if (!(isset($options['skip_instruct']) && $options['skip_instruct'] == true)) { $xml .= '<?xml version="1.0" encoding="UTF-8"?>'; } $root = $this->_convert_column_to_xml_element(strtolower($this->_modelName)); $xml .= '<' . $root . '>'; $xml .= "\n"; foreach ($this->_columns as $key => $def) { if (isset($options['except']) && in_array($key, $options['except'])) { continue; } else if (isset($options['only']) && !in_array($key, $options['only'])) { continue; } else { $columnType = $def['type']; $elementName = $this->_convert_column_to_xml_element($key); $xml .= '<' . $elementName; $val = $this->$key; if (!in_array($columnType,array('string','text','serial'))) { $xml .= ' type="' . $columnType . '"'; if ($columnType=='boolean') $val = $val?1:0; } $xml .= '>' . Ak::utf8($val) . '</' . $elementName . '>'; $xml .= "\n"; } } if (isset($options['include'])) { foreach($this->_associationIds as $key=>$val) { if ((in_array($key,$options['include']) || in_array($val,$options['include']))) { if (is_array($this->$key)) { $associationElement = $key; $associationElement = AkInflector::pluralize($associationElement); $associationElement = $this->_convert_column_to_xml_element($associationElement); $xml .= '<'.$associationElement.'>'; foreach ($this->$key as $el) { if (is_a($el,'AkActiveRecord')) { $xml .= $el->toXml(array('skip_instruct'=>true)); } } $xml .= '</' . $associationElement .'>'; } else { $el = &$this->$key->load(); if (is_a($el,'AkActiveRecord')) { $xml.=$el->toXml(array('skip_instruct'=>true)); } } } } } $xml .= '</' . $root . '>'; return $xml; }