本文整理汇总了PHP中Cake\Utility\Inflector::singularize方法的典型用法代码示例。如果您正苦于以下问题:PHP Inflector::singularize方法的具体用法?PHP Inflector::singularize怎么用?PHP Inflector::singularize使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Cake\Utility\Inflector
的用法示例。
在下文中一共展示了Inflector::singularize方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: main
/**
* main
*
*/
public function main()
{
$schemaPo = APP . 'Locale' . DS . 'schema.pot';
$conn = ConnectionManager::get('default');
$collection = $conn->schemaCollection();
$translations = new Translations();
$tables = $collection->listTables();
foreach ($tables as $table) {
$translations->insert($table, Inflector::humanize(Inflector::underscore($table)));
$translations->insert($table, Inflector::humanize(Inflector::underscore(Inflector::singularize($table))));
$columns = $collection->describe($table)->columns();
foreach ($columns as $column) {
$c = $collection->describe($table)->column($column);
$comment = $c['comment'];
$t = new Translation($table . '.' . $column, Inflector::humanize(Inflector::underscore($column)));
$translations[] = $t;
$t->setTranslation($comment);
$t = new Translation($table . '.' . $column, Inflector::humanize(Inflector::underscore(Inflector::singularize($table))) . ' ' . Inflector::humanize(Inflector::underscore($column)));
$translations[] = $t;
$t->setTranslation($comment);
}
}
$poString = $translations->toPoString();
$caked = preg_replace('/msgctxt "([^"]+)"/i', '#: \\1', $poString);
$this->createFile($schemaPo, $caked);
}
示例2: upload
/**
* Upload (Ajax)
**/
public function upload($ref, $ref_id)
{
$this->layout = null;
$this->autoRender = false;
if (!$this->canUploadMedias($ref, $ref_id)) {
throw new ForbiddenException();
}
$media = $this->Medias->newEntity();
$model = Inflector::singularize($ref);
if (isset($_FILES) && !empty($_FILES)) {
$data['ref'] = $model;
$data['ref_id'] = $ref_id;
$data['file'] = $_FILES['file'];
$new_media = $this->Medias->patchEntity($media, $data);
$this->Medias->save($new_media, $_FILES);
if ($this->Medias->error != "") {
echo json_encode(array('error' => $this->Medias->error));
return false;
}
}
$this->loadModel($ref);
$editor = isset($this->request->params['named']['editor']) ? $this->request->params['named']['editor'] : false;
$id = isset($this->request->query['id']) ? $this->request->query['id'] : false;
$this->set(compact('media', 'thumbID', 'editor', 'id'));
$this->layout = 'json';
$this->render('media');
}
示例3: output
public function output($field, $options = array())
{
// if there is a override type on the field, don't make it a belongsTo link
if (!$this->helper->hasOverride($field)) {
// if this is a belongsTo field, make it a link to the parent record
if (in_array($field, $this->helper->foreignKeys()) && ($this->belongsTo = $this->fieldIsKey($field, 'manyToOne'))) {
$nameable = new NameConventions($field);
$association_name = strtolower($nameable->modelName);
// This is the name of the associated entity. It will also be
// the node on the parent entity where the associated entity
// can be found (the conventional location)
$association_entity = Inflector::singularize($association_name);
// This is going to contain the associated Table object
$association = $this->helper->CrudData->associationCollection()->get($association_name);
if (!is_null($association) && is_subclass_of($this->helper->entity->{$association_entity}, 'Cake\\ORM\\Entity')) {
$displayField = $association->displayField();
$output = $this->helper->entity->{$association_entity}->{$displayField};
// $this->helper->CrudData->_columns[$field] = ['type' => 'string']; ////// THIS CASCADES TO ATTRIBUTES TOO
//
// $this->helper->swapEntity($this->helper->entity->$association_entity);
// $output = $this->base->output($displayField, $options);
// $this->helper->restoreEntity();
} else {
$output = $this->base->output($field, $options);
}
return $this->helper->entity->has($this->belongsTo['property']) ? $this->helper->Html->link($output, ['controller' => $this->belongsTo['name'], 'action' => 'view', $this->helper->entity->{$field}]) : '';
}
}
return $this->base->output($field, $options);
}
示例4: _deriveViewVar
/**
* Derive the viewVar based on the scope of the action
*
* Actions working on a single entity will use singular name,
* and actions working on a full table will use plural name
*
* @throws Exception
* @return string
*/
protected function _deriveViewVar()
{
if ($this->scope() === 'table') {
return Inflector::variable($this->_controller()->name);
}
if ($this->scope() === 'entity') {
return Inflector::variable(Inflector::singularize($this->_controller()->name));
}
throw new Exception('Unknown action scope: ' . $this->scope());
}
示例5: property
/**
* Sets the property name that should be filled with data from the target table
* in the source table record.
* If no arguments are passed, currently configured type is returned.
*
* @param string|null $name The name of the property. Pass null to read the current value.
* @return string
*/
public function property($name = null)
{
if ($name !== null) {
return parent::property($name);
}
if ($name === null && !$this->_propertyName) {
list(, $name) = pluginSplit($this->_name);
$this->_propertyName = Inflector::underscore(Inflector::singularize($name));
}
return $this->_propertyName;
}
示例6: foreignKey
/**
* Sets the name of the field representing the foreign key to the source table.
* If no parameters are passed current field is returned
*
* @param string $key the key to be used to link both tables together
* @return string
*/
public function foreignKey($key = null)
{
if ($key === null) {
if ($this->_foreignKey === null) {
$key = Inflector::singularize($this->source()->alias());
$this->_foreignKey = Inflector::underscore($key) . '_id';
}
return $this->_foreignKey;
}
return parent::foreignKey($key);
}
示例7: delete
/**
* Delete action.
*
* @param string|null $id Menu id.
* @return void Redirects to index.
* @throws \Cake\Network\Exception\NotFoundException When record not found.
*/
public function delete($id = null)
{
$this->request->allowMethod(['post', 'delete']);
$controllerName = Inflector::singularize($this->name);
$lowerController = mb_strtolower($controllerName);
$record = $this->{$this->name}->get($id);
$domain = Plugin::getPluginName($this->plugin);
if ($this->{$this->name}->delete($record)) {
$this->Flash->success(__d($domain, 'The {0} has been deleted.', $lowerController));
} else {
$this->Flash->error(__d($domain, 'The {0} could not be deleted. Please, try again.', $lowerController));
}
return $this->redirect(['action' => 'index']);
}
示例8: alterSchema
public function alterSchema(Schema $schema)
{
$customizedType = Inflector::singularize($schema->name());
$customFields = $this->find()->cache('custom_fields_' . $this->connection()->configName());
/** @var \Muffin\Webservice\Model\Resource[] $customFields */
foreach ($customFields as $customField) {
if ($customField->customized_type !== $customizedType) {
continue;
}
$columnKeys = ['type' => $customField->field_format, 'default' => $customField->default_value, 'custom_field_id' => $customField->id, 'custom_field_filterable' => $customField->is_filter];
$schema->addColumn(Schema::nameToField($customField->name), $columnKeys);
}
return $schema;
}
示例9: setDefaultTitle
public function setDefaultTitle()
{
switch ($this->request->params['action']) {
case 'index':
return Inflector::humanize($this->request->params['controller']);
case 'add':
return 'New ' . Inflector::singularize(Inflector::humanize($this->request->params['controller']));
case 'edit':
return 'Edit ' . Inflector::singularize(Inflector::humanize($this->request->params['controller']));
case 'image':
return 'Image for ' . Inflector::singularize(Inflector::humanize($this->request->params['controller']));
default:
return Inflector::humanize($this->request->params['action']);
}
return false;
}
示例10: getIndexMenuTop
/**
* Method that adds elements to index View top menu.
*
* @param Cake\Event\Event $event Event object
* @param Cake\Network\Request $request Request object
* @param array $options Entity options
* @return undefined
*/
public function getIndexMenuTop(Event $event, Request $request, array $options)
{
$appView = $event->subject();
if (!$appView instanceof View) {
$appView = new AppView();
}
$urlAdd = ['plugin' => $request->plugin, 'controller' => $request->controller, 'action' => 'add'];
$btnAdd = $appView->Html->link($appView->Html->icon('plus') . self::BLANK_SPACE . __('Add {0}', Inflector::singularize($options['title'])), $urlAdd, ['escape' => false, 'class' => 'btn btn-primary']);
$menu = [['label' => $btnAdd, 'url' => $urlAdd, 'capabilities' => 'fromUrl']];
if ($appView->elementExists(static::MENU_ELEMENT)) {
$result = $appView->element(static::MENU_ELEMENT, ['menu' => $menu, 'renderAs' => 'provided']);
} else {
$result = $btnAdd;
}
return $result;
}
示例11: upload
/**
* Upload a file to the server
* @param upload - file to upload
* @param owner - contains model name and id associated with the file
* @return array - uploaded file information or null for failure
*/
public function upload($upload, $owner = null)
{
// Filesize verification
if ($upload['size'] == 0) {
return null;
}
if ($upload['size'] > $this->maxFileSize) {
return null;
}
$path = $this->storagePath;
// Owner separated storage
if ($this->storeOwner == true && $owner) {
// Directory should be lower case
$ownerTable = strtolower($owner->source());
// Owner specific directory must be unique (uuid)
$ownerDirectory = Inflector::singularize($ownerTable) . $owner->id;
$path .= DS . $ownerTable . DS . $ownerDirectory;
}
// If types do not match, default subdir is 'document'
$subdir = 'document';
$types = ['image', 'audio', 'video'];
// Check for filetype
foreach ($types as $type) {
if (strstr($upload['type'], $type)) {
$subdir = $type;
}
}
// Append the subdirectory (filtype directory)
$path .= DS . $subdir;
// Make directory if there is none
$directory = new Folder();
if (!$directory->create(WWW_ROOT . DS . $path)) {
return null;
}
// Find file in tmp
$file = new File($upload['tmp_name']);
// File's name must be unique, making the path unique as well
$name = time() . '_' . Inflector::slug($upload['name']);
$path .= DS . $name;
// Copy from tmp to perm (create)
if ($file->copy($path)) {
return ['original_name' => $upload['name'], 'name' => $name, 'path' => $path, 'type' => $upload['type'], 'size' => $upload['size']];
} else {
return null;
}
}
示例12: inputs
/**
* Build Form::inputs structure
*
* @param ParameterRegistry $parameters Form parameters collection.
* @param array $options Additional input options.
* @return array
*/
public function inputs(ParameterRegistry $parameters, $options = [])
{
$result = [];
$entityName = Inflector::singularize($parameters->formName);
foreach ($parameters->collection() as $primaryParameter) {
foreach ($primaryParameter->viewValues() as $param) {
$name = $param->config('name');
$inputOptions = array_key_exists($name, $options) ? $options[$name] : [];
$input = $this->input($param, $inputOptions);
$field = $param->config('field');
if (!empty($entityName)) {
$field = "{$entityName}.{$field}";
}
$result[$field] = $input;
}
}
return $result;
}
示例13: getEntity
/**
* @param string $entityName
* @param array $data
* @return Entity\EntityInterface
* @throws Exception\LogicException
*/
public function getEntity($entityName, array $data = [])
{
$class = '';
$classCandidates = [__NAMESPACE__ . '\\Entity\\' . trim($entityName, '\\'), __NAMESPACE__ . '\\Entity\\Subset\\' . trim($entityName, '\\')];
foreach ($classCandidates as $classCandidate) {
if (class_exists($classCandidate)) {
$class = $classCandidate;
break;
}
}
if (!$class) {
throw new LogicException("Entity \"{$entityName}\" is undefined.");
}
$entity = new $class();
if (!$entity instanceof EntityInterface) {
throw new LogicException("Class \"{$class}\" isn't an implement of EntityInterface.");
}
foreach ($data as $key => $value) {
if (is_array($value)) {
$childEntityName = Inflector::singularize((string) Inflector::camelize($key));
if (Hash::check($value, '{s}')) {
// has string key => entity.
$value = $this->getEntity($childEntityName, $value);
} elseif (Hash::check($value, '{n}.{s}')) {
// has string key under number key => array of entities.
$children = [];
foreach ($value as $child) {
$children[] = $this->getEntity($childEntityName, $child);
}
$value = $children;
} else {
// else => array of scalar.
}
}
if (property_exists($entity, $key)) {
$entity->{$key} = $value;
}
}
return $entity;
}
示例14: paginate
public function paginate($query = null, array $settings = [])
{
$action = $this->request->action;
$nested = strcmp(substr($action, -5, 5), 'Index') === 0;
if (!$nested) {
foreach (explode(' ', $this->request->query('q')) as $q) {
foreach ($this->searchFields as $field) {
$ORs["{$field} LIKE"] = "%{$q}%";
}
if (empty($q) || empty($ORs)) {
continue;
}
$query->where(["OR" => $ORs]);
}
} else {
if (isset($this->viewVars['parent'])) {
$key = Inflector::singularize(substr($action, 0, -5)) . '_id';
$query->where([$key => $this->viewVars['parent']->id]);
}
}
return $query->all();
}
示例15: entityClass
/**
* Returns the class used to hydrate rows for this table or sets
* a new one
*
* @param string|null $name the name of the class to use
* @throws \Cake\ORM\Exception\MissingEntityException when the entity class cannot be found
* @return string
*/
public function entityClass($name = null)
{
if ($name === null && !$this->_entityClass) {
$default = '\\Cake\\ORM\\Entity';
$self = get_called_class();
$parts = explode('\\', $self);
if ($self === __CLASS__ || count($parts) < 3) {
return $this->_entityClass = $default;
}
$alias = Inflector::singularize(substr(array_pop($parts), 0, -5));
$name = implode('\\', array_slice($parts, 0, -1)) . '\\Entity\\' . $alias;
if (!class_exists($name)) {
return $this->_entityClass = $default;
}
}
if ($name !== null) {
$class = Core::className($name, 'Model/Entity');
$this->_entityClass = $class;
}
if (!$this->_entityClass) {
throw new MissingEntityException([$name]);
}
return $this->_entityClass;
}