| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | namespace Beanie\Command\CommandLineCreator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Beanie\Exception\InvalidArgumentException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | class GenericCommandLineCreator implements CommandLineCreatorInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     /** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     private $commandName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     /** @var array */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     protected $arguments; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     /** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     private $data = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * @param string $commandName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @param array $arguments | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @param array $argumentDefaults | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 | 53 |  |     public function __construct($commandName, array $arguments, array $argumentDefaults = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 | 53 |  |         $this->commandName = $commandName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 53 |  |         $this->initArguments(array_values($arguments), $argumentDefaults); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 52 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 53 |  |     private function initArguments($arguments, $defaults) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 53 |  |         foreach (array_keys($defaults) as $index => $key) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 40 |  |             $arguments[$index] = $this->extractDefault($index, $key, $arguments, $defaults); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 52 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 52 |  |         $this->setArguments($arguments); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 52 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @param int $index | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @param string $key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @param array $arguments | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @param array $defaults | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @throws InvalidArgumentException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 40 |  |     private function extractDefault($index, $key, $arguments, $defaults) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 40 |  |         if (!(isset($arguments[$index]) || isset($defaults[$key]))) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 1 |  |             throw new InvalidArgumentException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 1 |  |                 sprintf('Argument \'%s\' is required for \'%s\'', $key, $this->commandName) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 1 |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 39 |  |         return isset($arguments[$index]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 39 |  |             ? $arguments[$index] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 39 |  |             : $defaults[$key]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @inheritdoc | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 63 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 64 | 8 |  |     public function hasData() | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 66 | 8 |  |         return $this->data !== null; | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 8 |  |     public function getData() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 8 |  |         return $this->data; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 37 |  |     public function getCommandLine() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 37 |  |         return join(' ', array_merge([$this->commandName], $this->arguments)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * @param array $arguments | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 52 |  |     protected function setArguments(array $arguments) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 52 |  |         $this->arguments = $arguments; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 52 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * @param string $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 7 |  |     protected function setData($data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 7 |  |         $this->data = $data; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 7 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 100 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 101 |  |  |  |