/** * Choice between options. Similar to Laravel's choice() function. * If you had written 'apple | apples' in DB, you now can get 'apples' like: * Translator::choice('fruts.apple', 4) * * @param string $localeGroupNeedle * @param number $count Number for search for inside intervals * @param array $replaceRules E.g., ['name' => 'John'] * @param string $locale * @param bool $orDefault * @return mix */ public function choice($localeGroupNeedle, $count = 1, $replacements = false, $orDefault = true) { $text = $this->text($localeGroupNeedle, $replacements, $orDefault); $interval = new Interval(); $interval->decodeStr($text); return $interval->search($count); }
/** * Counts number of unique values that match a specific criterium * @param Interval $interval * @return array List of unique values */ public function uniqueValues($interval, $listable = true, $count = 2) { $uniqueValues = array(); if (!$listable) { return $uniqueValues; } $this->connect(); $sql = $this->match; $sql = str_replace("@period", "BETWEEN '" . $interval->getStartTS("YmdHis") . "' AND '" . $interval->getEndTS("YmdHis") . "' ", $sql); $sql = str_replace("@start", " '" . $interval->getStartTS("YmdHis") . "' ", $sql); $sql = str_replace("@end", " '" . $interval->getEndTS("YmdHis") . "' ", $sql); //echo '<br>'.$sql; $res = mysql_query($sql); if (!$res) { return $uniqueValues; } while ($row = mysql_fetch_array($res)) { $rowArr = array(); for ($i = 0; $i < $count; $i++) { $rowArr[] = $row[$i]; } $uniqueValues[] = $rowArr; } return $uniqueValues; }
/** * Given a message with different plural translations separated by a * pipe (|), this method returns the correct portion of the message based * on the given number, locale and the pluralization rules in the message * itself. * * The message supports two different types of pluralization rules: * * interval: {0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples * indexed: There is one apple|There is %count% apples * * The indexed solution can also contain labels (e.g. one: There is one apple). * This is purely for making the translations more clear - it does not * affect the functionality. * * The two methods can also be mixed: * {0} There is no apples|one: There is one apple|more: There is %count% apples * * @throws InvalidArgumentException * @param string $message The message being translated * @param integer $number The number of items represented for the message * @param string $locale The locale to use for choosing * @return string * * @api */ public function choose($message, $number, $locale) { $parts = explode('|', $message); $explicitRules = array(); $standardRules = array(); foreach ($parts as $part) { $part = trim($part); if (preg_match('/^(?<interval>' . Interval::getIntervalRegexp() . ')\\s+(?<message>.+?)$/x', $part, $matches)) { $explicitRules[$matches['interval']] = $matches['message']; } elseif (preg_match('/^\\w+\\: +(.+)$/', $part, $matches)) { $standardRules[] = $matches[1]; } else { $standardRules[] = $part; } } // try to match an explicit rule, then fallback to the standard ones foreach ($explicitRules as $interval => $m) { if (Interval::test($number, $interval)) { return $m; } } $position = PluralizationRules::get($number, $locale); if (!isset($standardRules[$position])) { throw new \InvalidArgumentException('Unable to choose a translation.'); } return $standardRules[$position]; }
/** * Given a message with different plural translations separated by a * pipe (|), this method returns the correct portion of the message based * on the given number, locale and the pluralization rules in the message * itself. * * The message supports two different types of pluralization rules: * * interval: {0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples * indexed: There is one apple|There are %count% apples * * The indexed solution can also contain labels (e.g. one: There is one apple). * This is purely for making the translations more clear - it does not * affect the functionality. * * The two methods can also be mixed: * {0} There are no apples|one: There is one apple|more: There are %count% apples * * @param string $message The message being translated * @param int $number The number of items represented for the message * @param string $locale The locale to use for choosing * * @return string * * @throws \InvalidArgumentException * * @api */ public function choose($message, $number, $locale) { $parts = explode('|', $message); $explicitRules = array(); $standardRules = array(); foreach ($parts as $part) { $part = trim($part); if (preg_match('/^(?P<interval>' . Interval::getIntervalRegexp() . ')\\s*(?P<message>.*?)$/x', $part, $matches)) { $explicitRules[$matches['interval']] = $matches['message']; } elseif (preg_match('/^\\w+\\:\\s*(.*?)$/', $part, $matches)) { $standardRules[] = $matches[1]; } else { $standardRules[] = $part; } } // try to match an explicit rule, then fallback to the standard ones foreach ($explicitRules as $interval => $m) { if (Interval::test($number, $interval)) { return $m; } } $position = PluralizationRules::get($number, $locale); if (!isset($standardRules[$position])) { // when there's exactly one rule given, and that rule is a standard // rule, use this rule if (1 === count($parts) && isset($standardRules[0])) { return $standardRules[0]; } throw new \InvalidArgumentException(sprintf('Unable to choose a translation for "%s" with locale "%s" for value "%d". Double check that this translation has the correct plural options (e.g. "There is one apple|There are %%count%% apples").', $message, $locale, $number)); } return $standardRules[$position]; }
/** * Run the database seeds. * * @return void */ public function run() { $intervals = [['start' => '08:00', 'end' => '09:20', 'duration' => 1.5, 'day' => 'Sunday'], ['start' => '09:30', 'end' => '10:50', 'duration' => 1.5, 'day' => 'Sunday'], ['start' => '11:00', 'end' => '12:20', 'duration' => 1.5, 'day' => 'Sunday'], ['start' => '12:30', 'end' => '13:50', 'duration' => 1.5, 'day' => 'Sunday'], ['start' => '14:00', 'end' => '15:20', 'duration' => 1.5, 'day' => 'Sunday'], ['start' => '15:30', 'end' => '16:50', 'duration' => 1.5, 'day' => 'Sunday'], ['start' => '08:00', 'end' => '09:20', 'duration' => 1.5, 'day' => 'Monday'], ['start' => '09:30', 'end' => '10:50', 'duration' => 1.5, 'day' => 'Monday'], ['start' => '11:00', 'end' => '12:20', 'duration' => 1.5, 'day' => 'Monday'], ['start' => '12:30', 'end' => '13:50', 'duration' => 1.5, 'day' => 'Monday'], ['start' => '14:00', 'end' => '15:20', 'duration' => 1.5, 'day' => 'Monday'], ['start' => '15:30', 'end' => '16:50', 'duration' => 1.5, 'day' => 'Monday'], ['start' => '08:00', 'end' => '09:20', 'duration' => 1.5, 'day' => 'Tuesday'], ['start' => '09:30', 'end' => '10:50', 'duration' => 1.5, 'day' => 'Tuesday'], ['start' => '11:00', 'end' => '12:20', 'duration' => 1.5, 'day' => 'Tuesday'], ['start' => '12:30', 'end' => '13:50', 'duration' => 1.5, 'day' => 'Tuesday'], ['start' => '14:00', 'end' => '15:20', 'duration' => 1.5, 'day' => 'Tuesday'], ['start' => '15:30', 'end' => '16:50', 'duration' => 1.5, 'day' => 'Tuesday'], ['start' => '08:00', 'end' => '09:20', 'duration' => 1.5, 'day' => 'Wednesday'], ['start' => '09:30', 'end' => '10:50', 'duration' => 1.5, 'day' => 'Wednesday'], ['start' => '11:00', 'end' => '12:20', 'duration' => 1.5, 'day' => 'Wednesday'], ['start' => '12:30', 'end' => '13:50', 'duration' => 1.5, 'day' => 'Wednesday'], ['start' => '14:00', 'end' => '15:20', 'duration' => 1.5, 'day' => 'Wednesday'], ['start' => '15:30', 'end' => '16:50', 'duration' => 1.5, 'day' => 'Wednesday'], ['start' => '08:00', 'end' => '09:20', 'duration' => 1.5, 'day' => 'Thursday'], ['start' => '09:30', 'end' => '10:50', 'duration' => 1.5, 'day' => 'Thursday'], ['start' => '11:00', 'end' => '12:20', 'duration' => 1.5, 'day' => 'Thursday'], ['start' => '12:30', 'end' => '13:50', 'duration' => 1.5, 'day' => 'Thursday'], ['start' => '14:00', 'end' => '15:20', 'duration' => 1.5, 'day' => 'Thursday'], ['start' => '15:30', 'end' => '16:50', 'duration' => 1.5, 'day' => 'Thursday']]; foreach ($intervals as $key => $interval) { Interval::create($interval); } }
/** * Retrieve chart data per date interval * @param StatsDataProvider $dataprovider Provider class for chart data * @param bool $aggregated Should counts be added * @param array $intervals List of Interval objects * @param bool $countable If true, result can be listet. * @return array List of numbers */ public static function getDataPerDateInterval($dataprovider, $aggregated, $intervals, $countable = false) { $data = array(); $sum = 0; if ($aggregated == 'aggregated') { $intervalBefore = new Interval(); $intervalBefore->setStartTS(0); $intervalBefore->setEndTS($intervals[0]->getStartTS()); $sum = $dataprovider->countInInterval($intervalBefore, $countable); } foreach ($intervals as $interval) { $item = $dataprovider->countInInterval($interval, $countable); if ($aggregated == 'aggregated') { $sum += $item; $data[] = $sum; } else { $data[] = $item; } } return $data; }
/** * Counts number of unique values that match a specific criterium * @param Interval $interval * @return array List of unique values */ public function uniqueValues($interval, $listable = true, $count = 2) { $uniqueValues = array(); if (!$listable) { return $uniqueValues; } $this->connect(); $sql = $this->match; $sql = str_replace("@period", "BETWEEN '" . $interval->getStartTS("Y-m-d H:i:s+00") . "' AND '" . $interval->getEndTS("Y-m-d H:i:s+00") . "' ", $sql); $sql = str_replace("@start", " '" . $interval->getStartTS("Y-m-d H:i:s+00") . "' ", $sql); $sql = str_replace("@end", " '" . $interval->getEndTS("Y-m-d H:i:s+00") . "' ", $sql); $sql = str_replace("stats_ts", "to_timestamp(stats_ts,'YYYYMMDDHH24MISS')", $sql); $res = pg_query($this->conn, $sql); while ($row = pg_fetch_array($res)) { $rowArr = array(); for ($i = 0; $i < $count; $i++) { $rowArr[] = $row[$i]; } $uniqueValues[] = $rowArr; } return $uniqueValues; }
/** * Get a list of intervals for a given period * @param string $step Interval descriptor as defined in BsDiagram * @param string $offset Date string * @param string $limit Date string * @param string $modLabel Date format item, e.g. "M" for "Jan, Feb" * @return Interval */ public static function getIntervals($step = "W", $offset = '05/01/2007', $limit = '01/31/2009', $modLabel = false) { $intervals = array(); $starttime = strtotime($offset); $endtime = strtotime($limit); $interval = new Interval(); $interval->setStartTS($starttime); $old = idate($step, $starttime); if ($modLabel) { $interval->setLabel(date($modLabel, $starttime)); } else { $interval->setLabel($old); } $oldts = $starttime; for ($ts = $starttime; $ts <= $endtime; $ts += 86400) { $oldts = $ts - 1; $cur = idate($step, $ts); if ($old != $cur) { $interval->setEndTS($oldts); $intervals[] = $interval; $interval = new Interval(); $interval->setStartTS($ts); if ($modLabel) { $interval->setLabel(date($modLabel, $ts)); } else { $interval->setLabel($cur); } //echo "<br/>-----------------------"; } $old = $cur; //echo "<br/>".$oldts." :: ".date("d.m.YHis", $oldts-1); } $oldts = $oldts + 86400; $interval->setEndTS($oldts); $intervals[] = $interval; //var_dump($intervals); return $intervals; }
function test_get_intervals() { $interval = new Interval(array(100, 101, 102, 103, 104, 105, 110, 111, 113, 114, 115, 150)); $this->assertEquals('[100-105], [110-111], [113-115], [150]', $interval->get_intervals()); }
<?php require_once __DIR__ . '/../vendor/autoload.php'; use CapMousse\ReactRestify\Server; use CapMousse\ReactRestify\Runner; use CapMousse\ReactRestify\Async\{Interval, Timeout}; require_once __DIR__ . "/src/Controllers/ProductController.php"; $server = new Server("ReactAPI", "0.0.0.1"); Interval::run([new App\Controllers\ProductController(), 'test'], 1); Interval::run(function () { echo "closure method \n"; }, 1); $server->any('/products', 'App\\Controllers\\ProductController')->where('id', '[0-9]?'); $server->on('NotFound', function ($request, $response, $next) { (new Timeout())->run(function () { echo "hello"; }, 4); $response->write('You fail, 404'); $response->setStatus(404); $next(); }); $runner = new Runner($server); $runner->listen(1337);
/** * * @global User $wgUser * @global WebRequest $wgRequest * @return type */ public static function ajaxSave() { $aResult = array("success" => false, "errors" => array(), "message" => '', "data" => array()); global $wgUser, $wgRequest; if (!$wgUser->isAllowed('read')) { $aResult["message"] = wfMessage('bs-statistics-not-allowed')->plain(); return json_encode($aResult); } $sDiagram = $wgRequest->getVal('inputDiagrams', ''); $sGrain = $wgRequest->getVal('InputDepictionGrain', 'Y'); $sFrom = $wgRequest->getVal('inputFrom', ''); $sMode = $wgRequest->getVal('rgInputDepictionMode', ''); $sTo = $wgRequest->getVal('inputTo', ''); $aAvailableDiagrams = Statistics::getAvailableDiagrams(); $aAllowedDiaKeys = array_keys($aAvailableDiagrams); if (empty($sDiagram)) { $aResult["errors"]['inputDiagrams'] = wfMessage('bs-statistics-err-emptyinput')->plain(); } elseif (!in_array($sDiagram, $aAllowedDiaKeys)) { $aResult["errors"]['inputDiagrams'] = wfMessage('bs-statistics-err-unknowndia')->plain(); } if (!array_key_exists($sGrain, BsConfig::get('MW::Statistics::AvailableGrains'))) { $aResult["errors"]['InputDepictionGrain'] = wfMessage('bs-statistics-err-unknowngrain')->plain(); } if (empty($sFrom)) { $aResult["errors"]['inputFrom'] = wfMessage('bs-statistics-err-emptyinput')->plain(); } elseif (!($oFrom = DateTime::createFromFormat('d.m.Y', $sFrom))) { $aResult["errors"]['inputFrom'] = wfMessage('bs-statistics-err-invaliddate')->plain(); } if (empty($sTo)) { $aResult["errors"]['inputTo'] = wfMessage('bs-statistics-err-emptyinput')->plain(); } elseif (!($oTo = DateTime::createFromFormat('d.m.Y', $sFrom))) { $aResult["errors"]['inputTo'] = wfMessage('bs-statistics-err-invaliddate')->plain(); } elseif ($oTo > new DateTime()) { $aResult["errors"]['inputTo'] = wfMessage('bs-statistics-err-invaliddate')->plain(); } if (isset($oFrom) && isset($oTo) && $oFrom > $oTo) { $aResult["errors"]['inputTo'] = wfMessage('bs-statistics-err-invalidtofromrelation')->plain(); } if (empty($sMode)) { $aResult["errors"]['rgInputDepictionMode'] = wfMessage('bs-statistics-err-emptyinput')->plain(); } elseif (!in_array($sMode, array('absolute', 'aggregated', 'list'))) { $aResult["errors"]['rgInputDepictionMode'] = wfMessage('bs-statistics-err-unknownmode')->plain(); } elseif (!isset($aResult["errors"]['inputDiagrams']) && $sMode == 'list' && !$aAvailableDiagrams[$sDiagram]->isListable()) { $aResult["errors"]['rgInputDepictionMode'] = wfMessage('bs-statistics-err-modeunsupported')->plain(); } if (!empty($aResult['errors'])) { return json_encode($aResult); } $oDiagram = Statistics::getDiagram($sDiagram); $oDiagram->setStartTime($sFrom); $oDiagram->setEndTime($sTo); $oDiagram->setActualGrain($sGrain); $oDiagram->setModLabel($sGrain); $oDiagram->setMode($sMode); //$oDiagram->setMessage( $sMessage ); //$oDiagram->setFilters( $aDiagFilter ); switch ($oDiagram->getActualGrain()) { // Here, only those grains are listed where label code differs from grain code. case 'm': $oDiagram->setModLabel('M y'); break; case 'd': $oDiagram->setModLabel('d.m'); break; //default : $oDiagram->modLabel = false; } switch ($oDiagram->getDataSource()) { case BsDiagram::DATASOURCE_DATABASE: global $wgDBtype, $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname; switch ($wgDBtype) { case "postgres": $oReader = new PostGreSQLDbReader(); break; case "oracle": $oReader = new OracleDbReader(); break; default: $oReader = new MySQLDbReader(); } //$oReader = $sDbType == 'mysql' ? new MySQLDbReader() : new PostGreSQLDbReader(); $oReader->host = $wgDBserver; $oReader->user = $wgDBuser; $oReader->pass = $wgDBpassword; $oReader->db = $wgDBname; break; } $intervals = Interval::getIntervalsFromDiagram($oDiagram); if (count($intervals) > BsConfig::get('MW::Statistics::MaxNumberOfIntervals')) { $aResult['message'] = wfMessage('bs-statistics-interval-too-big')->plain(); return json_encode($aResult); } //set_time_limit( 60 ); // TODO MRG (20.12.10 00:01): already called before //$intervals = Interval::getIntervalsFromDiagram( $oDiagram ); //$oDiagram->addFilterText( wfMessage( 'bs-statistics-from-to', $oDiagram->getStartTime(), $oDiagram->getEndTime() )->plain() ); // TODO MRG (20.12.10 00:19): This should be getModeText //$oDiagram->addFilterText( "\n".wfMessage( 'bs-statistics-mode' )->plain().": ".wfMessage( $oDiagram->getMessage() )->plain() ); // PostgreSQL-Check (uses mwuser instead of user) global $wgDBtype; // Pls. keep the space after user, otherwise, user_groups is also replaced $sql = $oDiagram->getSQL(); if ($wgDBtype == 'postgres') { $sql = str_replace('#__user', '#__mwuser', $sql); } if ($wgDBtype == 'postgres') { $sql = str_replace('#__mwuser_', '#__user_', $sql); } global $wgDBprefix; $sql = str_replace("#__", $wgDBprefix, $sql); foreach ($oDiagram->getFilters() as $oFilter) { $sFilterSql = $oFilter->getSql(); $sql = str_replace($oFilter->getSqlKey(), $sFilterSql, $sql); } $oReader->match = $sql; $oDiagram->setData(BsCharting::getDataPerDateInterval($oReader, $oDiagram->getMode(), $intervals, $oDiagram->isListable())); if ($oDiagram->isList()) { //$aResult['data']['list'] = BsCharting::drawTable($oDiagram); $aResult['data']['list'] = BsCharting::prepareList($oDiagram, $oReader); $aResult['label'] = $oDiagram->getTitle(); $aResult['success'] = true; return json_encode($aResult); } $aData = $oDiagram->getData(); $i = 0; foreach ($intervals as $interval) { $aResult['data'][] = array('name' => $interval->getLabel(), 'hits' => (int) $aData[$i]); $i++; } $aAvalableGrains = BsConfig::get('MW::Statistics::AvailableGrains'); $sLabelMsgKey = 'bs-statistics-label-time'; if (isset($aAvalableGrains[$oDiagram->getActualGrain()])) { $sLabelMsgKey = $aAvalableGrains[$oDiagram->getActualGrain()]; } $aResult['label'] = wfMessage($sLabelMsgKey)->plain(); $aResult['success'] = true; return json_encode($aResult); }
function smarty_modifier_interval_add($base = 0, $add) { $base = new Interval($base); $add = new Interval($add); return $base->add($add)->getValue(); }
/** * @param CalendarDate $start * @param CalendarDate $end * @param int|null $recurrence * * @return static */ public static function createFromStartEnd(CalendarDate $start, CalendarDate $end, $recurrence = null) { return static::create($start, null, Interval::create($start, $end)->duration(), $recurrence); }
/** * Funkce pro porovnání 2 numerických veličin vyjádřených výčtem prvků */ public static function numericEnumsCoverage($enumArr1, $enumArr2, $coverageMode, $minusInf = -999999999, $plusInf = 999999999) { if (count($enumArr1) > 0 && count($enumArr2) > 0) { //shody hodnot $match1 = 0; foreach ($enumArr1 as $value1) { if (!array_search($value1, $enumArr2) === false) { $match1++; } } $match1 = $match1 / max(count($enumArr1), count($enumArr2)); //velikost mnozin //zjistime min a max u jednotlivych mnozin $min1 = 999999999; $max1 = -999999999; foreach ($enumArr1 as $value1) { if ($value1 < $min1) { $min1 = $value1; } if ($value1 > $max1) { $max1 = $value1; } } $min2 = 999999999; $max2 = -999999999; foreach ($enumArr2 as $value2) { if ($value2 < $min2) { $min2 = $value2; } if ($value2 > $max2) { $max2 = $value2; } } //porovname je jako intervaly $interval1 = new Interval($min1, $max1, true, true, $minusInf, $plusInf); $match2 = $interval1->containtsInterval(new Interval($min2, $max2, true, true, $minusInf, $plusInf)); return (2 * $match2 + $match1) / 3; } else { return 0; } }