1. Go to this page and download the library: Download happy-types/enumerable-type 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/ */
happy-types / enumerable-type example snippets
if (($ticket->getType() === TicketType::Priority()) &&
($purchase->getStatus() === PurchaseStatus::Completed()) {
// do your logic here...
}
public function someMethod(CompanyType $companyType) {
// ...
}
// usage:
someMethod(CompanyType::Private());
someMethod(CompanyType::fromId('private'));
class CompanyType extends EnumerableType {
final public static function Unknown() { return static::get(null); }
final public static function Private() { return static::get('private'); }
final public static function Public() { return static::get('public'); }
}
class PaymentMethod extends EnumerableType {
final public static function Unknown() { return static::get(null); }
final public static function Cash() { return static::get('cash'); }
final public static function CreditCard() { return static::get('credit_card'); }
}
class DeliveryStatus extends EnumerableType {
final public static function Delivered() { return static::get(1, 'delivered'); }
final public static function NotDelivered() { return static::get(0, 'not_delivered'); }
}
class CompanyEnum { }
class CompanyEnum_Private extends CompanyEnum {}
class CompanyEnum_Public extends CompanyEnum {}
class CompanyEnum_Unknown extends CompanyEnum {}
function fromId($id) {
switch ($id) {
case 'private': return new CompanyEnum_Private();
case 'public': return new CompanyEnum_Public();
case null: return new CompanyEnum_Unknown();
default: throw new \RuntimeException("unhandled {$id}");
}
}
function doSomething(CompanyEnum $companyEnum) {
if ($companyEnum instanceof CompanyEnum_Private) {
// ...
}
}
class Company {
/**
* @var int
*/
private $companyTypeId;
public function setCompanyType(CompanyType $value)
{
$this->companyTypeId = $value->id();
}
/**
* @return CompanyType
*/
public function getCompanyType()
{
return CompanyType::fromId($this->companyTypeId);
}
}
switch ($companyType) {
case CompanyType::Private():
// ... do logic..
break;
case CompanyType::Public():
// ... do logic..
break;
default:
throw new \RuntimeException("unhandled case:" . $companyType->name());
}
// BAD CODE:
if ($companyType->id() === CompanyType::Private()->id()) { /*...*/ }
// AWFUL CODE:
if ($companyType->id() === 'private') { /*...*/ }
// GOOD CODE:
if ($companyType === CompanyType::Private()) { /*...*/ }
// BAD CODE:
$companyTypes = [CompanyType::Private(), CompanyType::Public()];
// GOOD CODE:
$companyTypes = CompanyType::enum();
foreach ($companyTypes as $companyType) {
echo $companyType->name();
}
// BAD CODE:
switch ($companyTypeId) {
case 'private': return CompanyType::Private();
case 'public': return CompanyType::Public();
default: return null;
}
// GOOD CODE:
$companyType = CompanyType::fromId($companyTypeId);
// BAD CODE:
class PaymentMethod extends EnumerableType {
final public static function Cash() { return static::get('cash'); }
final public static function CreditCard() { return static::get('credit_card'); }
}
function getPaymentMethod() {
return $id ? PaymentMethod::fromId($id) : null;
}
// GOOD CODE:
class PaymentMethod extends EnumerableType {
final public static function Unknown() { return static::get(null); }
final public static function Cash() { return static::get('cash'); }
final public static function CreditCard() { return static::get('credit_card'); }
}
function getPaymentMethod() {
return PaymentMethod::fromId($id);
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.