Skip to content

Instantly share code, notes, and snippets.

@tatesuke
Created August 9, 2017 14:36
Show Gist options
  • Select an option

  • Save tatesuke/bb8301a7e509e95d7c28e270faf4483d to your computer and use it in GitHub Desktop.

Select an option

Save tatesuke/bb8301a7e509e95d7c28e270faf4483d to your computer and use it in GitHub Desktop.
PHPのページUtil
<?php
class PageUtil {
private $values = array();
public function put($name, $value) {
$this->values[$name] = $value;
}
public function val(...$names) {
$val = $this->values;
foreach ($names as $name) {
$val = $val[$name];
}
return new PageValue($val, true);
}
public function unescapeVal(...$names) {
$val = $this->values;
foreach ($names as $name) {
$val = $val[$name];
}
return new PageValue($val, false);
}
}
class PageValue {
private $value;
private $isEscapeEnable;
public function __construct($value, $isEscapeEnable = true) {
$this->value = $value;
$this->isEscapeEnable = $isEscapeEnable;
}
public function format($format) {
return new PageValue(sprintf($format, $this->value), $this->isEscapeEnable);
}
public function dateFormat($format) {
return new PageValue(date($format, $this->value), $this->isEscapeEnable);
}
public function __toString() {
$str;
if (method_exists($this->value, "__toString")) {
$str = $this->value->__toString();
} else {
$str = (string)$this->value;
}
return $this->isEscapeEnable ? htmlspecialchars($str) : $str;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment