Created
August 9, 2017 14:36
-
-
Save tatesuke/bb8301a7e509e95d7c28e270faf4483d to your computer and use it in GitHub Desktop.
PHPのページUtil
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 | |
| 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