/** * SASS Export * * @param int $id * @return \Illuminate\Http\Response */ public function sassExport($id) { $swatch = Swatch::find(alphaID($id, true)); $values = $swatch->values(); $sass = array(); foreach ($values["blocks"] as $block) { $blocks[] = $block['value']; } $blocks = array_unique($blocks); foreach ($blocks as $block) { if (empty($block)) { continue; } $name = colorNamer($block); $sass = uniqueKeyArrayPush($sass, [$name => '$' . $name . ": #" . $block . ';']); } return ["sass" => implode(" \r\n", $sass)]; }
function uniqueKeyArrayPush($array, $element, $increment = 0) { foreach ($element as $key => $value) { $key = $key . ($increment > 0 ? $increment : ''); if (!isset($array[$key])) { $array[$key] = $value; } else { $increment++; $array = uniqueKeyArrayPush($array, $element, $increment); } } return $array; }