Skip to content

Instantly share code, notes, and snippets.

@dilab
Created July 19, 2016 05:49
Show Gist options
  • Save dilab/022a071b2a64fd4e00bf674c8b24fdcf to your computer and use it in GitHub Desktop.
Save dilab/022a071b2a64fd4e00bf674c8b24fdcf to your computer and use it in GitHub Desktop.
Generate unique names
<?php
class KeyGenerator
{
public static function fromLabels(array $labels)
{
if (empty($labels)) {
return [];
}
$labelsFormatted = array_map(function ($label) {
return Inflector::underscore(Inflector::camelize($label));
}, $labels);
$labelsCount = array_count_values($labelsFormatted);
$reverse = array_map(function ($label) use (&$labelsCount) {
$labelsCount[$label] = $labelsCount[$label] - 1;
$currentLabelCount = $labelsCount[$label];
if (0 == $currentLabelCount) {
return $label;
}
return $label . '_' . $currentLabelCount;
}, array_reverse($labelsFormatted, true));
return array_reverse($reverse);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment