public function request(Request $request)
 {
     $statusCode = f\get_or($request->getParameters(), 'statusCode', 200);
     $content = $request->getParameters() ? json_encode($request->getParameters()) : $request->getContent();
     $headers = $request->getHeaders();
     return new Response($statusCode, $content, $headers);
 }
Example #2
0
/**
 * f\get_in_or($coll, $in, $default)
 *
 * Returns a element of a collection in a nested structure in.
 * The default is returned if the in does not exist.
 *
 * f\get_in_or(array('a' => array('a1' => 'foo'), array('a', 'a1'));
 * => 'foo'
 *
 * f\get_in_or(array('a' => array('a1' => 'foo'), array('a', 'a2'), 'bar');
 * => 'bar'
 */
function get_in_or($coll, $in, $default)
{
    $arrayIn = f\_coll_in($coll, $in);
    if ($arrayIn === false) {
        return $default;
    }
    return f\get_or($arrayIn, f\last($in), $default);
}
Example #3
0
/**
 * f\fill($coll, $paramRules)
 *
 * Returns a new collection filled with param rules.
 * If a param is optional and it does not exist and there is a default value, it's filled.
 * If a param exists and there is no param rule for that param, it's not filled.
 *
 * // filling with empty coll
 * f\fill(array(), array('a' => f\optional(array('d' => 1)));
 * => array('a' => 1)
 *
 * // filling with existing coll
 * f\fill(array('a' => 1), array('a' => f\required(), 'b' => f\optional(array('d' => 2)));
 * => array('a' => 1, 'b' => 2)
 *
 * // without param rule
 * f\fill(array('a' => 1, 'b' => 2), array('a' => f\required()));
 * => array()
 */
function fill($coll, $paramRules)
{
    $fill = function ($rule, $key) use($coll) {
        if (f\not($rule instanceof param_rule)) {
            throw new \InvalidArgumentException('Fill rules must be created with felpado\\required and felpado\\optional.');
        }
        if ($rule instanceof optional) {
            $value = f\get_or($coll, $key, $rule->getDefaultValue());
        } else {
            $value = f\get($coll, $key);
        }
        return $value;
    };
    return f\map_indexed($fill, $paramRules);
}
Example #4
0
/**
 * f\assoc_in($coll, $in, $value)
 *
 * Returns an array based on coll with value associated in the nested structure of in.
 *
 * f\assoc_in(array('a' => 1), array('b', 'b1'), 2);
 * => array('a' => 1, 'b' => array('b1' => 2))
 *
 * // does nothing without in
 * f\assoc_in(array('a' => 1), array(), 2);
 * => array('a' => 1)
 *
 * // supports infinite nesting
 * f\assoc_in(array(), array('a', 'a1', 'a1I', 'a1IA'), 1);
 * => array('a' => array('a1' => array('a1I' => array('a1IA' => 1))))
 */
function assoc_in($coll, $in, $value)
{
    if (empty($in)) {
        return $coll;
    }
    $array = f\to_array($coll);
    $current =& $array;
    foreach ($in as $k) {
        if (f\not(f\get_or($current, $k, null))) {
            $current = f\assoc($current, $k, array());
        }
        $current =& $current[$k];
    }
    $current = $value;
    return $array;
}
Example #5
0
 /**
  * @Then /^the response header "(.*)" should be "(.*)"$/
  */
 public function checkResponseHeader($name, $expectedValue)
 {
     $value = f\get_or($this->response->getHeaders(), $name, null);
     if ($value !== $expectedValue) {
         throw new Exception(sprintf('The response header "%s" is "%s" and it should be "%s".', $name, $value, $expectedValue));
     }
 }
Example #6
0
 public function getScopes(array $inputData)
 {
     $string = f\get_or($inputData, 'scope', null);
     return ScopeCollection::createFromString($string);
 }
Example #7
0
 /**
  * @dataProvider provideGet
  */
 public function testGetOrReturnsTheDefaultIfTheCollectionDoesNotContainTheKey($coll)
 {
     $this->assertSame(10, f\get_or($coll, 'no', 10));
     $this->assertNull(f\get_or($coll, 'ups', null));
 }
 /**
  * @return Scope|null
  */
 public function find($name)
 {
     return f\get_or($this->scopes, $name, null);
 }