-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #238 from craue/3.0.x-steplabel-class
added `StepLabel` class to avoid issues with labels that are also PHP function names
- Loading branch information
Showing
6 changed files
with
226 additions
and
22 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
Exception/StepLabelCallableInvalidReturnValueException.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace Craue\FormFlowBundle\Exception; | ||
|
||
/** | ||
* @author Christian Raue <christian.raue@gmail.com> | ||
* @copyright 2011-2016 Christian Raue | ||
* @license http://opensource.org/licenses/mit-license.php MIT License | ||
*/ | ||
class StepLabelCallableInvalidReturnValueException extends \RuntimeException { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
|
||
namespace Craue\FormFlowBundle\Form; | ||
|
||
use Craue\FormFlowBundle\Exception\InvalidTypeException; | ||
use Craue\FormFlowBundle\Exception\StepLabelCallableInvalidReturnValueException; | ||
|
||
/** | ||
* @author Christian Raue <christian.raue@gmail.com> | ||
* @copyright 2011-2016 Christian Raue | ||
* @license http://opensource.org/licenses/mit-license.php MIT License | ||
*/ | ||
class StepLabel { | ||
|
||
/** | ||
* @var boolean If <code>$value</code> is callable. | ||
*/ | ||
private $callable; | ||
|
||
/** | ||
* @var string|callable|null | ||
*/ | ||
private $value = null; | ||
|
||
/** | ||
* @param string|null $value | ||
*/ | ||
public static function createStringLabel($value) { | ||
return new static($value); | ||
} | ||
|
||
/** | ||
* @param callable $value | ||
*/ | ||
public static function createCallableLabel($value) { | ||
return new static($value, true); | ||
} | ||
|
||
/** | ||
* @return string|null | ||
*/ | ||
public function getText() { | ||
if ($this->callable) { | ||
$returnValue = call_user_func($this->value); | ||
|
||
if ($returnValue === null || is_string($returnValue)) { | ||
return $returnValue; | ||
} | ||
|
||
throw new StepLabelCallableInvalidReturnValueException(); | ||
} | ||
|
||
return $this->value; | ||
} | ||
|
||
/** | ||
* @param string|callable|null $value | ||
* @param boolean $callable | ||
*/ | ||
private function __construct($value, $callable = false) { | ||
$this->setValue($value, $callable); | ||
} | ||
|
||
/** | ||
* @param string|callable|null $value | ||
* @param boolean $callable | ||
*/ | ||
private function setValue($value, $callable = false) { | ||
if ($callable) { | ||
if (!is_callable($value)) { | ||
throw new InvalidTypeException($value, array('callable')); | ||
} | ||
} else { | ||
if ($value !== null && !is_string($value)) { | ||
throw new InvalidTypeException($value, array('null', 'string')); | ||
} | ||
} | ||
|
||
$this->callable = $callable; | ||
$this->value = $value; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
<?php | ||
|
||
namespace Craue\FormFlowBundle\Tests\Form; | ||
|
||
use Craue\FormFlowBundle\Form\StepLabel; | ||
use Craue\FormFlowBundle\Tests\UnitTestCase; | ||
|
||
/** | ||
* @group unit | ||
* | ||
* @author Christian Raue <christian.raue@gmail.com> | ||
* @copyright 2011-2016 Christian Raue | ||
* @license http://opensource.org/licenses/mit-license.php MIT License | ||
*/ | ||
class StepLabelTest extends UnitTestCase { | ||
|
||
/** | ||
* @dataProvider dataCreateStringLabel | ||
*/ | ||
public function testCreateStringLabel($value) { | ||
$this->assertSame($value, StepLabel::createStringLabel($value)->getText()); | ||
} | ||
|
||
public function dataCreateStringLabel() { | ||
return array( | ||
array('label'), | ||
array('date'), | ||
array(null), | ||
); | ||
} | ||
|
||
/** | ||
* @dataProvider dataCreateStringLabel_invalidArgument | ||
* @expectedException \Craue\FormFlowBundle\Exception\InvalidTypeException | ||
*/ | ||
public function testCreateStringLabel_invalidArgument($value) { | ||
StepLabel::createStringLabel($value); | ||
} | ||
|
||
public function dataCreateStringLabel_invalidArgument() { | ||
return array( | ||
array(true), | ||
array(1.1), | ||
array(function() { return 'country'; }), | ||
); | ||
} | ||
|
||
/** | ||
* @dataProvider dataCreateCallableLabel | ||
*/ | ||
public function testCreateCallableLabel($value, $expectedText) { | ||
$this->assertSame($expectedText, StepLabel::createCallableLabel($value)->getText()); | ||
} | ||
|
||
public function dataCreateCallableLabel() { | ||
return array( | ||
array('Craue\FormFlowBundle\Tests\Form\StepLabelTest::_returnString', 'label'), | ||
array('Craue\FormFlowBundle\Tests\Form\StepLabelTest::_returnNull', null), | ||
array(function() { return 'label'; }, 'label'), | ||
); | ||
} | ||
|
||
/** | ||
* @dataProvider dataCreateCallableLabel_invalidArgument | ||
* @expectedException \InvalidArgumentException | ||
*/ | ||
public function testCreateCallableLabel_invalidArgument($value) { | ||
StepLabel::createCallableLabel($value)->getText(); | ||
} | ||
|
||
public function dataCreateCallableLabel_invalidArgument() { | ||
return array( | ||
array('label'), | ||
array('UnknownClass::unknownMethod'), | ||
array(null), | ||
); | ||
} | ||
|
||
/** | ||
* @dataProvider dataGetText_callableInvalidReturnValue | ||
* @expectedException Craue\FormFlowBundle\Exception\StepLabelCallableInvalidReturnValueException | ||
*/ | ||
public function testGetText_callableInvalidReturnValue($value) { | ||
StepLabel::createCallableLabel($value)->getText(); | ||
} | ||
|
||
public function dataGetText_callableInvalidReturnValue() { | ||
return array( | ||
array('Craue\FormFlowBundle\Tests\Form\StepLabelTest::_returnOne'), | ||
array(function() { return 1; }), | ||
); | ||
} | ||
|
||
public static function _returnString() { | ||
return 'label'; | ||
} | ||
|
||
public static function _returnNull() { | ||
return; | ||
} | ||
|
||
public static function _returnOne() { | ||
return 1; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters