You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The Singleton pattern ensures a class has only one instance and provides a global point of access to it. Our implementation uses a trait to make it reusable.
@@ -557,6 +558,164 @@ The State pattern is perfect for managing complex workflows like order processin
557
558
- Payment Processing
558
559
- Task Management Systems
559
560
561
+
### 10. Pipeline Pattern
562
+
563
+
The Pipeline pattern allows you to process data through a series of operations, where each operation takes input from the previous operation and produces output for the next one. This pattern is particularly useful for data transformation, validation, and processing workflows.
564
+
565
+
#### Features
566
+
- Fluent interface for operation chaining
567
+
- Built-in error handling
568
+
- Input validation
569
+
- Type-safe operations with PHP 8.2+ generics
570
+
- Side effect management
571
+
- Conditional processing
572
+
- Operation composition
573
+
574
+
#### Basic Usage
575
+
576
+
```php
577
+
use DesiredPatterns\Pipeline\Pipeline;
578
+
579
+
// Basic pipeline
580
+
$result = Pipeline::of(5)
581
+
->pipe(fn($x) => $x * 2) // 10
582
+
->pipe(fn($x) => $x + 1) // 11
583
+
->get(); // Returns: 11
584
+
585
+
// Pipeline with error handling
586
+
$result = Pipeline::of($value)
587
+
->try(
588
+
fn($x) => processData($x),
589
+
fn(\Throwable $e) => handleError($e)
590
+
)
591
+
->get();
592
+
593
+
// Pipeline with validation
594
+
$result = Pipeline::of($data)
595
+
->when(
596
+
fn($x) => $x > 0,
597
+
fn($x) => sqrt($x)
598
+
)
599
+
->get();
600
+
```
601
+
602
+
#### Advanced Usage with PipelineBuilder
603
+
604
+
The PipelineBuilder provides a more structured way to create complex pipelines with validation and error handling:
605
+
606
+
```php
607
+
use DesiredPatterns\Pipeline\PipelineBuilder;
608
+
609
+
$builder = new PipelineBuilder();
610
+
$result = $builder
611
+
->withValidation(fn($x) => $x > 0, 'Value must be positive')
612
+
->withValidation(fn($x) => $x < 100, 'Value must be less than 100')
0 commit comments