@@ -42,6 +42,11 @@ class Pipeline
4242 */
4343 private $ caster ;
4444
45+ /**
46+ * @var bool
47+ */
48+ private $ castFirst ;
49+
4550 /**
4651 * @var bool
4752 */
@@ -58,18 +63,20 @@ class Pipeline
5863 private $ DTOs ;
5964
6065 /**
61- * @param mixed $context Something that can be accessed with $dto->context()
62- * acting as context for all the callbacks
63- * @param int|callable|null $caster Values returned by any of pipeline callbacks can be
64- * casted using a class constants or a custom callback
66+ * @param mixed $context Something that can be accessed with $dto->context()
67+ * acting as context for all the callbacks
68+ * @param int|callable|null $caster Values returned by any of pipeline callbacks can be
69+ * casted using a class constants or a custom callback
70+ * @param bool $castFirst Should initial value should be casted?
6571 */
66- public function __construct ($ context = null , $ caster = null )
72+ public function __construct ($ context = null , $ caster = null , $ castFirst = false )
6773 {
6874 $ this ->pipeline = new SplObjectStorage ();
6975 if (is_int ($ caster ) && array_key_exists ($ caster , self ::$ casters_map )) {
7076 $ caster = [$ this , self ::$ casters_map [$ caster ]];
7177 }
7278 $ this ->caster = is_callable ($ caster ) ? $ caster : null ;
79+ $ this ->castFirst = $ this ->toBool ($ castFirst );
7380 $ this ->context = $ context ;
7481 $ this ->DTOs = new SplStack ();
7582 $ this ->locked = false ;
@@ -121,7 +128,7 @@ public function applyTo($initial, DTO $dto = null, $cursor = null)
121128 return $ initial ;
122129 }
123130 $ this ->DTOs ->push ($ this ->init ($ dto , $ initial ));
124- $ carry = is_null ( $ cursor ) ? $ initial : $ this ->maybeCast ( $ cursor );
131+ $ carry = $ this ->initialValue ( $ initial , $ cursor );
125132 while ($ this ->pipeline ->valid ()) {
126133 $ carry = $ this ->run ($ initial , $ carry );
127134 $ this ->pipeline ->next ();
@@ -198,6 +205,22 @@ private function init(DTO $dto = null, $initial = null)
198205 return $ dto ;
199206 }
200207
208+ /**
209+ * Setup initial value for Pipeline based on caster settings.
210+ *
211+ * @param mixed $initial
212+ * @param mixed $cursor
213+ * @return mixed
214+ */
215+ private function initialValue ($ initial , $ cursor )
216+ {
217+ if (! is_null ($ cursor )) {
218+ return $ this ->maybeCast ($ cursor );
219+ }
220+
221+ return $ this ->castFirst ? $ this ->maybeCast ($ initial ) : $ initial ;
222+ }
223+
201224 /**
202225 * Run current callback in the pipeline.
203226 *
@@ -223,6 +246,12 @@ private function run($initial, $carry)
223246 *
224247 * @param mixed $data
225248 * @return mixed
249+ * @uses \Toobo\PipePie\Pipeline::toArray()
250+ * @uses \Toobo\PipePie\Pipeline::toObject()
251+ * @uses \Toobo\PipePie\Pipeline::toString()
252+ * @uses \Toobo\PipePie\Pipeline::toInt()
253+ * @uses \Toobo\PipePie\Pipeline::toBool()
254+ * @uses \Toobo\PipePie\Pipeline::toFloat()
226255 */
227256 private function maybeCast ($ data )
228257 {
0 commit comments