Skip to content

Commit

Permalink
update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
arutyunyan committed Apr 16, 2024
1 parent 0a9eccc commit 4297a29
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 13 deletions.
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,24 @@ echo $timerOnSum(1, 2); // Total execution: 1.00034234 ms; Result: 3
# Generics
```php
use Sagittaracc\PhpPythonDecorator\Decorator;
use Sagittaracc\PhpPythonDecorator\T;
use Sagittaracc\PhpPythonDecorator\modules\generics\aliases\T;

#[T]
class Box
class PaymentInfo
{
use Decorator;

public string $id;

public int $amount;

#[T]
public array $items;
public $currency;
}

$box = new Box();
$box(Pencil::class); // new Box<Pencil>();

$pencil = new Pencil();
$pen = new Pen();
$box->_items = [$pencil, $pen]; // throws a GenericError
$paymentInfo = new PaymentInfo();
$paymentInfo(Number::class); // new PaymentInfo<Number>();
set_decorator_prop($paymentInfo, 'currency', 'rubles'); // throws a GenericError
```

# Validation
Expand Down
10 changes: 10 additions & 0 deletions src/modules/generics/aliases/T.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Sagittaracc\PhpPythonDecorator\modules\generics\aliases;

use Attribute;
use Sagittaracc\PhpPythonDecorator\modules\generics\BaseGeneric;

#[Attribute]
class T extends BaseGeneric
{}
2 changes: 1 addition & 1 deletion src/modules/generics/primitives/Number.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ class Number
{
public function validate($value)
{
return is_numeric($value);
return is_int($value);
}
}
16 changes: 13 additions & 3 deletions tests/GenericsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Sagittaracc\PhpPythonDecorator\modules\generics\primitives\Str;
use Sagittaracc\PhpPythonDecorator\tests\examples\MyAnotherBox;
use Sagittaracc\PhpPythonDecorator\tests\examples\MyBox;
use Sagittaracc\PhpPythonDecorator\tests\examples\PaymentInfo;
use Sagittaracc\PhpPythonDecorator\tests\examples\Pen;
use Sagittaracc\PhpPythonDecorator\tests\generics\T;
use Sagittaracc\PhpPythonDecorator\tests\generics\U;
Expand Down Expand Up @@ -51,7 +52,16 @@ public function testGenericFail(): void
$this->expectException(GenericError::class);

$box = new MyAnotherBox();
$box(Pen::class, Str::class);
set_decorator_prop($box, 'id', 3);
$box(Pen::class, Number::class);
set_decorator_prop($box, 'id', '3');
}

public function testPaymentInfo(): void
{
$this->expectException(GenericError::class);

$paymentInfo = new PaymentInfo();
$paymentInfo(Number::class);
set_decorator_prop($paymentInfo, 'currency', 'rubles');
}
}
}
19 changes: 19 additions & 0 deletions tests/examples/PaymentInfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Sagittaracc\PhpPythonDecorator\tests\examples;

use Sagittaracc\PhpPythonDecorator\Decorator;
use Sagittaracc\PhpPythonDecorator\modules\generics\aliases\T;

#[T]
class PaymentInfo
{
use Decorator;

public string $id;

public int $amount;

#[T]
public $currency;
}

0 comments on commit 4297a29

Please sign in to comment.