Skip to content
This repository has been archived by the owner on Aug 13, 2021. It is now read-only.

Commit

Permalink
Bump
Browse files Browse the repository at this point in the history
  • Loading branch information
f3l1x committed Apr 22, 2015
0 parents commit a6b2af8
Show file tree
Hide file tree
Showing 13 changed files with 524 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitattributes
@@ -0,0 +1,6 @@
.gitattributes export-ignore
.gitignore export-ignore
.travis.yml export-ignore
composer.lock export-ignore
tests/ export-ignore
*.md export-ignore
6 changes: 6 additions & 0 deletions .gitignore
@@ -0,0 +1,6 @@
# Folders
/vendor
# Files
/composer.lock
/tests/*.log
/tests/tmp
25 changes: 25 additions & 0 deletions .travis.yml
@@ -0,0 +1,25 @@
language: php
php:
- 5.4
- 5.5
- 5.6
- 7.0
- hhvm

matrix:
allow_failures:
- php: hhvm

script:
- vendor/bin/tester tests/Formatter -s -p php

after_failure:
# Print *.actual content
- 'for i in $(find tests -name \*.actual); do echo "--- $i"; cat $i; echo; echo; done'

before_script:
# Update Composer
- composer self-update

# Install Nette Tester
- composer install --no-interaction --prefer-source
23 changes: 23 additions & 0 deletions composer.json
@@ -0,0 +1,23 @@
{
"name": "minetro/formatter",
"description": "Simple string formatter for Latte",
"type": "library",
"license": ["BSD-3-Clause"],
"homepage": "https://github.com/minetro/formatter",
"authors": [
{
"name": "Milan Felix Sulc",
"homepage": "http://jfx.cz"
}
],
"require": {
"php": ">=5.4.0",
"nette/utils": ">=2.3.0,<2.4.0"
},
"require-dev": {
"nette/tester": "~1.3.0"
},
"autoload": {
"classmap": ["src/"]
}
}
31 changes: 31 additions & 0 deletions readme.md
@@ -0,0 +1,31 @@
# Events

[![Build Status](https://travis-ci.org/minetro/formatter.svg?branch=master)](https://travis-ci.org/minetro/formatter)
[![Downloads this Month](https://img.shields.io/packagist/dm/minetro/formatter.svg?style=flat)](https://packagist.org/packages/minetro/formatter)
[![Latest stable](https://img.shields.io/packagist/v/minetro/formatter.svg?style=flat)](https://packagist.org/packages/minetro/formatter)
[![HHVM Status](https://img.shields.io/hhvm/minetro/formatter.svg?style=flat)](http://hhvm.h4cc.de/package/minetro/formatter)

Simple string formatter for Latte.

## Install

```sh
$ composer require minetro/formatter:~1.0.0
```

## Usage

### Register in config

Register in your config file (e.q. config.neon).

```neon
services
formatter.number:
class: Minetro\Formatter\NumberFormatter('K猫')
nette.latteFactory:
setup:
- addFilter(money, [@formatter.number,format])
```
18 changes: 18 additions & 0 deletions src/IFormatter.php
@@ -0,0 +1,18 @@
<?php

namespace Minetro\Formatter;

/**
* Formatter Interface
*
* @author Milan Felix Sulc <sulcmil@gmail.com>
*/
interface IFormatter
{

/**
* @param mixed $value
* @return mixed
*/
function format($value);
}
202 changes: 202 additions & 0 deletions src/NumberFormatter.php
@@ -0,0 +1,202 @@
<?php

namespace Minetro\Formatter;

use InvalidArgumentException;
use Nette\Utils\Html;

/**
* Number Formatter
*
* @author Milan Felix Sulc <sulcmil@gmail.com>
*/
class NumberFormatter implements IFormatter
{
/** UTF-8 &nbsp; */
CONST NBSP = "\xc2\xa0";

/** Default mask */
CONST DEFAULT_MASK = "%d";

/** @var int|float */
private $rawValue;

/** @var string */
private $thousand = ' ';

/** @var int */
private $decimals = 2;

/** @var string */
private $point = ',';

/** @var callable */
private $callback;

/** @var bool */
private $zeros = FALSE;

/** @var bool */
private $strict = TRUE;

/** @var bool */
private $spaces = TRUE;

/** @var string */
private $prefix;

/** @var string */
private $suffix;

/** @var Html */
private $wrapper;

/**
* @param string $suffix
* @param string $prefix
*/
function __construct($suffix = NULL, $prefix = NULL)
{
$this->suffix = $suffix;
$this->prefix = $prefix;

$this->wrapper = Html::el();
}

/**
* @param int $decimals
*/
public function setDecimals($decimals)
{
$this->decimals = intval($decimals);
}

/**
* @param string $point
*/
public function setPoint($point)
{
$this->point = $point;
}

/**
* @param string $thousand
*/
public function setThousand($thousand)
{
$this->thousand = $thousand;
}

/**
* @param boolean $zeros
*/
public function setZeros($zeros)
{
$this->zeros = (bool)$zeros;
}

/**
* @param string $suffix
*/
public function setSuffix($suffix)
{
$this->suffix = $suffix;
}

/**
* @param string $prefix
*/
public function setPrefix($prefix)
{
$this->prefix = $prefix;
}

/**
* @param boolean $strict
*/
public function setStrict($strict)
{
$this->strict = (bool)$strict;
}

/**
* @param boolean $spaces
*/
public function setSpaces($spaces)
{
$this->spaces = (bool)$spaces;
}

/**
* @param callable $callback
*/
public function setCallback($callback)
{
$this->callback = $callback;
}

/**
* @return Html
*/
public function prototype()
{
return $this->wrapper;
}

/**
* @param int|float $value
* @param int $decimals
* @return mixed
*/
public function format($value, $decimals = NULL)
{
$value = trim($value);
$value = str_replace(',', '.', $value);

if (!is_numeric($value)) {
if ($this->strict) {
throw new InvalidArgumentException("Value must be numeric");
} else {
return $value;
}
}

$this->rawValue = $value;

if ($decimals == NULL) {
$decimals = $this->decimals;
}

if ($decimals < 0) {
$value = round($value, $decimals);
$decimals = 0;
}

$number = number_format((float)$value, $decimals, $this->point, $this->thousand);

if ($decimals > 0 && !$this->zeros) {
$number = rtrim(rtrim($number, '0'), $this->point);
}

if ($this->callback) {
return call_user_func_array($this->callback, [$this->prefix, $number, $this->suffix]);
} else if ($this->spaces) {
return trim(sprintf("%s %s %s", $this->prefix, $number, $this->suffix));
} else {
return trim(sprintf("%s%s%s", $this->prefix, $number, $this->suffix));
}
}

/**
* @param int|float $value
* @param int $decimals
* @return Html
*/
public function formatHtml($value, $decimals = NULL)
{
$wrapper = clone $this->wrapper;
$wrapper->setHtml($this->format($value, $decimals));
return $wrapper;
}

}

0 comments on commit a6b2af8

Please sign in to comment.