Skip to content

Commit 3191f9f

Browse files
committed
Renames examples classes.
Replace var_dump() with assert()
1 parent 76444e5 commit 3191f9f

15 files changed

+141
-132
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,16 @@ any static property that isn't an Enum element then you should override the `\Db
133133
$viewAction = Action::$view;
134134

135135
// it is possible to compare Enum elements
136-
var_dump($viewAction === Action::$view);
136+
assert($viewAction === Action::$view);
137137

138138
// you can get Enum element by name
139139
$editAction = Action::valueOf('edit');
140+
assert($editAction === Action::$edit);
140141

141142
// iterate over all Enum elements
142143
foreach (Action::values() as $name => $action) {
143-
echo $action;
144+
assert($action instanceof Action);
145+
assert($name === (string) $action);
144146
}
145147
```
146148

examples/Fixtures/Action.php renamed to examples/Enum/Action.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
declare(strict_types=1);
33

4-
namespace Dbalabka\Examples\Fixtures;
4+
namespace Dbalabka\Examples\Enum;
55

66
use Dbalabka\Enumeration;
77

examples/Enum/CardType.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Dbalabka\Examples\Enum;
5+
6+
use Dbalabka\Enumeration;
7+
8+
final class CardType extends Enumeration
9+
{
10+
public static $amex;
11+
public static $visa;
12+
public static $masterCard;
13+
14+
protected static function initializeValues() : void
15+
{
16+
self::$amex = new self();
17+
self::$visa = new self();
18+
self::$masterCard = new self();
19+
}
20+
}
21+
CardType::initialize();

examples/Fixtures/Color.php renamed to examples/Enum/Color.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
declare(strict_types=1);
33

4-
namespace Dbalabka\Examples\Fixtures;
4+
namespace Dbalabka\Examples\Enum;
55

66
use Dbalabka\Enumeration;
77

examples/Fixtures/Day.php renamed to examples/Enum/Day.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
declare(strict_types=1);
33

4-
namespace Dbalabka\Examples\Fixtures;
4+
namespace Dbalabka\Examples\Enum;
55

66
use Dbalabka\Enumeration;
77

examples/Enum/Flag.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Dbalabka\Examples\Enum;
5+
6+
use Dbalabka\Enumeration;
7+
8+
final class Flag extends Enumeration
9+
{
10+
public static $noState;
11+
public static $ok;
12+
public static $notOk;
13+
public static $unavailable;
14+
15+
private $flagValue;
16+
17+
protected function __construct()
18+
{
19+
$this->flagValue = 1 << $this->ordinal();
20+
}
21+
22+
public function getFlagValue() : int
23+
{
24+
return $this->flagValue;
25+
}
26+
}
27+
Flag::initialize();

examples/Fixtures/Planet.php renamed to examples/Enum/Planet.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
declare(strict_types=1);
33

4-
namespace Dbalabka\Examples\Fixtures;
4+
namespace Dbalabka\Examples\Enum;
55

66
use Dbalabka\Enumeration;
77

examples/cardType.php

Lines changed: 0 additions & 53 deletions
This file was deleted.

examples/card_type.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
use Dbalabka\Examples\Enum\CardType;
5+
6+
require_once(__DIR__ . '/../vendor/autoload.php');
7+
8+
$values = CardType::values();
9+
assert(is_array($values));
10+
assert($values['amex'] === CardType::$amex);
11+
assert($values['visa'] === CardType::$visa);
12+
assert($values['masterCard'] === CardType::$masterCard);
13+
14+
15+
class Card
16+
{
17+
public $type;
18+
19+
public function __construct(CardType $type)
20+
{
21+
$this->type = $type;
22+
}
23+
}
24+
25+
$card = new Card(CardType::$amex);
26+
assert($card->type !== CardType::$visa, 'We can use strict comparision, because each Enum\'s element is singleton');
27+
assert($card->type === CardType::$amex);
28+
29+
foreach (CardType::values() as $name => $type) {
30+
assert($type instanceof CardType, 'Array values are Enums instances');
31+
assert($name === (string) $type, 'Array keys are Enums elements names');
32+
}
33+
34+
$a = CardType::$visa;
35+
switch ($a) {
36+
case CardType::$amex:
37+
assert(false, 'Incorrect, it is not amex');
38+
break;
39+
case CardType::$visa:
40+
assert(true, 'Correct, it is visa!');
41+
break;
42+
}
43+
44+

examples/class_static_construct.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
<?php
22
declare(strict_types=1);
33

4-
use Dbalabka\Enumeration;
5-
use Dbalabka\Examples\Fixtures\Color;
4+
use Dbalabka\Examples\Enum\Color;
65

76
$composer = require_once(__DIR__ . '/../vendor/autoload.php');
87
$loader = new ConstructStatic\Loader($composer);
98

10-
var_dump(Color::$red instanceof Color && Color::$red === Color::$red);
9+
assert(Color::$red instanceof Color && Color::$red === Color::$red);

0 commit comments

Comments
 (0)