From f14cc0d24db716089feeda295a6c7ba91e57de06 Mon Sep 17 00:00:00 2001 From: Jurian Sluiman Date: Wed, 5 Mar 2014 10:07:11 +0100 Subject: [PATCH] Add NullValueStrategy to strip empty strings This hydrator strategy removes empty strings and converts them into null. --- .../Hydrator/Strategy/NullValueStrategy.php | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 src/Common/Hydrator/Strategy/NullValueStrategy.php diff --git a/src/Common/Hydrator/Strategy/NullValueStrategy.php b/src/Common/Hydrator/Strategy/NullValueStrategy.php new file mode 100644 index 0000000..a6418d3 --- /dev/null +++ b/src/Common/Hydrator/Strategy/NullValueStrategy.php @@ -0,0 +1,60 @@ + + * @copyright 2013 Jurian Sluiman. + * @license http://www.opensource.org/licenses/bsd-license.php BSD License + * @link http://soflomo.com + */ + +namespace Soflomo\Common\Hydrator\Strategy; + +use Zend\Stdlib\Hydrator\Strategy\DefaultStrategy; + +class NullValueStrategy extends DefaultStrategy +{ + /** + * {@inheritdoc} + * + * Convert an empty string value into null + */ + public function hydrate($value) + { + if (is_string($value) && $value === '') { + $value = null; + } + + return $value; + } +} \ No newline at end of file