Calculator
5 years ago
Guesser
5 years ago
ORM
5 years ago
Provider
5 years ago
DefaultGenerator.php
5 years ago
Documentor.php
5 years ago
Factory.php
5 years ago
Generator.php
5 years ago
UniqueGenerator.php
5 years ago
ValidGenerator.php
5 years ago
Documentor.php
67 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Faker; |
| 4 | |
| 5 | class Documentor |
| 6 | { |
| 7 | protected $generator; |
| 8 | |
| 9 | /** |
| 10 | * @param Generator $generator |
| 11 | */ |
| 12 | public function __construct(Generator $generator) |
| 13 | { |
| 14 | $this->generator = $generator; |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * @return array |
| 19 | */ |
| 20 | public function getFormatters() |
| 21 | { |
| 22 | $formatters = array(); |
| 23 | $providers = array_reverse($this->generator->getProviders()); |
| 24 | $providers[]= new Provider\Base($this->generator); |
| 25 | foreach ($providers as $provider) { |
| 26 | $providerClass = get_class($provider); |
| 27 | $formatters[$providerClass] = array(); |
| 28 | $refl = new \ReflectionObject($provider); |
| 29 | foreach ($refl->getMethods(\ReflectionMethod::IS_PUBLIC) as $reflmethod) { |
| 30 | if ($reflmethod->getDeclaringClass()->getName() == 'Faker\Provider\Base' && $providerClass != 'Faker\Provider\Base') { |
| 31 | continue; |
| 32 | } |
| 33 | $methodName = $reflmethod->name; |
| 34 | if ($reflmethod->isConstructor()) { |
| 35 | continue; |
| 36 | } |
| 37 | $parameters = array(); |
| 38 | foreach ($reflmethod->getParameters() as $reflparameter) { |
| 39 | $parameter = '$'. $reflparameter->getName(); |
| 40 | if ($reflparameter->isDefaultValueAvailable()) { |
| 41 | $parameter .= ' = ' . var_export($reflparameter->getDefaultValue(), true); |
| 42 | } |
| 43 | $parameters []= $parameter; |
| 44 | } |
| 45 | $parameters = $parameters ? '('. join(', ', $parameters) . ')' : ''; |
| 46 | try { |
| 47 | $example = $this->generator->format($methodName); |
| 48 | } catch (\InvalidArgumentException $e) { |
| 49 | $example = ''; |
| 50 | } |
| 51 | if (is_array($example)) { |
| 52 | $example = "array('". join("', '", $example) . "')"; |
| 53 | } elseif ($example instanceof \DateTime) { |
| 54 | $example = "DateTime('" . $example->format('Y-m-d H:i:s') . "')"; |
| 55 | } elseif ($example instanceof Generator || $example instanceof UniqueGenerator) { // modifier |
| 56 | $example = ''; |
| 57 | } else { |
| 58 | $example = var_export($example, true); |
| 59 | } |
| 60 | $formatters[$providerClass][$methodName . $parameters] = $example; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | return $formatters; |
| 65 | } |
| 66 | } |
| 67 |