File tree Expand file tree Collapse file tree 5 files changed +81
-0
lines changed Expand file tree Collapse file tree 5 files changed +81
-0
lines changed Original file line number Diff line number Diff line change 1+ <?php 
2+ 
3+ declare (strict_types=1 );
4+ 
5+ namespace  Growthdev \DesignPatterns \Structural \Adapter ;
6+ 
7+ interface  CurrencyConverter
8+ {
9+  public  function  convert (float  $ amount ): float ;
10+ }
Original file line number Diff line number Diff line change 1+ <?php 
2+ 
3+ declare (strict_types=1 );
4+ 
5+ namespace  Growthdev \DesignPatterns \Structural \Adapter ;
6+ 
7+ final  class  CurrencyConverterAdapter
8+ {
9+  private  CurrencyConverter   $ currencyConverter ;
10+ 
11+  public  function  __construct (CurrencyConverter   $ currencyConverter )
12+  {
13+  $ this  ->currencyConverter  = $ currencyConverter ;
14+  }
15+ 
16+  public  function  convert (float  $ amount ): float 
17+  {
18+  return  $ this  ->currencyConverter ->convert ($ amount );
19+  }
20+ }
Original file line number Diff line number Diff line change 1+ <?php 
2+ 
3+ declare (strict_types=1 );
4+ 
5+ namespace  Growthdev \DesignPatterns \Structural \Adapter ;
6+ 
7+ final  class  CurrencyToDollarConverter implements  CurrencyConverter
8+ {
9+  public  function  convert (float  $ amount ): float 
10+  {
11+  return  $ amount  * 5.61 ; // 1$ = R$ 5.61 
12+  }
13+ }
Original file line number Diff line number Diff line change 1+ <?php 
2+ 
3+ declare (strict_types=1 );
4+ 
5+ namespace  Growthdev \DesignPatterns \Structural \Adapter ;
6+ 
7+ final  class  CurrencyToEuroConverter implements  CurrencyConverter
8+ {
9+  public  function  convert (float  $ amount ): float 
10+  {
11+  return  $ amount  * 6.35 ; // 1 EUR = R$ 6.35 
12+  }
13+ }
Original file line number Diff line number Diff line change 1+ <?php 
2+ 
3+ declare (strict_types=1 );
4+ 
5+ namespace  Growthdev \DesignPatterns \Tests \Structural \Adapter ;
6+ 
7+ use  Growthdev \DesignPatterns \Structural \Adapter \CurrencyConverterAdapter ;
8+ use  Growthdev \DesignPatterns \Structural \Adapter \CurrencyToDollarConverter ;
9+ use  Growthdev \DesignPatterns \Structural \Adapter \CurrencyToEuroConverter ;
10+ use  PHPUnit \Framework \TestCase ;
11+ 
12+ final  class  CurrencyAdapterTest extends  TestCase
13+ {
14+  public  function  testCurrencyConverterToEuro (): void 
15+  {
16+  $ currencyConverter  = new  CurrencyConverterAdapter (new  CurrencyToEuroConverter );
17+  $ this  ->assertEquals (6.35 , $ currencyConverter ->convert (1.0 ));
18+  }
19+ 
20+  public  function  testCurrencyConverterToDollar (): void 
21+  {
22+  $ currencyConverter  = new  CurrencyConverterAdapter (new  CurrencyToDollarConverter );
23+  $ this  ->assertEquals (5.61 , $ currencyConverter ->convert (1.0 ));
24+  }
25+ }
                                 You can’t perform that action at this time. 
               
                  
0 commit comments