✏️ 正在编辑: Util.php
路径:
/srv/systems_dir/yuppiecred/library/Core/Util.php
提示:
您可以编辑任何文件(包括二进制文件),但请注意不当修改可能导致文件损坏。
<?php class Core_Util { protected $_coluna = null; /** * * @param string $mesAno MM/yyyy */ public static function getQtdDiasUteisMes($mesAno) { $dt = new Zend_Date("01/" . $mesAno); $qtdDiasMes = $dt->get(Zend_Date::MONTH_DAYS); $ultimoDiaMes = new Zend_Date($qtdDiasMes . "/" . $dt->get('MM') . "/" . $dt->get('yyyy')); $diasUteis = 0; while ( $dt->isEarlier($ultimoDiaMes) ) { $dia = $dt->get(Zend_Date::WEEKDAY_DIGIT); if( $dia != 0 && $dia != 6 ){ $diasUteis++; } $dt->addDay(1); } return $diasUteis; } /** @author yuppie * Modifica a key de um array para uma nova, mantendo a ordem do array principal * @param type $array * @param type $positionKey * @param type $newKey * @return type */ public static function changeKeyInArray(&$array = array(), $positionKey = null, $newKey = null) { $keys = array_keys($array); $keys[$positionKey] = $newKey; $array = array_combine($keys, array_values($array)); } /** @author yuppie * Define aonde pretende setar a posição do array * @param type $array * @param type $position * @param type $index * @param type $arrayInsert - o array o qual será inserido na posição setada */ public static function setPositionInArray(&$array = array(), $position = null, $index = null, $arrayInsert = array()) { array_splice($array, $position, $index, $arrayInsert); } public function orderArrayMulti(&$arrayMultidimensional = array(), $coluna = '', $order = null) { $this->_coluna = $coluna; switch ( $order ) { case 'desc': usort($arrayMultidimensional, array( "Core_Util", "cmp" )); break; case 'asc': usort($arrayMultidimensional, 'orderAsc'); break; case 'min': usort($arrayMultidimensional, array( "Core_Util", "cmp_min" )); break; } } function cmp($a, $b) { return $a[$this->_coluna] < $b[$this->_coluna]; } function cmp_min($a, $b) { return $a[$this->_coluna] > $b[$this->_coluna]; } public static function filterValor($valor) { $filter = new Zend_Filter_Digits(); $negativo = 1; if (empty($valor)) { return 0; } if( $valor < 0 ){ $negativo = -1; } if( strripos($valor, ".") || strripos($valor, ",") ){ $newValor = number_format($filter->filter($valor) / 100 * $negativo, 2, ',', '.'); } else { $newValor = number_format($filter->filter($valor) * $negativo, 2, ',', '.'); } return $newValor; } public static function arraySumOnKey($array, $array_key) { $totally = 0; foreach ( $array as $sec_array ) { $totally += $sec_array[$array_key]; } return $totally; } /** * DateTime convert * @param string $strDate * @param string $inputFormat * @param string $outputFormat (Y-m-d) * @return string|array Formmated date or errors */ public static function convertDate($strDate, $inputFormat = 'auto', $outputFormat = "Y-m-d") { if (empty($strDate)) { return null; } $strDate = substr($strDate, 0, 10); if (strpos($strDate, " ") !== false) { $strDate = substr($strDate, 0, 8); } if( $inputFormat == 'auto' ){ $inputFormat = self::formatDetect($strDate); } $data = date_create_from_format($inputFormat, substr($strDate, 0, 10)); $errors = array_filter(DateTime::getLastErrors()); if( $errors ){ return $errors; } if( substr($data->format('Y'), 0, 2) == "00" ){ return array( 'error_count' => 1, 'errors' => array( 'Formato de entrada diferente do informado' ) ); } return $data->format($outputFormat); } private static function formatDetect($strDate) { $delimitador = "/"; if( strstr($strDate, "-") ){ $delimitador = "-"; }elseif( strstr($strDate, ".") ){ $delimitador = "."; } $data = explode($delimitador, $strDate); if(count($data) <= 1){ return; } if( strlen($data[0]) == 4 ){ return "Y{$delimitador}m{$delimitador}d"; }elseif( strlen($data[2]) == 4 ){ return "d{$delimitador}m{$delimitador}Y"; }elseif(strlen($data[1]) == 3 && strlen($data[2]) == 2){ return "d{$delimitador}M{$delimitador}y"; }elseif(strlen($data[1]) == 3 && strlen($data[2]) == 4){ return "d{$delimitador}M{$delimitador}Y"; }elseif( strlen($data[2]) == 2 ){ return "d{$delimitador}m{$delimitador}y"; } } /** * Verifica se a extensão do arquivo corresponde ao tipo de informação gravada no arquivo * * @param string $filename * @return boolean */ public static function verifyExtensionCorrespondsToMime($filename) { try{ $extension = pathinfo($filename,PATHINFO_EXTENSION); $validMimes = self::mimesByExtension(strtolower($extension)); $mime = self::detectFileMime($filename); $valid = in_array($mime, $validMimes); }catch (InvalidArgumentException $ex){ $valid = false; } return $valid; } /** * Retorna uma lista de possíveis mimetypes por extensão * * @param string $extension * @return array * * @throws InvalidArgumentException */ private static function mimesByExtension($extension) { $mimes = array( 'aac' => array('audio/aac'), 'abw' => array('application/x-abiword'), 'arc' => array('application/octet-stream'), 'avi' => array('video/x-msvideo'), 'azw' => array('application/vnd.amazon.ebook'), 'bin' => array('application/octet-stream'), 'bz' => array('application/x-bzip'), 'bz2' => array('application/x-bzip2'), 'csh' => array('application/x-csh'), 'css' => array('text/css', 'text/plain'), 'csv' => array('text/csv', 'text/plain', 'application/octet-stream'), 'doc' => array('application/msword'), 'eot' => array('application/vnd.ms-fontobject'), 'epub' => array('application/epub+zip'), 'gif' => array('image/gif'), 'htm' => array('text/html'), 'html' => array('text/html'), 'ico' => array('image/x-icon'), 'ics' => array('text/calendar'), 'jar' => array('application/java-archive'), 'jpeg' => array('image/jpeg'), 'jpg' => array('image/jpeg'), 'js' => array('application/javascript'), 'json' => array('application/json'), 'mid' => array('audio/midi'), 'midi' => array('audio/midi'), 'mpeg' => array('video/mpeg'), 'mpkg' => array('application/vnd.apple.installer+xml'), 'odp' => array('application/vnd.oasis.opendocument.presentation'), 'ods' => array('application/vnd.oasis.opendocument.spreadsheet'), 'odt' => array('application/vnd.oasis.opendocument.text'), 'oga' => array('audio/ogg'), 'ogv' => array('video/ogg'), 'ogx' => array('application/ogg'), 'otf' => array('font/otf'), 'png' => array('image/png'), 'pdf' => array('application/pdf'), 'ppt' => array('application/vnd.ms-powerpoint'), 'rar' => array('application/x-rar-compressed'), 'rtf' => array('application/rtf'), 'sh' => array('application/x-sh'), 'svg' => array('image/svg+xml'), 'swf' => array('application/x-shockwave-flash'), 'tar' => array('application/x-tar'), 'tif' => array('image/tiff'), 'tiff' => array('image/tiff'), 'ts' => array('application/typescript'), 'ttf' => array('font/ttf'), 'txt' => array('text/plain'), 'vsd' => array('application/vnd.visio'), 'wav' => array('audio/x-wav'), 'weba' => array('audio/webm'), 'webm' => array('video/webm'), 'webp' => array('image/webp'), 'woff' => array('font/woff'), 'woff2' => array('font/woff2'), 'xhtml' => array('application/xhtml+xml'), 'xls' => array('application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/vnd.ms-office'), 'xlsx' => array('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/vnd.ms-excel', 'application/vnd.ms-office'), 'xml' => array('application/xml'), 'xul' => array('application/vnd.mozilla.xul+xml'), 'zip' => array('application/zip'), '3gp' => array('video/3gpp', 'audio/3gpp'), '3g2' => array('video/3gpp2', 'audio/3gpp2'), '7z' => array('application/x-7z-compressed') ); if(!key_exists($extension, $mimes)){ throw new InvalidArgumentException("Extensão desconhecida"); } return $mimes[$extension]; } /** * Detecta a codificacao do arquivo utilizando o comando file do linux que possui mais precisao que o mb_detect * * @param $filepath * @return string */ public static function detectFileEncoding($filepath) { $output = array(); exec("file -i '$filepath'", $output); if (isset($output[0])){ $ex = explode('charset=', $output[0]); return isset($ex[1]) ? $ex[1] : null; } return null; } /** * Detecta o mime do arquivo utilizando o comando file do linux que possui mais precisao que o finfo_file * * @param $filepath * @return string */ public static function detectFileMime($filepath) { $output = array(); exec("file -i '$filepath'", $output); if (isset($output[0])){ $matches = array(); $success = preg_match("#:\s.*\;#",$output[0], $matches); $result = current($matches); return $success ? substr($result, 2, strlen($result) -3) : ''; } return ''; } public static function calcularIdade($dataNascimento, $simplificada = false) { if( !empty($dataNascimento) && $dataNascimento != '0000-00-00' && strlen($dataNascimento) > 8){ $date = Zend_Date::now(); $dataNascimento = new Zend_date($dataNascimento); $dtMes = clone $date; $dtDia = clone $date; $mesAnterior = clone $date; $mesAnterior->subMonth(1); $mesAnterior->setDay($dataNascimento->get('dd')); $anos = $date->subDate($dataNascimento, Zend_Date::YEAR)->get('YY'); $meses = $dtMes->subDate($dataNascimento, Zend_Date::MONTH)->get('MM'); $dias = $dtDia->subDay($mesAnterior)->get('dd'); $txtMes = $meses > 1 ? "meses" : "mês"; $txtDia = $dias > 1 ? "dias" : "dia"; if ($simplificada) { return $anos; } return "{$anos} anos, {$meses} {$txtMes} e $dias $txtDia"; } return null; } }
💾 保存文件
← 返回文件管理器