/** * Constructor. Register built in Transformers. */ private function __construct() { $this->registerBuiltInTransformers(); usort($this->items, function (TransformerInterface $a, TransformerInterface $b) { return Utils::cmpInt($b->getPriority(), $a->getPriority()); }); }
/** * Sort fixers by their priorities. * * @return $this */ private function sortFixers() { // Schwartzian transform is used to improve the efficiency and avoid // `usort(): Array was modified by the user comparison function` warning for mocked objects. $data = array_map(function (FixerInterface $fixer) { return array($fixer, $fixer->getPriority()); }, $this->fixers); usort($data, function (array $a, array $b) { return Utils::cmpInt($b[1], $a[1]); }); $this->fixers = array_map(function (array $item) { return $item[0]; }, $data); return $this; }
/** * @dataProvider provideCmpIntCases */ public function testCmpInt($expected, $left, $right) { $this->assertSame($expected, Utils::cmpInt($left, $right)); }
/** * Get the delimiter that would require the least escaping in a regular expression. * * @param string $pattern the regular expression * * @return string the preg delimiter */ private function getBestDelimiter($pattern) { // try do find something that's not used $delimiters = array(); foreach (self::$delimiters as $k => $d) { if (false === strpos($pattern, $d)) { return $d; } $delimiters[$d] = array(substr_count($pattern, $d), $k); } // return the least used delimiter, using the position in the list as a tie breaker uasort($delimiters, function ($a, $b) { if ($a[0] === $b[0]) { return Utils::cmpInt($a, $b); } return $a[0] < $b[0] ? -1 : 1; }); return key($delimiters); }