Skip to content

Instantly share code, notes, and snippets.

@murilozilli
Last active February 14, 2020 03:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save murilozilli/c8738b2b33691764677b4af53e8959c5 to your computer and use it in GitHub Desktop.
Save murilozilli/c8738b2b33691764677b4af53e8959c5 to your computer and use it in GitHub Desktop.
Constant validation by value
<?php
namespace Application\Model;
class Model
{
/**
* @var string
*/
const TYPE_PDF = 'pdf';
/**
* @var string
*/
const TYPE_EMAIL_MARKETING = 'mkt';
/**
* @var string
*/
const TYPE_BUSINESS_CARD = 'business_card';
/**
* @var string
*/
const TYPE_TIMBER_PAPER = 'timbred_paper';
/**
* @var string
*/
const TYPE_LANDING_PAGE = 'landing_page';
/**
* Inform if given type is a valid constant
* @param string $type
* @return bool
*/
public static function isValidType($type) {
$reflectedClass = new \ReflectionClass(self::class);
$constants = $reflectedClass->getConstants();
foreach ($constants as $constName => $constValue) {
if (substr($constName, 0, 4) === 'TYPE' && $constValue === $type) {
return true;
}
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment