/** * 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; }
/** * @covers Mobileka\MosaicArray\MosaicArray::replaceTarget() */ public function test_replaces_target_array() { $ma = new MosaicArray($this->target); $expect = ['some value']; $result = $ma->replaceTarget($expect)->toArray(); assertEquals($expect, $result); assertNotEquals($this->target, $result); }
<?php use Mobileka\MosaicArray\MosaicArray; return MosaicArray::make(['firstCase' => ['allowedScopes' => ['scope', 'anotherScope'], 'result' => ['scope' => ['alias' => 'scope'], 'anotherScope' => ['alias' => 'anotherScope']]], 'secondCase' => ['allowedScopes' => ['scope' => ['alias' => 'scopeAlias', 'keys' => ['firstKey', 'secondKey'], 'type' => 'int']], 'result' => ['scope' => ['alias' => 'scopeAlias', 'keys' => ['firstKey', 'secondKey'], 'type' => 'int']]], 'thirdCase' => ['allowedScopes' => ['scope' => ['type' => 'boolean']], 'result' => ['scope' => ['alias' => 'scope', 'type' => 'boolean']]]]);
/** * @return array */ public function get() { return $this->data->toArray(); }