/** * Get the validaton rules within the validation context. * * @return array */ protected function getRulesInContext() { if (!$this->hasContext()) { return static::$rules; } $rulesInContext = Arr::get(static::$rules, self::DEFAULT_KEY, array()); if (!Arr::get(static::$rules, $this->context)) { throw new \Exception(sprintf("'%s' does not contain the validation context '%s'", get_called_class(), $this->context)); } $rulesInContext = array_merge($rulesInContext, static::$rules[$this->context]); return $rulesInContext; }
/** * Transforms the input array into the output array based on the rules * stated in the configuration file. * * @return array */ public function transform($root = '') { $output = []; $array = Arr::dot($this->feed_data); $rules = Arr::dot($this->rules); $all_rules = []; foreach ($rules as $key => $value) { if (strpos($key, '*')) { $new_keys = Arr::expandKeys($key, $this->expand_size); $new_values = Arr::expandKeys($value, $this->expand_size); $new_rules = array_combine($new_keys, $new_values); foreach ($new_rules as $new_key => $new_rule) { if (array_key_exists($new_rule, $array)) { $all_rules[$new_key] = $new_rule; } } } else { $all_rules[$key] = $value; } } foreach ($all_rules as $k => $v) { Arr::set($output, $k, Arr::get($array, $v)); } return $output; }
public function testGet() { $array = ['products' => ['desk' => ['price' => 100]]]; $value = Arr::get($array, 'products.desk'); $this->assertEquals(['price' => 100], $value); // Test null array values $array = ['foo' => null, 'bar' => ['baz' => null]]; $this->assertNull(Arr::get($array, 'foo', 'default')); $this->assertNull(Arr::get($array, 'bar.baz', 'default')); // Test direct ArrayAccess object $array = ['products' => ['desk' => ['price' => 100]]]; $arrayAccessObject = new ArrayObject($array); $value = Arr::get($arrayAccessObject, 'products.desk'); $this->assertEquals(['price' => 100], $value); // Test array containing ArrayAccess object $arrayAccessChild = new ArrayObject(['products' => ['desk' => ['price' => 100]]]); $array = ['child' => $arrayAccessChild]; $value = Arr::get($array, 'child.products.desk'); $this->assertEquals(['price' => 100], $value); // Test array containing multiple nested ArrayAccess objects $arrayAccessChild = new ArrayObject(['products' => ['desk' => ['price' => 100]]]); $arrayAccessParent = new ArrayObject(['child' => $arrayAccessChild]); $array = ['parent' => $arrayAccessParent]; $value = Arr::get($array, 'parent.child.products.desk'); $this->assertEquals(['price' => 100], $value); // Test missing ArrayAccess object field $arrayAccessChild = new ArrayObject(['products' => ['desk' => ['price' => 100]]]); $arrayAccessParent = new ArrayObject(['child' => $arrayAccessChild]); $array = ['parent' => $arrayAccessParent]; $value = Arr::get($array, 'parent.child.desk'); $this->assertNull($value); // Test missing ArrayAccess object field $arrayAccessObject = new ArrayObject(['products' => ['desk' => null]]); $array = ['parent' => $arrayAccessObject]; $value = Arr::get($array, 'parent.products.desk.price'); $this->assertNull($value); // Test null ArrayAccess object fields $array = new ArrayObject(['foo' => null, 'bar' => new ArrayObject(['baz' => null])]); $this->assertNull(Arr::get($array, 'foo', 'default')); $this->assertNull(Arr::get($array, 'bar.baz', 'default')); // Test null key returns the whole array $array = ['foo', 'bar']; $this->assertEquals($array, Arr::get($array, null)); // Test $array not an array $this->assertSame('default', Arr::get(null, 'foo', 'default')); $this->assertSame('default', Arr::get(false, 'foo', 'default')); // Test $array not an array and key is null $this->assertSame('default', Arr::get(null, null, 'default')); // Test $array is empty and key is null $this->assertSame([], Arr::get([], null)); $this->assertSame([], Arr::get([], null, 'default')); }
/** * Get the specified configuration value. * * @param string $key * @param mixed $default * @return mixed */ public function get($key, $default = null) { return Arr::get($this->items, $key, $default); }