Created
July 19, 2016 05:49
-
-
Save dilab/022a071b2a64fd4e00bf674c8b24fdcf to your computer and use it in GitHub Desktop.
Generate unique names
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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