コード例 #1
0
ファイル: BagTest.php プロジェクト: adamnicholson/bag
 public function testSet()
 {
     $bag = new Bag(['foo' => 'bar', 4 => '15']);
     $bag->set('hello', 'world');
     $this->assertEquals('bar', $bag->get('foo'));
     $this->assertEquals('world', $bag->get('hello'));
 }
コード例 #2
0
ファイル: HeaderBag.php プロジェクト: JanHuang/http
 /**
  * @param $name
  * @param $value
  * @return $this
  */
 public function set($name, $value)
 {
     $name = str_replace('_', '-', strtolower($name));
     $info = explode('-', $name);
     array_map(function ($value) {
         return ucfirst($value);
     }, $info);
     $value = explode(',', $value);
     $this->headers[implode('-', $info)] = $value;
     parent::set($name, $value);
     return $this;
 }
コード例 #3
0
ファイル: NestedBag.php プロジェクト: bolt/collections
 /**
  * Set a value in a nested collection key.
  * Sub-collections will be created as needed to set the value.
  *
  * A value may be appended by using "[]".
  *
  * Nested set example:
  *
  *    $c = new NestedCollection();
  *    $c->set('foo/bar', 'world');
  *    $c->get('foo'); // Returns ['bar' => 'world']
  *
  * Nested append example:
  *
  *    $c->set('foo/bar/[]', 'world');
  *    $c->get('foo'); // Returns ['bar' => ['world']]
  *
  * @param int|string $path Path to key
  * @param mixed      $value Value to set at the key
  *
  * @return $this|NestedBag
  *
  * @throws \RuntimeException When trying to set a path that travels through
  *                           a scalar value or an array under an object.
  */
 public function set($path, $value)
 {
     // This provides consistent functionality.
     // Instead of saying [] can only be used in a sub-path
     if ($path === '[]') {
         return $this->add($value);
     }
     $parts = explode('/', $path);
     // If not nested path, use normal set
     if (count($parts) <= 1) {
         return parent::set($path, $value);
     }
     $previousKey = null;
     $current = $this;
     while (null !== ($key = array_shift($parts))) {
         // Check if current is not array accessible
         if (!Utils::isArrayAccessible($current)) {
             throw new \RuntimeException("Trying to set path {$path}, " . "but {$previousKey} is set and is not array accessible");
         }
         $previousKey = $key;
         // If last part in path, set value at key
         if (empty($parts)) {
             if ($key === '[]') {
                 $current[] = $value;
             } else {
                 $current[$key] = $value;
             }
             // Sub-collection exists, so get it and continue looping
         } elseif (isset($current[$key])) {
             $current =& $this->getSubCollection($current, $key);
             // Create sub-collection and continue looping
         } else {
             $new = new static();
             if ($key === '[]') {
                 $current[] = $new;
             } else {
                 $current[$key] = $new;
             }
             unset($current);
             $current = $new;
             unset($new);
         }
     }
     return $this;
 }