-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[2.2] [Config] Better handling for numerical values: #4714
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Config\Definition\Builder; | ||
|
||
use Symfony\Component\Config\Definition\FloatNode; | ||
|
||
/** | ||
* This class provides a fluent interface for defining a float node. | ||
* | ||
* @author Jeanmonod David <david.jeanmonod@gmail.com> | ||
*/ | ||
class FloatNodeDefinition extends NumericNodeDefinition | ||
{ | ||
/** | ||
* Instantiate a Node | ||
* | ||
* @return FloatNode The node | ||
*/ | ||
protected function instantiateNode() | ||
{ | ||
return new FloatNode($this->name, $this->parent, $this->min, $this->max); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Config\Definition\Builder; | ||
|
||
use Symfony\Component\Config\Definition\IntegerNode; | ||
|
||
/** | ||
* This class provides a fluent interface for defining an integer node. | ||
* | ||
* @author Jeanmonod David <david.jeanmonod@gmail.com> | ||
*/ | ||
class IntegerNodeDefinition extends NumericNodeDefinition | ||
{ | ||
/** | ||
* Instantiate a Node | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. need a trailing dot. |
||
* | ||
* @return IntegerNode The node | ||
*/ | ||
protected function instantiateNode() | ||
{ | ||
return new IntegerNode($this->name, $this->parent, $this->min, $this->max); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Config\Definition\Builder; | ||
|
||
/** | ||
* Abstract class that contain common code of integer and float node definition. | ||
* | ||
* @author David Jeanmonod <david.jeanmonod@gmail.com> | ||
*/ | ||
abstract class NumericNodeDefinition extends ScalarNodeDefinition | ||
{ | ||
|
||
protected $min; | ||
protected $max; | ||
|
||
/** | ||
* Ensure the value is smaller than the given reference | ||
* | ||
* @param mixed $max | ||
* | ||
* @return NumericNodeDefinition | ||
*/ | ||
public function max($max) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing phpdoc for \InvalidArgumentException (same on |
||
{ | ||
if (isset($this->min) && $this->min > $max) { | ||
throw new \InvalidArgumentException(sprintf('You cannot define a max(%s) as you already have a min(%s)', $max, $this->min)); | ||
} | ||
$this->max = $max; | ||
|
||
return $this; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing blank line here. |
||
} | ||
|
||
/** | ||
* Ensure the value is bigger than the given reference | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. need a trailing dot. |
||
* | ||
* @param mixed $min | ||
* | ||
* @return NumericNodeDefinition | ||
*/ | ||
public function min($min) | ||
{ | ||
if (isset($this->max) && $this->max < $min) { | ||
throw new \InvalidArgumentException(sprintf('You cannot define a min(%s) as you already have a max(%s)', $min, $this->max)); | ||
} | ||
$this->min = $min; | ||
|
||
return $this; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Config\Definition; | ||
|
||
use Symfony\Component\Config\Definition\Exception\InvalidTypeException; | ||
|
||
/** | ||
* This node represents a float value in the config tree. | ||
* | ||
* @author Jeanmonod David <david.jeanmonod@gmail.com> | ||
*/ | ||
class FloatNode extends NumericNode | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
protected function validateType($value) | ||
{ | ||
// Integers are also accepted, we just cast them | ||
if (is_int($value)) { | ||
$value = (float) $value; | ||
} | ||
|
||
if (!is_float($value)) { | ||
$ex = new InvalidTypeException(sprintf( | ||
'Invalid type for path "%s". Expected float, but got %s.', | ||
$this->getPath(), | ||
gettype($value) | ||
)); | ||
$ex->setPath($this->getPath()); | ||
|
||
throw $ex; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Config\Definition; | ||
|
||
use Symfony\Component\Config\Definition\Exception\InvalidTypeException; | ||
|
||
/** | ||
* This node represents an integer value in the config tree. | ||
* | ||
* @author Jeanmonod David <david.jeanmonod@gmail.com> | ||
*/ | ||
class IntegerNode extends NumericNode | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
protected function validateType($value) | ||
{ | ||
if (!is_int($value)) { | ||
$ex = new InvalidTypeException(sprintf( | ||
'Invalid type for path "%s". Expected int, but got %s.', | ||
$this->getPath(), | ||
gettype($value) | ||
)); | ||
$ex->setPath($this->getPath()); | ||
|
||
throw $ex; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Config\Definition; | ||
|
||
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; | ||
|
||
/** | ||
* This node represents a numeric value in the config tree | ||
* | ||
* @author David Jeanmonod <david.jeanmonod@gmail.com> | ||
*/ | ||
class NumericNode extends ScalarNode | ||
{ | ||
protected $min; | ||
protected $max; | ||
|
||
public function __construct($name, NodeInterface $parent = null, $min = null, $max = null) | ||
{ | ||
parent::__construct($name, $parent); | ||
$this->min = $min; | ||
$this->max = $max; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
protected function finalizeValue($value) | ||
{ | ||
$value = parent::finalizeValue($value); | ||
|
||
$errorMsg = null; | ||
if (isset($this->min) && $value < $this->min) { | ||
$errorMsg = sprintf('The value %s is too small for path "%s". Should be greater than: %s', $value, $this->getPath(), $this->min); | ||
} | ||
if (isset($this->max) && $value > $this->max) { | ||
$errorMsg = sprintf('The value %s is too big for path "%s". Should be less than: %s', $value, $this->getPath(), $this->max); | ||
} | ||
if (isset($errorMsg)) { | ||
$ex = new InvalidConfigurationException($errorMsg); | ||
$ex->setPath($this->getPath()); | ||
throw $ex; | ||
} | ||
|
||
return $value; | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need a trailing dot.