Skip to content

Commit

Permalink
Move parameter type construction out of ParamDefinitionFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
JeroenDeDauw committed Aug 2, 2019
1 parent 039b1ad commit 93e4df0
Show file tree
Hide file tree
Showing 8 changed files with 209 additions and 148 deletions.
8 changes: 8 additions & 0 deletions README.md
Expand Up @@ -297,6 +297,14 @@ support the [Maps](https://github.com/JeroenDeDauw/Maps) and

## Release notes

### 1.8.0 (dev)

* Removed `ParamDefinitionFactory::getComponentForType`
* Added `ParamDefinitionFactory` constructor
* Added `ParameterTypes` constructor
* Added `ParameterTypes::addType`
* Added `ParameterTypes::newCoreTypes`

### 1.7.0 (2019-08-02)

* Added `ParameterTypes` public constants: `BOOLEAN`, `FLOAT`, `INTEGER`, `STRING`, `DIMENSION`
Expand Down
16 changes: 7 additions & 9 deletions src/PackagePrivate/Param.php
Expand Up @@ -203,18 +203,16 @@ public function process( array &$definitions, array $params, Options $options )
public function getValueParser( Options $options ): ValueParser {
$parser = $this->definition->getValueParser();

if ( get_class( $parser ) === NullParser::class ) {
$parserType = $options->isStringlyTyped() ? 'string-parser' : 'typed-parser';
if ( !( $parser instanceof NullParser ) ) {
return $parser;
}

// TODO: inject factory
$parserClass = ParamDefinitionFactory::singleton()->getComponentForType( $this->definition->getType(), $parserType );
// TODO: inject factory
$type = ParamDefinitionFactory::singleton()->getType( $this->definition->getType() );

if ( $parserClass !== NullParser::class ) {
$parser = new $parserClass( new \ValueParsers\ParserOptions() );
}
}
$parserClass = $options->isStringlyTyped() ? $type->getStringParserClass() : $type->getTypedParserClass();

return $parser;
return new $parserClass( new \ValueParsers\ParserOptions() );
}

protected function parseAndValidate( Options $options ) {
Expand Down
55 changes: 55 additions & 0 deletions src/PackagePrivate/ParamType.php
@@ -0,0 +1,55 @@
<?php

namespace ParamProcessor\PackagePrivate;

use ParamProcessor\ParamDefinition;
use ValueParsers\NullParser;
use ValueValidators\NullValidator;

class ParamType {

private $typeId;

private $className;
private $stringParser;
private $typedParser;
private $validator;
private $validationCallback;

private function __construct( string $typeId ) {
$this->typeId = $typeId;
}

public static function newFromArray( string $typeId, array $spec ): self {
$type = new self( $typeId );

$type->className = array_key_exists( 'definition', $spec ) ? $spec['definition'] : ParamDefinition::class;
$type->stringParser = array_key_exists( 'string-parser', $spec ) ? $spec['string-parser'] : NullParser::class;
$type->typedParser = array_key_exists( 'typed-parser', $spec ) ? $spec['typed-parser'] : NullParser::class;
$type->validator = array_key_exists( 'validator', $spec ) ? $spec['validator'] : NullValidator::class;
$type->validationCallback = array_key_exists( 'validation-callback', $spec ) ? $spec['validation-callback'] : null;

return $type;
}

public function getClassName(): string {
return $this->className;
}

public function getValidatorClass(): string {
return $this->validator;
}

public function getValidationCallback(): ?callable {
return $this->validationCallback;
}

public function getStringParserClass(): string {
return $this->stringParser;
}

public function getTypedParserClass(): string {
return $this->typedParser;
}

}
86 changes: 21 additions & 65 deletions src/ParamDefinitionFactory.php
Expand Up @@ -4,7 +4,7 @@

use Exception;
use OutOfBoundsException;
use ValueParsers\NullParser;
use ParamProcessor\PackagePrivate\ParamType;
use ValueValidators\NullValidator;

/**
Expand All @@ -15,37 +15,22 @@
*/
class ParamDefinitionFactory {

/**
* Maps parameter type to handling ParameterDefinition implementing class.
*
* @since 1.0
*
* @var array
*/
private $typeToClass = [];
private $types;

/**
* Maps parameter type to its associated components.
*
* @since 1.0
*
* @var array
* @since 1.8
*/
private $typeToComponent = [];
public function __construct( ParameterTypes $types = null ) {
$this->types = $types ?? new ParameterTypes();
}

/**
* Returns a ParamDefinitionFactory that already has the core parameter types (@see ParameterTypes) registered.
*
* @since 1.6
*/
public static function newDefault(): self {
$instance = new self();

foreach ( ParameterTypes::getCoreTypes() as $type => $data ) {
$instance->registerType( $type, $data );
}

return $instance;
return new self( ParameterTypes::newCoreTypes() );
}

/**
Expand Down Expand Up @@ -101,34 +86,19 @@ public function registerGlobals() {
* @return boolean DEPRECATED since 1.6 - Indicates if the type was registered
*/
public function registerType( $type, array $data ) {
if ( array_key_exists( $type, $this->typeToClass ) ) {
if ( $this->types->hasType( $type ) ) {
return false;
}

// Deprecated: definition key
$class = array_key_exists( 'definition', $data ) ? $data['definition'] : ParamDefinition::class;
$this->typeToClass[$type] = $class;

$defaults = [
'string-parser' => NullParser::class,
'typed-parser' => NullParser::class,
'validator' => NullValidator::class,
'validation-callback' => null,
];

$this->typeToComponent[$type] = [];

foreach ( $defaults as $component => $default ) {
$this->typeToComponent[$type][$component] = array_key_exists( $component, $data ) ? $data[$component] : $default;
}
$this->types->addType( $type, $data );

return true;
}

/**
* Creates a new instance of a ParamDefinition based on the provided type.
*
* @param string $type
* @param string $typeName
* @param string $name
* @param mixed $default
* @param string $message
Expand All @@ -137,31 +107,32 @@ public function registerType( $type, array $data ) {
* @return ParamDefinition
* @throws OutOfBoundsException
*/
public function newDefinition( string $type, string $name, $default, string $message, bool $isList = false ): ParamDefinition {
if ( !array_key_exists( $type, $this->typeToClass ) ) {
throw new OutOfBoundsException( 'Unknown parameter type "' . $type . '".' );
public function newDefinition( string $typeName, string $name, $default, string $message, bool $isList = false ): ParamDefinition {
if ( !$this->types->hasType( $typeName ) ) {
throw new OutOfBoundsException( 'Unknown parameter type "' . $typeName . '".' );
}

$class = $this->typeToClass[$type];
$type = $this->types->getType( $typeName );
$class = $type->getClassName();

/**
* @var ParamDefinition $definition
*/
$definition = new $class(
$type,
$typeName,
$name,
$default,
$message,
$isList
);

$validator = $this->typeToComponent[$type]['validator'];
$validator = $type->getValidatorClass();

if ( $validator !== NullValidator::class ) {
$definition->setValueValidator( new $validator() );
}

$validationCallback = $this->typeToComponent[$type]['validation-callback'];
$validationCallback = $type->getValidationCallback();

if ( $validationCallback !== null ) {
$definition->setValidationCallback( $validationCallback );
Expand All @@ -171,25 +142,10 @@ public function newDefinition( string $type, string $name, $default, string $mes
}

/**
* Returns the specified component for the provided parameter type.
* This method is likely to change in the future in a compat breaking way.
*
* @param string $paramType
* @param string $componentType
*
* @throws Exception
* @return mixed
* Package private
*/
public function getComponentForType( $paramType, $componentType ) {
if ( !array_key_exists( $paramType, $this->typeToComponent ) ) {
throw new Exception( 'Unknown parameter type "' . $paramType . '".' );
}

if ( !array_key_exists( $componentType, $this->typeToComponent[$paramType] ) ) {
throw new Exception( 'Unknown parameter component type "' . $paramType . '".' );
}

return $this->typeToComponent[$paramType][$componentType];
public function getType( string $typeName ): ParamType {
return $this->types->getType( $typeName );
}

/**
Expand Down
43 changes: 43 additions & 0 deletions src/ParameterTypes.php
Expand Up @@ -6,6 +6,7 @@

use ParamProcessor\Definition\DimensionParam;
use ParamProcessor\Definition\StringParam;
use ParamProcessor\PackagePrivate\ParamType;
use ValueParsers\BoolParser;
use ValueParsers\FloatParser;
use ValueParsers\IntParser;
Expand All @@ -28,6 +29,48 @@ class ParameterTypes {
public const STRING = 'string';
public const DIMENSION = 'dimension';

/**
* @var ParamType[]
*/
private $types = [];

/**
* @param array[] $typeSpecs
*/
public function __construct( array $typeSpecs = [] ) {
foreach ( $typeSpecs as $typeName => $typeSpec ) {
$this->addType( $typeName, $typeSpec );
}
}

/**
* @since 1.8
*/
public function addType( string $typeName, array $typeSpec ) {
$this->types[$typeName] = ParamType::newFromArray( $typeName, $typeSpec );
}

/**
* Package private
*/
public function hasType( string $typeName ): bool {
return array_key_exists( $typeName, $this->types );
}

/**
* Package private
*/
public function getType( string $typeName ): ParamType {
return $this->types[$typeName];
}

/**
* @since 1.8
*/
public static function newCoreTypes(): self {
return new self( self::getCoreTypes() );
}

/**
* @since 1.4
*/
Expand Down
11 changes: 0 additions & 11 deletions tests/Integration/Definitions/StringParamTest.php
Expand Up @@ -8,17 +8,6 @@
*/
class StringParamTest extends ParamDefinitionTest {

/**
* @see ParamDefinitionTest::getDefinitions
*/
public function getDefinitions() {
$params = parent::getDefinitions();



return $params;
}

/**
* @see ParamDefinitionTest::valueProvider
*
Expand Down

0 comments on commit 93e4df0

Please sign in to comment.