public function testArgumentCollectionCreateAndWalk()
 {
     $collection = new ArgumentCollection(new Argument('arg0', new TypeCollection(new Type\AnyType())), new Argument('arg1', new TypeCollection(new Type\AnyType())));
     $collection->addArgument(new Argument('arg2', new TypeCollection(new Type\AnyType())));
     $this->assertTrue($collection->getCount() == 3);
     $this->assertTrue($collection->key() == 'arg0');
     $collection->next();
     $this->assertTrue($collection->key() == 'arg1');
     $collection->next();
     $this->assertTrue($collection->key() == 'arg2');
     $collection->next();
     $this->assertFalse($collection->valid());
     $collection->rewind();
     $this->assertTrue($collection->key() == 'arg0');
 }
Example #2
0
 /**
  * Initialize the loop arguments.
  *
  * @param array $nameValuePairs a array of name => value pairs. The name is the name of the argument.
  *
  * @throws \InvalidArgumentException if some argument values are missing, or invalid
  */
 public function initializeArgs(array $nameValuePairs)
 {
     $faultActor = [];
     $faultDetails = [];
     if (null !== ($eventName = $this->getDispatchEventName(TheliaEvents::LOOP_EXTENDS_INITIALIZE_ARGS))) {
         $event = new LoopExtendsInitializeArgsEvent($this, $nameValuePairs);
         $this->dispatcher->dispatch($eventName, $event);
         $nameValuePairs = $event->getLoopParameters();
     }
     $loopType = isset($nameValuePairs['type']) ? $nameValuePairs['type'] : "undefined";
     $loopName = isset($nameValuePairs['name']) ? $nameValuePairs['name'] : "undefined";
     $this->args->rewind();
     while (($argument = $this->args->current()) !== false) {
         $this->args->next();
         $value = isset($nameValuePairs[$argument->name]) ? $nameValuePairs[$argument->name] : null;
         /* check if mandatory */
         if ($value === null && $argument->mandatory) {
             $faultActor[] = $argument->name;
             $faultDetails[] = $this->translator->trans('"%param" parameter is missing in loop type: %type, name: %name', ['%param' => $argument->name, '%type' => $loopType, '%name' => $loopName]);
         } elseif ($value === '') {
             if (!$argument->empty) {
                 /* check if empty */
                 $faultActor[] = $argument->name;
                 $faultDetails[] = $this->translator->trans('"%param" parameter cannot be empty in loop type: %type, name: %name', ['%param' => $argument->name, '%type' => $loopType, '%name' => $loopName]);
             }
         } elseif ($value !== null && !$argument->type->isValid($value)) {
             /* check type */
             $faultActor[] = $argument->name;
             $faultDetails[] = $this->translator->trans('Invalid value "%value" for "%param" parameter in loop type: %type, name: %name', ['%value' => $value, '%param' => $argument->name, '%type' => $loopType, '%name' => $loopName]);
         } else {
             /* set default value */
             /* did it as last checking for we consider default value is acceptable no matter type or empty restriction */
             if ($value === null) {
                 $value = $argument->default;
             }
             $argument->setValue($value);
         }
     }
     if (!empty($faultActor)) {
         $complement = sprintf('[%s]', implode(', ', $faultDetails));
         throw new \InvalidArgumentException($complement);
     }
 }