Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 439db1b

Browse files
author
andreasschacht
committed
add a flag indicates to skip null values, adding possibility to postprocess generated array
1 parent f481a8e commit 439db1b

2 files changed

Lines changed: 98 additions & 1 deletion

File tree

src/Sp/FixtureDumper/Generator/AbstractGenerator.php

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,23 @@ abstract class AbstractGenerator
5050
*/
5151
protected $models;
5252

53+
/**
54+
* @var array
55+
*/
5356
protected $propertyReader = array();
5457

58+
/**
59+
* @var array
60+
*/
61+
protected $fieldNamesPostProcessors = array();
62+
63+
/**
64+
* @var bool
65+
*/
66+
protected $skipNullValues = false;
67+
68+
69+
5570

5671
/**
5772
* @param \Doctrine\Common\Persistence\ObjectManager|null $manager
@@ -75,6 +90,36 @@ public function addPropertyReader(PropertyReaderInterface $propertyReader)
7590
$this->propertyReader[] = $propertyReader;
7691
}
7792

93+
/**
94+
* adds a postprocessor
95+
*
96+
* @param FieldNamesPostProcessorInterface $postProcessor
97+
*/
98+
public function addFieldNamesPostProcessor(FieldNamesPostProcessorInterface $postProcessor)
99+
{
100+
$this->fieldNamesPostProcessors[] = $postProcessor;
101+
}
102+
103+
/**
104+
* @return boolean
105+
*/
106+
public function isSkipNullValues()
107+
{
108+
return $this->skipNullValues;
109+
}
110+
111+
/**
112+
* if set to true, null values won't be dumped
113+
*
114+
* @param boolean $skipNullValues
115+
*/
116+
public function setSkipNullValues($skipNullValues)
117+
{
118+
$this->skipNullValues = $skipNullValues;
119+
}
120+
121+
122+
78123
/**
79124
* @param \Doctrine\Common\Persistence\Mapping\ClassMetadata $metadata
80125
* @param array|null $models
@@ -235,8 +280,22 @@ protected function processFieldNames(ClassMetadata $metadata, $model)
235280
continue;
236281
}
237282

238-
$data[$fieldName] = $this->navigator->accept($this->getVisitor(), $this->readProperty($model, $fieldName));
283+
$value = $this->readProperty($model, $fieldName);
284+
if ($this->skipNullValues && $value === null) {
285+
continue;
286+
}
287+
288+
$data[$fieldName] = $this->navigator->accept($this->getVisitor(), $value);
239289
}
290+
291+
foreach ($this->fieldNamesPostProcessors as $postProcessor) {
292+
/** @var FieldNamesPostProcessorInterface $postProcessor */
293+
if ($postProcessor->isSupporting($model, $metadata, $data)) {
294+
$data = $postProcessor->getData($model, $metadata, $data);
295+
}
296+
}
297+
298+
240299

241300
return $data;
242301
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: andreasschacht
5+
* Date: 01.08.16
6+
* Time: 11:27
7+
*/
8+
9+
namespace Sp\FixtureDumper\Generator;
10+
11+
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
12+
13+
interface FieldNamesPostProcessorInterface
14+
{
15+
16+
/**
17+
* Indicates if this class supports processing data
18+
*
19+
* @param $object
20+
* @param ClassMetaData $metadata
21+
* @param array $data
22+
*
23+
*
24+
* @return boolean true, if this class can process the data
25+
*/
26+
public function isSupporting($object, ClassMetaData $metadata, array $data);
27+
28+
/**
29+
* returns the processed data
30+
*
31+
* @param $object
32+
* @param ClassMetaData $metadata
33+
* @param array $data
34+
*
35+
* @return array
36+
*/
37+
public function getData($object, ClassMetaData $metadata, array $data);
38+
}

0 commit comments

Comments
 (0)