Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Merge branch 'feature/7263'
Browse files Browse the repository at this point in the history
  • Loading branch information
Show file tree
Hide file tree
Showing 10 changed files with 628 additions and 484 deletions.
11 changes: 9 additions & 2 deletions src/FilterPluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
*/
class FilterPluginManager extends AbstractPluginManager
{
protected $aliases = array(
'Zend\Filter\Int' => 'Zend\Filter\ToInt',
'Zend\Filter\Null' => 'Zend\Filter\ToNull',
);

/**
* Default set of plugins factories
*
Expand Down Expand Up @@ -68,9 +73,9 @@ class FilterPluginManager extends AbstractPluginManager
'fileuppercase' => 'Zend\Filter\File\UpperCase',
'htmlentities' => 'Zend\Filter\HtmlEntities',
'inflector' => 'Zend\Filter\Inflector',
'int' => 'Zend\Filter\Int',
'int' => 'Zend\Filter\ToInt',
'monthselect' => 'Zend\Filter\MonthSelect',
'null' => 'Zend\Filter\Null',
'null' => 'Zend\Filter\ToNull',
'numberformat' => 'Zend\I18n\Filter\NumberFormat',
'numberparse' => 'Zend\I18n\Filter\NumberParse',
'pregreplace' => 'Zend\Filter\PregReplace',
Expand All @@ -80,6 +85,8 @@ class FilterPluginManager extends AbstractPluginManager
'stringtrim' => 'Zend\Filter\StringTrim',
'stripnewlines' => 'Zend\Filter\StripNewlines',
'striptags' => 'Zend\Filter\StripTags',
'toint' => 'Zend\Filter\ToInt',
'tonull' => 'Zend\Filter\ToNull',
'urinormalize' => 'Zend\Filter\UriNormalize',
'whitelist' => 'Zend\Filter\Whitelist',
'wordcamelcasetodash' => 'Zend\Filter\Word\CamelCaseToDash',
Expand Down
38 changes: 20 additions & 18 deletions src/Int.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,27 @@

namespace Zend\Filter;

class Int extends AbstractFilter
/**
* Stub class for backwards compatibility.
*
* Since PHP 7 adds "int" as a reserved keyword, we can no longer have a class
* named that and retain PHP 7 compatibility. The original class has been
* renamed to "ToInt", and this class is now an extension of it. It raises an
* E_USER_DEPRECATED to warn users to migrate.
*
* @deprecated
*/
class Int extends ToInt
{
/**
* Defined by Zend\Filter\FilterInterface
*
* Returns (int) $value
*
* If the value provided is non-scalar, the value will remain unfiltered
*
* @param string $value
* @return int|mixed
*/
public function filter($value)
public function __construct()
{
if (!is_scalar($value)) {
return $value;
}
$value = (string) $value;

return (int) $value;
trigger_error(
sprintf(
'The class %s has been deprecated; please use %s\\ToInt',
__CLASS__,
__NAMESPACE__
),
E_USER_DEPRECATED
);
}
}
181 changes: 22 additions & 159 deletions src/Null.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,169 +9,32 @@

namespace Zend\Filter;

use Traversable;

class Null extends AbstractFilter
/**
* Stub class for backwards compatibility.
*
* Since PHP 7 adds "null" as a reserved keyword, we can no longer have a class
* named that and retain PHP 7 compatibility. The original class has been
* renamed to "ToNull", and this class is now an extension of it. It raises an
* E_USER_DEPRECATED to warn users to migrate.
*
* @deprecated
*/
class Null extends ToNull
{
const TYPE_BOOLEAN = 1;
const TYPE_INTEGER = 2;
const TYPE_EMPTY_ARRAY = 4;
const TYPE_STRING = 8;
const TYPE_ZERO_STRING = 16;
const TYPE_FLOAT = 32;
const TYPE_ALL = 63;

/**
* @var array
*/
protected $constants = array(
self::TYPE_BOOLEAN => 'boolean',
self::TYPE_INTEGER => 'integer',
self::TYPE_EMPTY_ARRAY => 'array',
self::TYPE_STRING => 'string',
self::TYPE_ZERO_STRING => 'zero',
self::TYPE_FLOAT => 'float',
self::TYPE_ALL => 'all',
);

/**
* @var array
*/
protected $options = array(
'type' => self::TYPE_ALL,
);

/**
* Constructor
*
* @param string|array|Traversable $typeOrOptions OPTIONAL
* {@inheritdoc}
*/
public function __construct($typeOrOptions = null)
{
if ($typeOrOptions !== null) {
if ($typeOrOptions instanceof Traversable) {
$typeOrOptions = iterator_to_array($typeOrOptions);
}

if (is_array($typeOrOptions)) {
if (isset($typeOrOptions['type'])) {
$this->setOptions($typeOrOptions);
} else {
$this->setType($typeOrOptions);
}
} else {
$this->setType($typeOrOptions);
}
}
}

/**
* Set boolean types
*
* @param int|array $type
* @throws Exception\InvalidArgumentException
* @return self
*/
public function setType($type = null)
{
if (is_array($type)) {
$detected = 0;
foreach ($type as $value) {
if (is_int($value)) {
$detected += $value;
} elseif (in_array($value, $this->constants)) {
$detected += array_search($value, $this->constants);
}
}

$type = $detected;
} elseif (is_string($type) && in_array($type, $this->constants)) {
$type = array_search($type, $this->constants);
}

if (!is_int($type) || ($type < 0) || ($type > self::TYPE_ALL)) {
throw new Exception\InvalidArgumentException(sprintf(
'Unknown type value "%s" (%s)',
$type,
gettype($type)
));
}

$this->options['type'] = $type;
return $this;
}

/**
* Returns defined boolean types
*
* @return int
*/
public function getType()
{
return $this->options['type'];
}

/**
* Defined by Zend\Filter\FilterInterface
*
* Returns null representation of $value, if value is empty and matches
* types that should be considered null.
*
* @param string $value
* @return string
*/
public function filter($value)
{
$type = $this->getType();

// FLOAT (0.0)
if ($type >= self::TYPE_FLOAT) {
$type -= self::TYPE_FLOAT;
if (is_float($value) && ($value == 0.0)) {
return;
}
}

// STRING ZERO ('0')
if ($type >= self::TYPE_ZERO_STRING) {
$type -= self::TYPE_ZERO_STRING;
if (is_string($value) && ($value == '0')) {
return;
}
}

// STRING ('')
if ($type >= self::TYPE_STRING) {
$type -= self::TYPE_STRING;
if (is_string($value) && ($value == '')) {
return;
}
}

// EMPTY_ARRAY (array())
if ($type >= self::TYPE_EMPTY_ARRAY) {
$type -= self::TYPE_EMPTY_ARRAY;
if (is_array($value) && ($value == array())) {
return;
}
}

// INTEGER (0)
if ($type >= self::TYPE_INTEGER) {
$type -= self::TYPE_INTEGER;
if (is_int($value) && ($value == 0)) {
return;
}
}

// BOOLEAN (false)
if ($type >= self::TYPE_BOOLEAN) {
$type -= self::TYPE_BOOLEAN;
if (is_bool($value) && ($value == false)) {
return;
}
}

return $value;
trigger_error(
sprintf(
'The class %s has been deprecated; please use %s\\ToNull',
__CLASS__,
__NAMESPACE__
),
E_USER_DEPRECATED
);

parent::__construct($typeOrOptions);
}
}
33 changes: 33 additions & 0 deletions src/ToInt.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\Filter;

class ToInt extends AbstractFilter
{
/**
* Defined by Zend\Filter\FilterInterface
*
* Returns (int) $value
*
* If the value provided is non-scalar, the value will remain unfiltered
*
* @param string $value
* @return int|mixed
*/
public function filter($value)
{
if (!is_scalar($value)) {
return $value;
}
$value = (string) $value;

return (int) $value;
}
}
Loading

0 comments on commit d66e855

Please sign in to comment.