Skip to content

Commit 8f90a7d

Browse files
committed
Move property name conversion into its own class
1 parent 113e1cf commit 8f90a7d

4 files changed

Lines changed: 50 additions & 10 deletions

File tree

spec/SimpleXmlMapper/XmlMapperSpec.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
use DateTime;
66
use SimpleXMLElement;
77
use PhpSpec\ObjectBehavior;
8-
use InvalidArgumentException;
98
use SimpleXmlMapper\XmlMapper;
9+
use SimpleXmlMapper\CamelCasePropertyNameConverter;
1010
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
1111
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
1212
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
@@ -18,7 +18,8 @@ function let()
1818
$listExtractors = [new ReflectionExtractor];
1919
$typeExtractors = [new PhpDocExtractor];
2020
$extractor = new PropertyInfoExtractor($listExtractors, $typeExtractors);
21-
$this->beConstructedWith($extractor);
21+
$nameConverter = new CamelCasePropertyNameConverter;
22+
$this->beConstructedWith($extractor, $nameConverter);
2223
$this->addType(DateTime::class, function ($xml) {
2324
return DateTime::createFromFormat('Y-m-d H:i:s', $xml);
2425
});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace SimpleXmlMapper;
4+
5+
use Doctrine\Common\Inflector\Inflector;
6+
7+
class CamelCasePropertyNameConverter implements PropertyNameConverterInterface
8+
{
9+
/**
10+
* Convert the specified XML property name to its PHP property name.
11+
*
12+
* @param string $name
13+
* @return string
14+
*/
15+
public function convert($name)
16+
{
17+
return Inflector::camelize($name);
18+
}
19+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace SimpleXmlMapper;
4+
5+
interface PropertyNameConverterInterface
6+
{
7+
/**
8+
* Convert the specified XML property name to its PHP property name.
9+
*
10+
* @param string $name
11+
* @return string
12+
*/
13+
public function convert($name);
14+
}

src/XmlMapper.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use SimpleXMLElement;
66
use InvalidArgumentException;
77
use UnexpectedValueException;
8-
use Doctrine\Common\Inflector\Inflector;
98
use Symfony\Component\PropertyInfo\Type;
109
use Symfony\Component\PropertyInfo\PropertyInfoExtractorInterface;
1110

@@ -18,6 +17,13 @@ class XmlMapper
1817
*/
1918
protected $extractor;
2019

20+
/**
21+
* The property name converter instance.
22+
*
23+
* @var PropertyNameConverterInterface|null
24+
*/
25+
protected $nameConverter;
26+
2127
/**
2228
* The default type instance.
2329
*
@@ -36,12 +42,13 @@ class XmlMapper
3642
* Create a new mapper instance.
3743
*
3844
* @param PropertyInfoExtractorInterface $extractor
39-
* @param Type|null $defaultType
45+
* @param PropertyNameConverterInterface|null $nameConverter
4046
*/
41-
public function __construct(PropertyInfoExtractorInterface $extractor, Type $defaultType = null)
47+
public function __construct(PropertyInfoExtractorInterface $extractor, PropertyNameConverterInterface $nameConverter = null)
4248
{
4349
$this->extractor = $extractor;
44-
$this->defaultType = $defaultType ?: new Type('string');
50+
$this->nameConverter = $nameConverter;
51+
$this->defaultType = new Type('string');
4552
}
4653

4754
/**
@@ -60,10 +67,9 @@ public function addType($type, callable $callback)
6067
*
6168
* @param SimpleXMLElement $xml
6269
* @param string $class
63-
* @param bool $camelize
6470
* @return mixed
6571
*/
66-
public function map(SimpleXMLElement $xml, $class, $camelize = false)
72+
public function map(SimpleXMLElement $xml, $class)
6773
{
6874
if (!class_exists($class)) {
6975
throw new InvalidArgumentException("Class [$class] does not exist");
@@ -75,8 +81,8 @@ public function map(SimpleXMLElement $xml, $class, $camelize = false)
7581
foreach ($xml as $node) {
7682
$name = $node->getName();
7783

78-
if ($camelize) {
79-
$name = Inflector::camelize($name);
84+
if ($this->nameConverter) {
85+
$name = $this->nameConverter->convert($name);
8086
}
8187

8288
if (in_array($name, $properties)) {

0 commit comments

Comments
 (0)