Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Authorizing fields to have child nodes, and fix their parsing in the … #1

Merged
merged 1 commit into from
Mar 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion src/elasticsuite-core/Index/Indices/Config/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ class Converter implements \Magento\Framework\Config\ConverterInterface
const MAPPING_FIELD_NODE_TYPE = 'field';
const DATASOURCES_PATH = 'datasources/datasource';

/**
* Tag names underscore transformation cache
*
* @var array
*/
private $underscoreCache = [];

/**
* Convert dom node tree to array
*
Expand Down Expand Up @@ -159,10 +166,34 @@ private function createMappingField(\DOMNode $fieldNode)

foreach ($fieldNode->childNodes as $childNode) {
if ($childNode instanceof \DOMElement) {
$fieldParam[$childNode->tagName] = $childNode->nodeValue;
$tagName = $this->underscore($childNode->tagName);
$fieldParam[$tagName] = $childNode->nodeValue;
}
}

return $fieldParam;
}

/**
* Converts tag name from camelCase to snake_case
*
* isSearchable === is_searchable
* Uses cache to eliminate unnecessary preg_replace
*
* @param string $name The name to transform
*
* @return string
*/
private function underscore($name)
{
if (isset($this->underscoreCache[$name])) {
return $this->underscoreCache[$name];
}

$result = strtolower(preg_replace('/(.)([A-Z])/', "$1_$2", $name));

$this->underscoreCache[$name] = $result;

return $result;
}
}
3 changes: 3 additions & 0 deletions src/elasticsuite-core/etc/elasticsuite_indices.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@
<xs:annotation>
<xs:documentation>Mapping field declaration root node</xs:documentation>
</xs:annotation>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:any processContents="lax" />
</xs:choice>
<xs:attribute type="xs:string" name="name" use="required"/>
<xs:attribute type="xs:string" name="type" use="required"/>
<xs:attribute type="xs:string" name="nestedPath" use="optional"/>
Expand Down