Skip to content

Commit

Permalink
Option to convert property names to camel case
Browse files Browse the repository at this point in the history
  • Loading branch information
robgridley committed Mar 9, 2017
1 parent 177f793 commit 113e1cf
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"require": {
"php": ">=5.6.0",
"symfony/property-info": "^3.2",
"phpdocumentor/reflection-docblock": "^3.1"
"phpdocumentor/reflection-docblock": "^3.1",
"doctrine/inflector": "^1.1"
},
"require-dev": {
"phpspec/phpspec": "^3.2"
Expand Down
24 changes: 23 additions & 1 deletion spec/SimpleXmlMapper/XmlMapperSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,16 @@ function it_maps_custom_types()
$this->make_it_map_xml_to_object()->manufacturer->founded->shouldHaveType(DateTime::class);
}

function it_converts_studly_case_to_camel_case()
{
$this->make_it_map_xml_to_object()->horsePower->shouldEqual(292);
}

function it_converts_snake_case_to_camel_case()
{
$this->make_it_map_xml_to_object()->litresPerHundred->shouldEqual(7.7);
}

function make_it_map_xml_to_object()
{
$file = <<<XML
Expand All @@ -106,10 +116,12 @@ function make_it_map_xml_to_object()
</options>
<hybrid>false</hybrid>
<awd>true</awd>
<HorsePower>292</HorsePower>
<litres_per_hundred>7.7</litres_per_hundred>
</car>
XML;
$xml = new SimpleXMLElement($file);
return $this->map($xml, Car::class);
return $this->map($xml, Car::class, true);
}

function getMatchers()
Expand Down Expand Up @@ -168,6 +180,16 @@ class Car
* @var bool
*/
public $awd;

/**
* @var int
*/
public $horsePower;

/**
* @var float
*/
public $litresPerHundred;
}

class Manufacturer
Expand Down
8 changes: 7 additions & 1 deletion src/XmlMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use SimpleXMLElement;
use InvalidArgumentException;
use UnexpectedValueException;
use Doctrine\Common\Inflector\Inflector;
use Symfony\Component\PropertyInfo\Type;
use Symfony\Component\PropertyInfo\PropertyInfoExtractorInterface;

Expand Down Expand Up @@ -59,9 +60,10 @@ public function addType($type, callable $callback)
*
* @param SimpleXMLElement $xml
* @param string $class
* @param bool $camelize
* @return mixed
*/
public function map(SimpleXMLElement $xml, $class)
public function map(SimpleXMLElement $xml, $class, $camelize = false)
{
if (!class_exists($class)) {
throw new InvalidArgumentException("Class [$class] does not exist");
Expand All @@ -73,6 +75,10 @@ public function map(SimpleXMLElement $xml, $class)
foreach ($xml as $node) {
$name = $node->getName();

if ($camelize) {
$name = Inflector::camelize($name);
}

if (in_array($name, $properties)) {
$type = $this->getType($class, $name) ?: $this->defaultType;
$entity->{$name} = $this->getValue($node, $type);
Expand Down

0 comments on commit 113e1cf

Please sign in to comment.