Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
franatieu committed Jul 18, 2016
0 parents commit f571c3e
Show file tree
Hide file tree
Showing 9 changed files with 269 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.DS_Store
vendor
docs
tmp/
composer.lock
1 change: 1 addition & 0 deletions .styleci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
preset: recommended
21 changes: 21 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
language: php

php:
- '5.6'
- '7.0'

before_script:
- travis_retry composer self-update
- travis_retry composer install --no-interaction --prefer-source --dev

script:
- vendor/bin/phpunit --coverage-clover=coverage.xml

after_script:
- php vendor/bin/codacycoverage clover coverage.xml

after_success:
- bash <(curl -s https://codecov.io/bash)

env:
- CODACY_PROJECT_TOKEN=b6406e77270549e6acb96c4d1a634c69
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

Copyright (C) Anekdotes Communication inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
35 changes: 35 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "anekdotes/formater",
"type": "library",
"description": "Utility to format whole Input using provided rules",
"keywords": [
"mail",
"mailer",
"adapter"
],
"license": "MIT",
"authors": [
{
"name": "Mathieu Gosselin",
"email": "mathieu.gosselin@anekdotes.com",
"homepage": "http://www.anekdotes.com"
}
],
"require": {
"php": ">=5.6.0",
"anekdotes/support": "<2.0.0"
},
"require-dev": {
"phpunit/phpunit": "~5.4"
},
"autoload": {
"classmap": [
"src/"
]
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
}
}
26 changes: 26 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
>

<filter>
<whitelist>
<directory>src/anekdotes</directory>
</whitelist>
</filter>

<testsuites>
<testsuite name="Formatter Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>

</phpunit>
17 changes: 17 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Anekdotes Formatter

[![Latest Stable Version](https://poser.pugx.org/anekdotes/formatter/v/stable)](https://packagist.org/packages/anekdotes/formatter)
[![Build Status](https://travis-ci.org/anekdotes/formatter.svg?branch=master)](https://travis-ci.org/anekdotes/formatter)
[![codecov.io](https://codecov.io/github/anekdotes/formatter/coverage.svg)](https://codecov.io/github/anekdotes/formatter?branch=master)
[![StyleCI](https://styleci.io/repos/62647499/shield?style=flat)](https://styleci.io/repos/62647499)
[![License](https://poser.pugx.org/anekdotes/formatter/license)](https://packagist.org/packages/anekdotes/formatter)
[![Total Downloads](https://poser.pugx.org/anekdotes/formatter/downloads)](https://packagist.org/packages/anekdotes/formatter)
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/50134febcefe4cc78daf07ca45969728)](https://www.codacy.com/app/Grasseh/formatter?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=anekdotes/formatter&amp;utm_campaign=Badge_Grade)

Utility to format whole Input using provided rules

## Installation

Install via composer into your project:

composer require anekdotes/formatter
144 changes: 144 additions & 0 deletions src/anekdotes/Formater.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
<?php

namespace Anekdotes\Formater;

use Anekdotes\Support\Str;

/**
* Formater class, format whole Input using provided rules
*/
class Formater
{

/**
* Provided Input. Uses a key->value format
* @var string[]
*/
private $items;
/**
* Formatting Rules to be used. The key represents the key of the Input to format. The value associated to said key is a list of strings representing which rules to use to format the input's value.
* @var array[]
*/
private $rules;

/**
* Generates an instance of a Formater, using the provided items and rules
* @param string[] $items Provided Input, contains key->values pair to be formatted
* @param array[] $rules Formatting Rules to be used. The key represents the key of the Input to format. The value associated to said key is a list of strings representing which rules to use to format the input's value.
* @return Formater Generated Formated instance
*/
public static function make($items, $rules) {
$Formater = new Formater();
$Formater->items = $items;
$Formater->rules = $rules;
return $Formater;
}

/**
* Format everything in Formater according to rules
*
* @return string[] The input given on formatter creation, now formatted
*/
public function format() {
$mergedParams = array();
foreach ($this->rules as $itemName => $ruleNames) {
foreach ($ruleNames as $rule) {
$ruleParams = explode(":", $rule);
$rule = $ruleParams[0];
array_splice($ruleParams, 0, 1);
if (array_key_exists($itemName, $this->items)) {
$mergedParams[] = $this->items[$itemName];
if (count($ruleParams) > 0) {
$ruleParams = explode(",", $ruleParams[0]);
$mergedParams = array_merge($mergedParams, $ruleParams);
}
$this->items[$itemName] = call_user_func_array(array($this, $rule), $mergedParams);
}
else {
// item doesn't exist
}
array_splice($mergedParams, 0, count($mergedParams));
}
}
return $this->items;
}


/**
* Format the postal code value into the following format : J4R 2L6
* @param string $value The input string to format
* @return string The formatted value
*/
public static function postalCode($value) {
if (strlen($value) > 0) {
$value = str_replace(" ", "", $value);
$value = substr($value, 0, 3)." ".substr($value, 3, 3);
$value = Str::upper($value);
}
return $value;
}

/**
* Format the phone number value into the following format : (450) 748-2822
* @param string $value The input string to format
* @return string The formatted value
*/
public static function phoneNumber($value) {
if (strlen($value) > 1) {
$value = preg_replace("/[^\d]+/", "", $value);
if (strlen($value) === 12) {
$value = substr($value, 0, 3)." (".substr($value, 3, 3).") ".substr($value, 6, 3)."-".substr($value, 9, 3);
} else if (strlen($value) === 11) {
$value = substr($value, 0, 1)." (".substr($value, 1, 3).") ".substr($value, 4, 3)."-".substr($value, 7, 4);
} else if (strlen($value) === 10) {
$value = "(".substr($value, 0, 3).") ".substr($value, 3, 3)."-".substr($value, 6, 4);
} else if (strlen($value) === 7) {
$value = substr($value, 0, 3)."-".substr($value, 3, 4);
}
}
return $value;
}

/**
* Format the value into a floating point number : "122.2ABD" -> 122.2
*
* @param string $value The input string to format
* @return string The formatted value
*/
public static function float($value) {
return floatval($value);
}

/**
* Format the value into an integer : "122.2ABD" -> 122
*
* @param string $value The input string to format
* @return string The formatted value
*/
public static function int($value) {
return intval($value);
}

/**
* Format the value into an integer : "122.2ABD" -> 122
*
* @param string $value The input string to format
* @return string The formatted value
*/
public static function integer($value) {
return static::int($value);
}

/**
* Format the received value into a datetime string. Currently only checks if the value is empty.
*
* @todo Format the actual string depending on the received object.
*
* @param string $value The input string to format
* @return string The formatted value
*/
public static function datetime($value) {
return $value == '' ? null : $value;
}

}
Empty file added tests/.gitkeep
Empty file.

0 comments on commit f571c3e

Please sign in to comment.