@ 2015-04-08T12:36:19Z <?php
class Parser {
private $parse;
public function __construct() {
$this->parse = new ArrayObject();
}
public function parse($str){
preg_match_all("/@(.*) \((.*)\)/\n", $str, $matches);
$this->fill($matches[1],$matches[2]);
return $this;
}
protected function fill(array $classes, array $args){
$count = count($classes);
for($i=0;$i<$count;$i++){
$this->parse->offsetSet($classes[$i], new $classes[$i]($args[$i]));
}
return $this;
}
function getParse() {
return $this->parse;
}
public function getData($name) {
return $this->parse->offsetExists($name)?$this->parse->offsetGet($name):NULL;
}
}
class Column {
private $name;
private $type;
private $length;
function __construct($args) {
$parse = json_decode($args);
$this->setLength($parse->length);
$this->setName($parse->name);
$this->setType($parse->type);
}
function getName() {
return $this->name;
}
function getType() {
return $this->type;
}
function getLength() {
return $this->length;
}
function setName($name) {
$this->name = $name;
}
function setType($type) {
$this->type = $type;
}
function setLength($length) {
$this->length = $length;
}
}
class Form {
private $field;
function __construct($args) {
$parser = json_decode($args);
$this->setField($parser->field);
}
function getField() {
return $this->field;
}
function setField($field) {
$this->field = $field;
}
}
class Teste
{
/**
* @Column ({"name":"name","type":"varchar","length":123})
* @Form ({"field":"nome"})
*/
private $nome;
/**
* @Column ({"name":"idade","type":"int","length":123})
* @Form ({"field":"idade"})
*/
private $idade;
}
$p = new Parser();
$r = new ReflectionClass(new Teste());
$props = $r->getProperties();
$array = array();
foreach($props as $prop){
var_dump($p->parse($prop->getDocComment())->getParse());
$array[$prop->getName()] = $p->parse($prop->getDocComment())->getParse();
}
echo '<pre>';
print_r($array['idade']['Column']->getType());
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 5.3.0 - 5.3.29 , 5.4.0 - 5.4.45 , 5.5.24 - 5.5.35 , 5.6.7 - 5.6.28 , 7.0.0 - 7.0.20 , 7.1.0 - 7.1.25 , 7.2.0 - 7.2.33 , 7.3.0 - 7.3.33 , 7.4.0 - 7.4.33 , 8.0.0 - 8.0.30 , 8.1.0 - 8.1.28 , 8.2.0 - 8.2.18 , 8.3.0 - 8.3.7 object(ArrayObject)#2 (1) {
["storage":"ArrayObject":private]=>
array(2) {
["Column"]=>
object(Column)#6 (3) {
["name":"Column":private]=>
string(4) "name"
["type":"Column":private]=>
string(7) "varchar"
["length":"Column":private]=>
int(123)
}
["Form"]=>
object(Form)#7 (1) {
["field":"Form":private]=>
string(4) "nome"
}
}
}
object(ArrayObject)#2 (1) {
["storage":"ArrayObject":private]=>
array(2) {
["Column"]=>
object(Column)#7 (3) {
["name":"Column":private]=>
string(5) "idade"
["type":"Column":private]=>
string(3) "int"
["length":"Column":private]=>
int(123)
}
["Form"]=>
object(Form)#8 (1) {
["field":"Form":private]=>
string(5) "idade"
}
}
}
<pre>int Output for 5.2.0 - 5.2.17 object(ArrayObject)#2 (2) {
["Column"]=>
object(Column)#6 (3) {
["name:private"]=>
string(4) "name"
["type:private"]=>
string(7) "varchar"
["length:private"]=>
int(123)
}
["Form"]=>
object(Form)#7 (1) {
["field:private"]=>
string(4) "nome"
}
}
object(ArrayObject)#2 (2) {
["Column"]=>
object(Column)#7 (3) {
["name:private"]=>
string(5) "idade"
["type:private"]=>
string(3) "int"
["length:private"]=>
int(123)
}
["Form"]=>
object(Form)#8 (1) {
["field:private"]=>
string(5) "idade"
}
}
<pre>int Output for 5.1.0 - 5.1.6 Fatal error: Call to undefined function json_decode() in /in/aYGUE on line 40
Process exited with code 255 . Output for 5.0.0 - 5.0.5 Parse error: parse error, unexpected T_ARRAY, expecting '&' or T_VARIABLE in /in/aYGUE on line 17
Process exited with code 255 . Output for 4.4.2 - 4.4.9 Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/aYGUE on line 5
Process exited with code 255 . Output for 4.3.0 - 4.3.1 , 4.3.5 - 4.3.11 , 4.4.0 - 4.4.1 Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/aYGUE on line 5
Process exited with code 255 . Output for 4.3.2 - 4.3.4 Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/aYGUE on line 5
Process exited with code 255 . preferences:dark mode live preview
259.07 ms | 401 KiB | 329 Q