Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 53 additions & 4 deletions src/Laracasts/Commander/CommanderTrait.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<?php namespace Laracasts\Commander;

use ReflectionClass;
use InvalidArgumentException;
use Input, App;
use InvalidArgumentException;

trait CommanderTrait {

Expand Down Expand Up @@ -56,10 +55,28 @@ public function getCommandBus()
*/
protected function mapInputToCommand($command, array $input)
{
$dependencies = [];

$class = new ReflectionClass($command);

if (is_null($class->getConstructor())) {
return $this->mapInputToCommandProperties($input, $class);
} else {
return $this->mapInputToCommandConstructor($input, $class);
}
}

/**
* Map an array of input to a command's properties via its constructor
*
* @param array $input
* @param $class
* @throws InvalidArgumentException
*
* @return mixed
*/
protected function mapInputToCommandConstructor(array $input, $class)
{
$dependencies = [];

foreach ($class->getConstructor()->getParameters() as $parameter)
{
$name = $parameter->getName();
Expand All @@ -81,4 +98,36 @@ protected function mapInputToCommand($command, array $input)
return $class->newInstanceArgs($dependencies);
}

/**
* Map an array of input to a command's properties via its public properties
*
* @param array $input
* @param $class
*
* @return mixed
*/
protected function mapInputToCommandProperties(array $input, $class)
{
$instance = $class->newInstance();

foreach ($class->getProperties(\ReflectionProperty::IS_PUBLIC) as $parameter)
{
$name = $parameter->getName();

if (array_key_exists($name, $input))
{
$instance->$name = $input[$name];
}
else
{
// if parameter has no default value
if(is_null($instance->$name))
{
throw new InvalidArgumentException("Unable to map input to command: {$name}");
}
}
}

return $instance;
}
}