setTimestamp($u); return date_i18n( $f, $u + $d->getOffset() ); } /** * Wrapper for sprintf so we can handle PHP 8 exceptions * @param string * @param array * @return string */ public static function format( $format, array $args ){ try { return vsprintf($format,$args); } // Note that PHP8 will throw Error (not Exception), PHP 7 will trigger E_WARNING catch( Error $e ){ Loco_error_AdminNotices::warn( $e->getMessage().' in vsprintf('.var_export($format,true).')' ); return ''; } } /** * @internal * @param string property name * @return mixed */ public function __get( $p ){ return isset($this[$p]) ? $this[$p] : null; } /** * @param string property name * @return bool */ public function has( $p ){ return isset($this[$p]); } /** * Print escaped property value * @param string property key * @return string empty string */ public function e( $p ){ $text = $this->__get($p); echo $this->escape( $text ); return ''; } /** * Print property as string date, including time * @param string property name * @param string date format * @return string empty string */ public function date( $p, $f = null ){ $u = (int) $this->__get($p); if( $u > 0 ){ echo $this->escape( self::date_i18n($u,$f) ); } return ''; } /** * Print property as a string-formatted number * @param string property name * @param int optional decimal places * @return string empty string */ public function n( $p, $dp = null ){ // number_format_i18n is pre-escaped for HTML echo number_format_i18n( $this->__get($p), $dp ); return ''; } /** * Print property with passed formatting string * e.g. $params->f('name', 'My name is %s' ); * @param string property name * @param string formatting string * @return string empty string */ public function f( $p, $f = '%s' ){ echo $this->escape( self::format( $f, [$this->__get($p)] ) ); return ''; } /** * Print property value for JavaScript * @param string property name * @return string empty string */ public function j( $p ){ echo json_encode($this->__get($p) ); return ''; } /** * @return array */ #[ReturnTypeWillChange] public function jsonSerialize(){ return $this->getArrayCopy(); } /** * Fetch whole object as JSON * @return string */ public function exportJson(){ return json_encode( $this->jsonSerialize() ); } /** * Merge parameters into ours * @param ArrayObject * @return Loco_mvc_ViewParams */ public function concat( ArrayObject $more ){ foreach( $more as $name => $value ){ $this[$name] = $value; } return $this; } /** * Debugging function * @codeCoverageIgnore */ public function dump(){ echo '
',$this->escape( json_encode( $this->getArrayCopy(),JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE ) ),''; } /** * @param callable * @return Loco_mvc_ViewParams */ public function sort( $callback ){ $raw = $this->getArrayCopy(); uasort( $raw, $callback ); $this->exchangeArray( $raw ); return $this; } }