1: <?php
2:
3: /**
4: * Copyright 2015, Eduardo Trujillo
5: *
6: * For the full copyright and license information, please view the LICENSE
7: * file that was distributed with this source code.
8: *
9: * This file is part of the Nucleus package
10: */
11:
12: namespace Chromabits\Nucleus\Transformation;
13:
14: use Chromabits\Nucleus\Foundation\BaseObject;
15: use Chromabits\Nucleus\Support\Std;
16: use Chromabits\Nucleus\Transformation\Interfaces\TransformInterface;
17: use Closure;
18:
19: /**
20: * Class TransformPipeline.
21: *
22: * @author Eduardo Trujillo <ed@chromabits.com>
23: * @package Chromabits\Nucleus\Support
24: */
25: class TransformPipeline extends BaseObject implements TransformInterface
26: {
27: /**
28: * @var TransformInterface[]
29: */
30: protected $transforms;
31:
32: /**
33: * @return static
34: */
35: public static function define()
36: {
37: return new static();
38: }
39:
40: /**
41: * Add a transform to the pipeline.
42: *
43: * @param TransformInterface $transform
44: *
45: * @return $this
46: */
47: public function add(TransformInterface $transform)
48: {
49: $this->transforms[] = $transform;
50:
51: return $this;
52: }
53:
54: /**
55: * Add a transform to the pipeline using the provided closure.
56: *
57: * @param Closure $transform
58: *
59: * @return TransformPipeline
60: */
61: public function inline(Closure $transform)
62: {
63: return $this->add(new ClosureTransform($transform));
64: }
65:
66: /**
67: * Run the input through the pipeline.
68: *
69: * @param array $input
70: *
71: * @return array
72: */
73: public function run(array $input)
74: {
75: return Std::foldl(function ($current, TransformInterface $input) {
76: return $input->run($current);
77: }, $input, $this->transforms);
78: }
79: }
80: