Created
May 28, 2015 21:54
-
-
Save kastaneda/d191848a55ea891b7138 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
interface ClassGenerator | |
{ | |
/** | |
* @return string | |
*/ | |
public function getClassName(); | |
/** | |
* @return string | |
*/ | |
public function getClassCode(); | |
} | |
class BaseClassManifest implements ClassGenerator | |
{ | |
/** @var string */ | |
private $className; | |
/** @var array */ | |
private $fields; | |
/** | |
* @param string $className | |
* @param array $fields | |
*/ | |
public function __construct($className, array $fields) | |
{ | |
$this->className = $className; | |
$this->fields = $fields; | |
} | |
/** | |
* @return string | |
*/ | |
public function getClassName() | |
{ | |
if (strpos($this->className, ' ') === false) { | |
return $this->className; | |
} | |
list($name, $dummy) = explode(' ', $this->className, 2); | |
return $name; | |
} | |
/** | |
* @param string $type | |
* @return string | |
*/ | |
private function getTypeHint($type) | |
{ | |
if (strpos($type, '|') !== false || | |
in_array($type, [ | |
'mixed', 'bool', 'int', 'integer', 'string', | |
'float', 'double', 'resource', 'null' | |
])) { | |
return ''; | |
}; | |
if (substr($type, -2) === '[]') { | |
return 'array '; | |
} | |
return $type . ' '; | |
} | |
/** | |
* @return string | |
*/ | |
public function getClassCode() | |
{ | |
$code = ''; | |
foreach ($this->fields as $fieldName => $fieldType) { | |
$fieldUppercase = ucfirst($fieldName); | |
$fieldTypeHint = $this->getTypeHint($fieldType); | |
$code .= <<<PHP | |
/** @var $fieldType */ | |
protected \$$fieldName; | |
/** | |
* @return $fieldType | |
*/ | |
public function get$fieldUppercase() | |
{ | |
return \$this->$fieldName; | |
} | |
/** | |
* @param $fieldType $$fieldName | |
* @return \$this | |
*/ | |
public function set$fieldUppercase($fieldTypeHint$$fieldName) | |
{ | |
\$this->$fieldName = \$$fieldName; | |
return \$this; | |
} | |
PHP; | |
} | |
// TODO: namespaces | |
return "class {$this->className}\n{{$code}}"; | |
} | |
} | |
class Scaffolding | |
{ | |
/** @var ClassGenerator[] */ | |
private static $generators = []; | |
private function __construct() | |
{ | |
} | |
/** | |
* @param ClassGenerator $generator | |
*/ | |
public static function addClassGenerator(ClassGenerator $generator) | |
{ | |
self::$generators[$generator->getClassName()] = $generator; | |
} | |
/** | |
* @param string $className | |
* @return string|bool | |
*/ | |
public static function getClassCode($className) | |
{ | |
if (!isset(self::$generators[$className])) { | |
return false; | |
} | |
return self::$generators[$className]->getClassCode(); | |
} | |
} | |
function __autoload($className) { | |
eval(Scaffolding::getClassCode($className)); | |
} | |
Scaffolding::addClassGenerator(new BaseClassManifest( | |
'BaseCustomer', | |
[ | |
'firstName' => 'string', | |
'lastName' => 'string', | |
'dateOfBirth' => 'DateTimeInterface', | |
'passwordHash' => 'string', | |
] | |
)); | |
/* | |
CREATE TABLE `customers` ( | |
`id` INT AUTO_INCREMENT NOT NULL, | |
`first_name` VARCHAR(255), | |
`last_name` VARCHAR(255), | |
`date_of_birth` DATETIME, | |
`password_hash` VARCHAR(255), | |
PRIMARY KEY(`id`) | |
); | |
*/ | |
class Customer extends BaseCustomer | |
{ | |
public function debug() | |
{ | |
var_dump($this); | |
} | |
} | |
$test = (new Customer) | |
->setFirstName('Dmitry') | |
->setLastName('Kolesnikov') | |
->setDateOfBirth(new DateTime('1980-09-06 Europe/Kiev')) | |
->setPasswordHash(password_hash('test01', PASSWORD_DEFAULT)) | |
->debug(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment