/** * Format a string depending on a quantity. * * See the class documentation for defining `$format`. * * @param string $format * @param string $quantity * * @return mixed */ public static function format($format, $quantity) { $quantity = Numeric::ensure($quantity); $callback = function ($matches) use($quantity) { // Get available choices $choices = preg_split('/\\|/', $matches[2]); // Assume plural $choice = 0; // Choose singular if ($quantity === 1.0 || $quantity === 1) { $choice = 1; } // Choose zero if it's defined, otherwise keep using plural format if (($quantity === 0 || $quantity === 0.0) && isset($choices[2])) { $choice = 2; } return Dot::get($choices, $choice, ''); }; $pattern = '/(?<!\\\\)(\\((.+?(?<!\\\\))\\))/'; $out = preg_replace_callback($pattern, $callback, $format); $out = sprintf($out, $quantity); return $out; }
/** * Get frontmatter value using dot.notation as a key/path. * * @param string $dotPath Dot notation path to look up * @param null|mixed $default Default value to return if the element doesn't exist. Optional, defaults to NULL. * * @return mixed */ public function get($dotPath, $default = null) { return Dot::get($this->data, $dotPath, $default); }