1. Go to this page and download the library: Download mpscholten/typesafe-enum library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
mpscholten / typesafe-enum example snippets
class UserType extends \TypesafeEnum\Enum
{
public static function PAID()
{
return new UserType('paid');
}
public static function FREE()
{
return new UserType('free');
}
public function isPaid()
{
return $this->is('paid');
}
public function isFree()
{
return $this->is('free');
}
}
class User
{
public function __construct($email, UserType $type)
{
$this->type = $type;
}
public function getType()
{
return $this->type;
}
}
$user = new User('[email protected]', UserType::PAID()); // Good
$user->getType()->isPaid(); // true
$user->getType()->isFree(); // false
(string) $user->getType(); // "paid"
$user = new User('[email protected]', UserType::FREE()); // Good
$user->getType()->isFree(); // true
$user->getType()->isPaid(); // false
(string) $user->getType(); // "free"
$user = new User('[email protected]', 'some string'); // Type error (PHP Catchable fatal error: Argument 2 passed to User::__construct() must be an instance of UserType, integer given, called in ...)
$user = new User('[email protected]', null); // Type error (PHP Catchable fatal error: Argument 2 passed to User::__construct() must be an instance of UserType, null given, called in ...)
class Temperature extends Enum
{
private $celsius;
protected function __construct($value, $celsius)
{
parent::__construct($value);
$this->celsius = $celsius;
}
public static function HOT()
{
return new Temperature('hot', 40);
}
public static function COLD()
{
return new Temperature('cold', 10);
}
public function isHot()
{
return $this->is('hot');
}
public function isCold()
{
return $this->is('cold');
}
/**
* @return int
*/
public function getCelsius()
{
return $this->celsius;
}
}
$temperature = Temperature::HOT();
$temperature->isHot(); // true
$temperature->isCold(); // false
$temperature->getCelsius(); // 40
(string) $temperature; // "hot"
$temperature = Temperature::COLD();
$temperature->isCold(); // true
$temperature->isHot(); // false
$temperature->getCelsius(); // 10
(string) $temperature; // "cold"
/** @Embeddable */
class UserType extends \TypesafeEnum\Enum
{
/** @Column(type = "smallint", name = "type") */
protected $value; // Override the `$value` property of \TypesafeEnum\Enum and apply mapping
public static function PAID()
{
return new UserType(0);
}
public static function FREE()
{
return new UserType(1);
}
public function isPaid()
{
return $this->is(0);
}
public function isFree()
{
return $this->is(1);
}
}
class User
{
/** @Embedded(class = "UserType", columnPrefix = false) */
private $type;
public function __construct($email, UserType $type)
{
$this->type = $type;
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.