Skip to content

Commit

Permalink
added skip attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
alevikzs committed Feb 27, 2016
1 parent fdad4ec commit 1a7d56a
Show file tree
Hide file tree
Showing 7 changed files with 191 additions and 9 deletions.
25 changes: 24 additions & 1 deletion src/Mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ abstract class Mapper implements MapperInterface {
*/
private $validation;

/**
* @var array
*/
private $skipAttributes;

/**
* @return string
*/
Expand Down Expand Up @@ -159,14 +164,32 @@ public function setValidation($validation) {
return $this;
}

/**
* @param array $attributes
* @return $this
*/
public function setSkipAttributes(array $attributes = []) {
$this->skipAttributes = $attributes;

return $this;
}

/**
* @return array
*/
public function getSkipAttributes() {
return $this->skipAttributes;
}

/**
* @param string|object $outputClassOrObject
* @param integer $adapter
*/
public function __construct($outputClassOrObject, $adapter = self::MEMORY_ANNOTATION_ADAPTER) {
$this->setOutputClassOrObject($outputClassOrObject)
->setAnnotationAdapterType($adapter)
->setValidation(true);
->setValidation(true)
->setSkipAttributes();
}

/**
Expand Down
70 changes: 62 additions & 8 deletions src/Mapper/Structure.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
\Phalcon\Annotations\Adapter\Xcache,

\PhMap\Mapper,
\PhMap\Transforms,
\PhMap\Transform,
\PhMap\Exception\UnknownAnnotationAdapter,
\PhMap\Exception\FieldValidator\UnknownField as UnknownFieldException,
\PhMap\Exception\FieldValidator\MustBeSimple as MustBeSimpleException,
Expand Down Expand Up @@ -203,19 +203,21 @@ public function map() {
$transform = $this->getTransforms() ? $this->getTransforms()->findByInputFieldName($attribute) : null;
$transformedAttribute = $transform ? $transform->getOutputFieldName() : $attribute;

if ($this->needToSkip($transformedAttribute)) {
continue;
}

$setter = $this->createSetter($transformedAttribute);

if ($this->hasAttribute($transformedAttribute)) {
/** @var Annotations $methodAnnotations */
$methodAnnotations = $methodsAnnotations[$setter];

$childTransforms = $transform ? $transform->getTransforms() : null;

$valueToMap = $this->buildValueToMap(
$transformedAttribute,
$value,
$methodAnnotations,
$childTransforms
$transform
);

if (!is_null($valueToMap)) {
Expand All @@ -231,11 +233,51 @@ public function map() {
return $this->getOutputObject();
}

/**
* @return array
*/
protected function getCurrentSkipAttributes() {
static $currentSkipAttributes = [];

if (empty($currentSkipAttributes)) {
foreach ($this->getSkipAttributes() as $attribute) {
$attributes = explode('.', $attribute);
if (count($attributes) === 1) {
$currentSkipAttributes[] = array_pop($attributes);
}
}
}

return $currentSkipAttributes;
}

protected function getSkipAttributesByParent($parentAttribute) {
$skipAttributes = [];

foreach ($this->getSkipAttributes() as $skipAttribute) {
$parentAttributeWithDelimiter = $parentAttribute . '.';

if (strpos($skipAttribute, $parentAttributeWithDelimiter) === 0) {
$skipAttributes[] = str_replace($parentAttributeWithDelimiter, '', $skipAttribute);
}
}

return $skipAttributes;
}

/**
* @param $attribute
* @return boolean
*/
protected function needToSkip($attribute) {
return in_array($attribute, $this->getSkipAttributes());
}

/**
* @param string $attribute
* @param mixed $value
* @param Annotations $methodAnnotations
* @param Transforms|null $transforms
* @param Transform|null $transform
* @return mixed
* @throws MustBeSimpleException
* @throws MustBeSequenceException
Expand All @@ -245,7 +287,7 @@ protected function buildValueToMap(
$attribute,
$value,
Annotations $methodAnnotations,
Transforms $transforms = null
Transform $transform = null
) {
$valueToMap = $value;

Expand All @@ -255,6 +297,10 @@ protected function buildValueToMap(
$mapperAnnotationClass = $mapperAnnotation->getArgument('class');
$mapperAnnotationIsArray = $mapperAnnotation->getArgument('isArray');

$transforms = $transform ? $transform->getTransforms() : null;

$skipAttributes = $this->getSkipAttributesByParent($attribute);

if ($this->isObject($value)) {
if ($mapperAnnotationIsArray) {
if ($this->getValidation()) {
Expand All @@ -266,20 +312,28 @@ protected function buildValueToMap(
/** @var object|array $value */
/** @var Mapper $mapper */
$mapper = new static($value, $mapperAnnotationClass);
$valueToMap = $mapper->setTransforms($transforms)
$valueToMap = $mapper
->setTransforms($transforms)
->setValidation($this->getValidation())
->setSkipAttributes($skipAttributes)
->map();
}
} else {
if ($mapperAnnotationIsArray) {
$validation = $this->getValidation();
$valueToMap = array_map(function($value) use ($mapperAnnotationClass, $transforms, $validation) {
$valueToMap = array_map(function($value) use (
$mapperAnnotationClass,
$transforms,
$validation,
$skipAttributes
) {
/** @var object|array $value */
/** @var Mapper $mapper */
$mapper = new static($value, $mapperAnnotationClass);
return $mapper
->setTransforms($transforms)
->setValidation($validation)
->setSkipAttributes($skipAttributes)
->map();
}, $value);
} elseif ($this->getValidation()) {
Expand Down
11 changes: 11 additions & 0 deletions src/MapperInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ public function enableValidation();
*/
public function getValidation();

/**
* @param array $attributes
* @return $this
*/
public function setSkipAttributes(array $attributes = []);

/**
* @return array
*/
public function getSkipAttributes();

/**
* @return object
*/
Expand Down
19 changes: 19 additions & 0 deletions src/Wrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,25 @@ public function enableValidation() {
return $this;
}

/**
* @param array $attributes
* @return $this
*/
public function setSkipAttributes(array $attributes = []) {
$this->getMapper()
->setSkipAttributes($attributes);

return $this;
}

/**
* @return array
*/
public function getSkipAttributes() {
return $this->getMapper()
->getSkipAttributes();
}

/**
* @return object
*/
Expand Down
25 changes: 25 additions & 0 deletions tests/AssociativeMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,31 @@ public function testTransforms() {
$this->assertEquals($objectMapped, self::getTree());
}

/**
* @return void
*/
public function testSkipAttributes() {
$inputStructure = self::getTreeDecodedToArray();

$skipAttributes = [
'name',
'branch.length'
];

$objectMapped = (new Associative($inputStructure, new Tree(), Mapper::FILES_ANNOTATION_ADAPTER))
->setSkipAttributes($skipAttributes)
->map();
$this->assertEquals(
$objectMapped,
self::getTree()
->setName(null)
->setBranch(
self::getBranch()
->setLength(null)
)
);
}

/**
* @throws \PhMap\Exception\FieldValidator\MustBeSimple
* @return void
Expand Down
25 changes: 25 additions & 0 deletions tests/JsonMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,31 @@ public function testTransforms() {
$this->assertEquals($objectMapped, self::getTree());
}

/**
* @return void
*/
public function testSkipAttributes() {
$inputStructure = self::getTreeJson();

$skipAttributes = [
'name',
'branch.length'
];

$objectMapped = (new Json($inputStructure, new Tree(), Mapper::FILES_ANNOTATION_ADAPTER))
->setSkipAttributes($skipAttributes)
->map();
$this->assertEquals(
$objectMapped,
self::getTree()
->setName(null)
->setBranch(
self::getBranch()
->setLength(null)
)
);
}

/**
* @throws \PhMap\Exception\FieldValidator\MustBeSimple
* @return void
Expand Down
25 changes: 25 additions & 0 deletions tests/ObjectMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,31 @@ public function testTransforms() {
$this->assertEquals($objectMapped, self::getTree());
}

/**
* @return void
*/
public function testSkipAttributes() {
$inputStructure = self::getTree();

$skipAttributes = [
'name',
'branch.length'
];

$objectMapped = (new Object($inputStructure, new Tree(), Mapper::FILES_ANNOTATION_ADAPTER))
->setSkipAttributes($skipAttributes)
->map();
$this->assertEquals(
$objectMapped,
self::getTree()
->setName(null)
->setBranch(
self::getBranch()
->setLength(null)
)
);
}

/**
* @throws \PhMap\Exception\FieldValidator\MustBeSimple
* @return void
Expand Down

0 comments on commit 1a7d56a

Please sign in to comment.