Skip to content

Instantly share code, notes, and snippets.

@k-holy
Created January 30, 2013 05:51
Show Gist options
  • Save k-holy/4671008 to your computer and use it in GitHub Desktop.
Save k-holy/4671008 to your computer and use it in GitHub Desktop.
IMAGETYPE定数と拡張子とmimeTypeの一覧(遅延評価版)
<?php
namespace Acme;
class ImageType
{
private $name;
private $value;
private $supportedImageTypes;
public function __construct($name, $supportedImageTypes = null)
{
if (!defined($name)) {
throw new \InvalidArgumentException(
sprintf('The constant "%s" does not exists.', $name));
}
$this->name = $name;
$this->supportedImageTypes = (isset($supportedImageTypes)) ? $supportedImageTypes : imagetypes();
}
public function __get($name)
{
$method = 'is' . ucfirst($name);
if (method_exists($this, $method)) {
return (bool)$this->{$method}();
}
$method = 'get' . ucfirst($name);
if (method_exists($this, $method)) {
return $this->{$method}();
}
throw new \InvalidArgumentException(
sprintf('The property "%s" does not exists.', $name));
}
private function getName()
{
return $this->name;
}
private function getValue()
{
if (!isset($this->value)) {
$this->value = constant($this->name);
}
return $this->value;
}
private function isSupported()
{
$_type = 0;
switch ($this->getValue()) {
case IMAGETYPE_GIF:
$_type = IMG_GIF;
break;
case IMAGETYPE_JPEG:
$_type = IMG_JPG;
break;
case IMAGETYPE_PNG:
$_type = IMG_PNG;
break;
case IMAGETYPE_WBMP:
$_type = IMG_WBMP;
break;
}
return (($_type & $this->supportedImageTypes) !== 0);
}
private function getExtension()
{
$extension = image_type_to_extension($this->getValue());
if ($extension === false) {
return null;
}
return $extension;
}
private function getMimeType()
{
$mime = image_type_to_mime_type($this->getValue());
if ($mime === 'application/octet-stream') {
return null;
}
return $mime;
}
}
define('SUPPORTED_IMAGE_TYPES', imagetypes());
$constants = get_defined_constants(true)['standard'];
$image_types = array_intersect_ukey($constants, $constants,
function($name1, $name2) {
return (strpos($name1, 'IMAGETYPE_') === 0) ? 0 : 1;
}
);
$data = array();
array_walk($image_types, function($value, $name) use (&$data) {
$data[] = new ImageType($name, SUPPORTED_IMAGE_TYPES);
});
$h = function($var, $default=null) {
if (!isset($var)) {
return $default;
}
if (is_bool($var)) {
return ($var) ? '*' : '';
}
return htmlspecialchars($var, ENT_QUOTES, 'UTF-8');
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<style type="text/css">
body {font-family:monospace;}
table {border-collapse:collapse; margin:5px 0px;}
table * {text-align:left;}
caption {font-size:200%;}
th, td {padding:2px; border:solid 1px #999999;}
</style>
</head>
<body>
<table>
<thead>
<tr>
<th>name</th>
<th>value</th>
<th>supported</th>
<th>extension</th>
<th>mimeType</th>
</tr>
</thead>
<tbody>
<?php foreach ($data as $image) : ?>
<tr>
<th><?=$h($image->name)?></th>
<td><?=$h($image->value)?></td>
<td><?=$h($image->supported)?></td>
<td><?=$h($image->extension)?></td>
<td><?=$h($image->mimeType)?></td>
</tr>
<?php endforeach ?>
</tbody>
</table>
</body>
</html>
@k-holy
Copy link
Author

k-holy commented Jan 30, 2013

https://gist.github.com/1437431 をネタに__get()マジックメソッドを使った遅延評価プロパティの例。
PHP5.4のArray dereferencingも初めて使ってみた。

遅延評価というと呼ばれるまで無駄なオブジェクトを作らないとか速度面での利点が挙げられるけど、テンプレートの構文が綺麗になる、設計面でのDRYが促進される効果もあるのかなーと。今更。
アプリケーションコードではまだバリバリ配列使ってるので早くなんとかしないと。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment