Skip to content

Commit

Permalink
Move property name conversion into its own class
Browse files Browse the repository at this point in the history
  • Loading branch information
robgridley committed Mar 10, 2017
1 parent 113e1cf commit 8f90a7d
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 10 deletions.
5 changes: 3 additions & 2 deletions spec/SimpleXmlMapper/XmlMapperSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
use DateTime;
use SimpleXMLElement;
use PhpSpec\ObjectBehavior;
use InvalidArgumentException;
use SimpleXmlMapper\XmlMapper;
use SimpleXmlMapper\CamelCasePropertyNameConverter;
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
Expand All @@ -18,7 +18,8 @@ function let()
$listExtractors = [new ReflectionExtractor];
$typeExtractors = [new PhpDocExtractor];
$extractor = new PropertyInfoExtractor($listExtractors, $typeExtractors);
$this->beConstructedWith($extractor);
$nameConverter = new CamelCasePropertyNameConverter;
$this->beConstructedWith($extractor, $nameConverter);
$this->addType(DateTime::class, function ($xml) {
return DateTime::createFromFormat('Y-m-d H:i:s', $xml);
});
Expand Down
19 changes: 19 additions & 0 deletions src/CamelCasePropertyNameConverter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace SimpleXmlMapper;

use Doctrine\Common\Inflector\Inflector;

class CamelCasePropertyNameConverter implements PropertyNameConverterInterface
{
/**
* Convert the specified XML property name to its PHP property name.
*
* @param string $name
* @return string
*/
public function convert($name)
{
return Inflector::camelize($name);
}
}
14 changes: 14 additions & 0 deletions src/PropertyNameConverterInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace SimpleXmlMapper;

interface PropertyNameConverterInterface
{
/**
* Convert the specified XML property name to its PHP property name.
*
* @param string $name
* @return string
*/
public function convert($name);
}
22 changes: 14 additions & 8 deletions src/XmlMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use SimpleXMLElement;
use InvalidArgumentException;
use UnexpectedValueException;
use Doctrine\Common\Inflector\Inflector;
use Symfony\Component\PropertyInfo\Type;
use Symfony\Component\PropertyInfo\PropertyInfoExtractorInterface;

Expand All @@ -18,6 +17,13 @@ class XmlMapper
*/
protected $extractor;

/**
* The property name converter instance.
*
* @var PropertyNameConverterInterface|null
*/
protected $nameConverter;

/**
* The default type instance.
*
Expand All @@ -36,12 +42,13 @@ class XmlMapper
* Create a new mapper instance.
*
* @param PropertyInfoExtractorInterface $extractor
* @param Type|null $defaultType
* @param PropertyNameConverterInterface|null $nameConverter
*/
public function __construct(PropertyInfoExtractorInterface $extractor, Type $defaultType = null)
public function __construct(PropertyInfoExtractorInterface $extractor, PropertyNameConverterInterface $nameConverter = null)
{
$this->extractor = $extractor;
$this->defaultType = $defaultType ?: new Type('string');
$this->nameConverter = $nameConverter;
$this->defaultType = new Type('string');
}

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

if ($camelize) {
$name = Inflector::camelize($name);
if ($this->nameConverter) {
$name = $this->nameConverter->convert($name);
}

if (in_array($name, $properties)) {
Expand Down

0 comments on commit 8f90a7d

Please sign in to comment.