ostatnio dawno nie kodowałem w PHP właściwie większość czasu poświęcam JavaScript, dlatego postanowiłem napisać klasę(ObjectConverter) w celu poznania bliżej refleksji i odświeżenia umiejętności. Klasa ma na celu pobieranie wyników metod klasy podanej konwersji, metody są pobierane na podstawie członu nazwy np: wszystkie publiczne metody zaczynające się na "get" zostaną użyte do konwersji, po czym wynik zostanie zapisany do tablicy i zwrócony. Metody podczas konwersji mogą otrzymywać zewnętrznie parametry.
<?php class ObjectConverter { protected $object; protected $params; protected $notConvert; protected $member = 'get'; 'object' => '%s is not an object', 'array' => '%s must be an array', 'not-param' => '%s expected parameter', 'not-exists' => 'public methods %s not exists', 'bad-quantity-param' => '%s bad quantity parameter in $params', 'too-many-param' => 'your method have too many parameter - allowed 5' ); $this->exception('object', '$object'); } $this->exception('array', '$params'); } $this->exception('array', '$not_convert'); } $this->setObject($object); $this->setParams($params); $this->setNotConvert($not_convert); return $this->prepareDataObject(); } protected function setObject($obj) { $this->object = $obj; } protected function setParams($params) { $this->params = $params; } protected function setNotConvert($not_convert) { } protected function prepareDataObject() { $methods = $this->reflectionClass()->getMethods(); if ($methods[$i]->isPublic() && $this->isMember($methods[$i]->name) && $this->isConvertMethod($methods[$i]->name)) { $this->methods[] = $methods[$i]; $this->methodsName[] = $methods[$i]->name; $this->quantityParams[] = $methods[$i]->getNumberOfRequiredParameters(); } } } public function toArray() { if (0 < $this->quantityParams[$i]) { $result[$this->createIndex($i)] = $this->withParams($i); } else { $result[$this->createIndex($i)] = $this->withoutParams($i); } } return $result; } protected function withParams($i) { $method_name = $this->methodsName[$i]; $this->exception('not-param', $method_name); } $params = $this->params[$method_name]; switch ($this->quantityParams[$i]) { case 1 : return $this->object->$method_name($params[0]); break; case 2 : return $this->object->$method_name($params[0], $params[1]); break; case 3 : return $this->object->$method_name($params[0], $params[1], $params[2]); break; case 4 : return $this->object->$method_name($params[0], $params[1], $params[2], $params[3]); break; case 5 : return $this->object->$method_name($params[0], $params[1], $params[2], $params[3], $params[4]); break; default : $this->exception('too-many-param', $method_name); } } else { $this->exception('bad-quantity-param'); } } protected function reflectionClass() { return new \ReflectionClass($this->object); } protected function withoutParams($i) { $method_name = $this->methodsName[$i]; return $this->object->$method_name(); } protected function createIndex($i) { return strtolower(preg_replace('![a-z]\K(?=[A-Z])!', '_', substr($this->methodsName[$i], strlen($this->member)))); } protected function isConvertMethod($method) { } protected function isMember($method) { } public function setMemberNameMethods($member) { $this->member = $member; return $this; } protected function exception($index, $var_name = null) { } } ?>
Poniżej zamieszczam dwa przykłady działania nie tłumacze dokładnie całego procesu zachodzącego w klasie, ponieważ zależy mi na waszej ocenie czytelności/przejrzystości kodu. Kod jest kompletny można go odpalić.
class Example { protected $enlargement = 'txt'; protected $file = 'log'; public function getEnlargement() { return $this->enlargement[0] !== '.' ? '.' . $this->enlargement : $this->enlargement; } public function getFileName() { return $this->file; } public function getLengthFileName() { } } $c = new ObjectConverter(); $example = new Example(); try { $result = $c->convert($example) ->toArray(); } catch (Exception $e) { } /* Array ( [enlargement] => .txt [file_name] => log [length_file_name] => 3 ) */ class Example2 { public function calculateAvg($a, $b) { return @($a / $b); } public function calculateSum($a, $b) { return @($a * $b); } public function calculateDiff($a, $b) { return $a - $b; } public function calculatePi($a) { return 3.14 * $a; } } $c = new ObjectConverter(); $example2 = new Example2(); ); try { $result = $c->setMemberNameMethods('calculate') ->convert($example2, $params, $notConvert) ->toArray(); } catch (Exception $e) { } /* Array ( [avg] => 0.2 [sum] => 275 [diff] => 15 ) */