Chris@0: getSelectedFieldKeys($fields, $fieldLabels); Chris@0: if (empty($fields)) { Chris@0: return array_intersect_key($fieldLabels, $firstRow); Chris@0: } Chris@0: return $this->reorderFieldLabels($fields, $fieldLabels, $data); Chris@0: } Chris@0: Chris@0: protected function reorderFieldLabels($fields, $fieldLabels, $data) Chris@0: { Chris@0: $result = []; Chris@0: $firstRow = reset($data); Chris@0: if (!$firstRow) { Chris@0: $firstRow = $fieldLabels; Chris@0: } Chris@0: foreach ($fields as $field) { Chris@0: if (array_key_exists($field, $firstRow)) { Chris@0: if (array_key_exists($field, $fieldLabels)) { Chris@0: $result[$field] = $fieldLabels[$field]; Chris@0: } Chris@0: } Chris@0: } Chris@0: return $result; Chris@0: } Chris@0: Chris@0: protected function getSelectedFieldKeys($fields, $fieldLabels) Chris@0: { Chris@17: if (empty($fieldLabels)) { Chris@17: return []; Chris@17: } Chris@0: if (is_string($fields)) { Chris@0: $fields = explode(',', $fields); Chris@0: } Chris@0: $selectedFields = []; Chris@0: foreach ($fields as $field) { Chris@0: $matchedFields = $this->matchFieldInLabelMap($field, $fieldLabels); Chris@0: if (empty($matchedFields)) { Chris@0: throw new UnknownFieldException($field); Chris@0: } Chris@0: $selectedFields = array_merge($selectedFields, $matchedFields); Chris@0: } Chris@0: return $selectedFields; Chris@0: } Chris@0: Chris@0: protected function matchFieldInLabelMap($field, $fieldLabels) Chris@0: { Chris@0: $fieldRegex = $this->convertToRegex($field); Chris@0: return Chris@0: array_filter( Chris@0: array_keys($fieldLabels), Chris@0: function ($key) use ($fieldRegex, $fieldLabels) { Chris@0: $value = $fieldLabels[$key]; Chris@0: return preg_match($fieldRegex, $value) || preg_match($fieldRegex, $key); Chris@0: } Chris@0: ); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Convert the provided string into a regex suitable for use in Chris@0: * preg_match. Chris@0: * Chris@0: * Matching occurs in the same way as the Symfony Finder component: Chris@0: * http://symfony.com/doc/current/components/finder.html#file-name Chris@0: */ Chris@0: protected function convertToRegex($str) Chris@0: { Chris@0: return $this->isRegex($str) ? $str : Glob::toRegex($str); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks whether the string is a regex. This function is copied from Chris@0: * MultiplePcreFilterIterator in the Symfony Finder component. Chris@0: * Chris@0: * @param string $str Chris@0: * Chris@0: * @return bool Whether the given string is a regex Chris@0: */ Chris@0: protected function isRegex($str) Chris@0: { Chris@0: if (preg_match('/^(.{3,}?)[imsxuADU]*$/', $str, $m)) { Chris@0: $start = substr($m[1], 0, 1); Chris@0: $end = substr($m[1], -1); Chris@0: Chris@0: if ($start === $end) { Chris@0: return !preg_match('/[*?[:alnum:] \\\\]/', $start); Chris@0: } Chris@0: Chris@0: foreach (array(array('{', '}'), array('(', ')'), array('[', ']'), array('<', '>')) as $delimiters) { Chris@0: if ($start === $delimiters[0] && $end === $delimiters[1]) { Chris@0: return true; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: return false; Chris@0: } Chris@0: }