Created
November 8, 2012 09:43
-
-
Save k-holy/4037832 to your computer and use it in GitHub Desktop.
Silex + OptionsResolverでFizzBuzz
This file contains hidden or 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 | |
$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); | |
if (file_exists(realpath(__DIR__ . $path))) { | |
return false; | |
} | |
require __DIR__ . DIRECTORY_SEPARATOR . 'index.php'; |
This file contains hidden or 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
{ | |
"require": { | |
"silex/silex": "1.0.*", | |
"symfony/options-resolver": "2.1.*" | |
}, | |
"minimum-stability": "dev" | |
} |
This file contains hidden or 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 | |
require_once realpath(__DIR__ .'/../vendor/autoload.php'); | |
use Silex\Application; | |
use Symfony\Component\OptionsResolver\OptionsResolver; | |
use Symfony\Component\OptionsResolver\Options; | |
$app = new Application(); | |
$app->get('/', function(Application $app) { | |
$fizzBuzz = function($var) { | |
return ($var % 3 * $var % 5) ? sprintf('%d', $var) | |
: ($var % 3 ? '' : 'Fizz') . ($var % 5 ? '' : 'Buzz'); | |
}; | |
$resolver = new OptionsResolver(); | |
$resolver->setRequired(['number']); | |
$resolver->setDefaults(['result' => function(Options $options) use ($fizzBuzz) { | |
return $fizzBuzz($options['number']); | |
}]); | |
return sprintf('<pre>%s</pre>', print_r(array_map(function($var) use ($resolver) { | |
return $resolver->resolve(['number' => $var])['result']; | |
}, range(1, 100)), true)); | |
}); | |
$app->run(); |
This file contains hidden or 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 | |
require_once realpath(__DIR__ .'/../vendor/autoload.php'); | |
use Silex\Application; | |
use Symfony\Component\HttpFoundation\Request; | |
use Symfony\Component\HttpFoundation\Response; | |
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; | |
use Symfony\Component\OptionsResolver\OptionsResolver; | |
use Symfony\Component\OptionsResolver\Options; | |
$app = new Application(); | |
$app['debug'] = true; | |
$app['fizzBuzz'] = $app->protect(function($from, $to) use ($app) { | |
if (!is_int($from) && !ctype_digit($from)) { | |
throw new \InvalidArgumentException( | |
sprintf('From パラメータ "%s" が数値ではありません', (string)$from)); | |
} | |
if (!is_int($to) && !ctype_digit($to)) { | |
throw new \InvalidArgumentException( | |
sprintf('To パラメータが "%s" が数値ではありません', (string)$to)); | |
} | |
if ($from > $to) { | |
throw new \InvalidArgumentException( | |
sprintf('To パラメータは From パラメータよりも大きい値をセットしてください From:%s To:%s', (string)$from, (string)$to)); | |
} | |
$fizzBuzz = function($var) { | |
return ($var % 3 * $var % 5) ? sprintf('%d', $var) | |
: ($var % 3 ? '' : 'Fizz') . ($var % 5 ? '' : 'Buzz'); | |
}; | |
$resolver = new OptionsResolver(); | |
$resolver->setRequired(['number']); | |
$resolver->setDefaults(['result' => function(Options $options) use ($fizzBuzz) { | |
return $fizzBuzz($options['number']); | |
}]); | |
return array_map(function($var) use ($resolver) { | |
return $resolver->resolve(['number' => $var])['result']; | |
}, range(intval($from), intval($to))); | |
}); | |
$app->get('/{var}/{type}', function(Application $app, Request $request, $var, $type) { | |
$fizzBuzz = $app['fizzBuzz']; | |
$vars = explode('-', $var); | |
try { | |
$results = $fizzBuzz($vars[0], isset($vars[1]) ? $vars[1] : $vars[0]); | |
} catch (\InvalidArgumentException $e) { | |
if ($type == 'json') { | |
return $app->json($e->getMessage(), 400); | |
} | |
throw new BadRequestHttpException($e->getMessage()); | |
} | |
return ($type == 'json') | |
? $app->json($results) | |
: new Response(sprintf('<pre>%s</pre>', print_r($results, true))); | |
}) | |
->value('var', '1-100') | |
->value('type', 'html'); | |
$app->run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment