Skip to content

Commit fb386fe

Browse files
author
nix
committed
added Pipe::set()
1 parent 42c60dc commit fb386fe

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ Example:
176176
<?php
177177
echo (new Pipe('NOW')) // wrap initial value
178178
(strtolower(...)) // map through strtolower() => 'now'
179-
->new(DateTimeImmutable::class, Pipe::PLACEHOLDER, DateTimeZone::UTC) // create class
179+
->new(DateTimeImmutable::class, Pipe::PLACEHOLDER, new DateTimeZone('UTC')) // create class
180180
->format("Y_m_d") // call 'format' method magically => '2025_01_01' (that was 'now' not long ago...)
181181
(str_replace(...), '_', '-') // => '2025-01-01'
182182
(explode(...), '-', Pipe::PLACEHOLDER, 2) // => ['2025', '01-01']

src/Pipe.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* ```
99
* echo (new Pipe('NOW')) // wrap initial value
1010
* (strtolower(...)) // map through strtolower() => 'now'
11-
* ->new(DateTimeImmutable::class, Pipe::PLACEHOLDER, DateTimeZone::UTC) // create class
11+
* ->new(DateTimeImmutable::class, Pipe::PLACEHOLDER, new DateTimeZone('UTC')) // create class
1212
* ->format("Y_m_d") // calls 'format' method => '2025_01_01' (that was 'now' not long ago...)
1313
* (str_replace(...), '_', '-') // => '2025-01-01'
1414
* (explode(...), '-', Pipe::PLACEHOLDER, 2) // => ['2025', '01-01']
@@ -63,7 +63,7 @@ public function new(string $class, mixed ... $args): self
6363

6464
/**
6565
* Returns the class field or array element with key `$key`.
66-
* @param string|int $key the key. when it is an integer, array access is used automatically.
66+
* @param string|int $key the key. When it is an integer, array access is used automatically.
6767
* @param bool $array_access whether to use array access
6868
* @since 1.0
6969
*/
@@ -73,6 +73,22 @@ public function get(string|int $key, bool $array_access = false): self
7373
return $this;
7474
}
7575

76+
/**
77+
* Sets the class field or array element with key `$key` to `$value`.
78+
* @param string|int $key the key. When it is an integer, array access is used automatically.
79+
* @param mixed $value the value to be set for the `$key`.
80+
* @param bool $array_access whether to use array access
81+
* @since 1.2
82+
*/
83+
public function set(string|int $key, mixed $value, bool $array_access = false): self
84+
{
85+
if ($array_access || is_int($key))
86+
$this->value[$key] = $value;
87+
else
88+
$this->value->{$key} = $value;
89+
return $this;
90+
}
91+
7692
public function __toString(): string
7793
{
7894
return "$this->value";

0 commit comments

Comments
 (0)