Ejemplo n.º 1
0
 /**
  * Parse scope arguments from request parameters
  *
  * @param  array $scope
  * @return array
  */
 public function parseScopeArguments(array $scope)
 {
     $scope = new MosaicArray($scope);
     $result = [];
     // If "type" key has been provided, we have to typecast the result
     $type = $scope->getItem('type');
     // Get default scope argument value
     $default = $scope->getItem('default');
     // Get request parameter value
     $value = $this->getInputManager()->get($scope['alias'], null);
     // If there are no parameters with the scope name:
     // 1) in a case when no default value is set, return null to ignore this scope
     // 2) if default value is set, return it
     if (is_null($value)) {
         return !is_null($default) ? [$default] : null;
     }
     // If "keys" configuration key has been provided, we are dealing with an array parameter (e.g. <input name="somename[somekey]">)
     $keys = $scope->getItem('keys');
     // If "keys" are empty, we have to perform some DRY actions
     if (is_null($keys)) {
         $keys = ['default'];
         $value = ['default' => $value];
     }
     foreach ((array) $keys as $key) {
         $arg = $this->setType($value[$key], $type);
         // Empty arguments are not allowed by default in order to allow default scope argument values
         // Set allowEmpty option to `true` if you want to change this behavior
         if ($arg !== '' or $scope->getItem('allowEmpty')) {
             $result[] = $arg;
         }
     }
     return $result;
 }
Ejemplo n.º 2
0
 /**
  * @covers Mobileka\MosaicArray\MosaicArray::getItem
  */
 public function test_gets_item_if_exists_or_returns_default_result()
 {
     $ma = new MosaicArray($this->target);
     // numbers
     $expect = range(1, 5);
     $result = $ma->getItem('numbers');
     assertEquals($expect, $result);
     // no key
     $expect = null;
     $result = $ma->getItem('no such key');
     assertEquals($expect, $result);
     // custom result
     $expect = 'custom result';
     $result = $ma->getItem('no such key', 'custom result');
     assertEquals($expect, $result);
     // negative condition
     $expect = range(1, 5);
     $result = $ma->getItem('no such key', false);
     assertNotEquals($expect, $result);
 }
Ejemplo n.º 3
0
 /**
  * A scope with an optional argument
  *
  * @param  string $six
  * @return static
  */
 public function six($six = 'six')
 {
     $this->data->replaceTarget($this->data->getItem($six, []));
     return $this;
 }