PHPackages                             sanchobbdo/codes - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. sanchobbdo/codes

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

sanchobbdo/codes
================

Promotional codes generator and validator.

1.0.3(13y ago)8731[1 issues](https://github.com/sanchobbdo/codes/issues)MITPHPPHP &gt;=5.3.2

Since May 2Pushed 13y ago3 watchersCompare

[ Source](https://github.com/sanchobbdo/codes)[ Packagist](https://packagist.org/packages/sanchobbdo/codes)[ Docs](https://github.com/sanchobbdo/codes)[ RSS](/packages/sanchobbdo-codes/feed)WikiDiscussions master Synced 1w ago

READMEChangelogDependencies (6)Versions (5)Used By (0)

Codes [![Build Status](https://camo.githubusercontent.com/4138d6296e35bb43c868cbf628a09806ca92de4f04e7f5340bfc3ffae6a1ae0c/68747470733a2f2f7472617669732d63692e6f72672f73616e63686f6262646f2f636f6465732e706e67)](https://travis-ci.org/sanchobbdo/codes)
=========================================================================================================================================================================================================================================================

[](#codes-)

A PHP code generator and validator. Can be use for promotions, sweepstakes, coupons or any other application which needs to validate codes provided by users.

Installing
----------

[](#installing)

Create a `composer.json` file in the project root:

```
{
    "require": {
        "sanchobbdo/codes": "~1.0"
    }
}
```

Then download `composer.phar` and run the install command:

```
curl -s http://getcomposer.org/installer | php && ./composer.phar install
```

Initialize
----------

[](#initialize)

```
// Include composer's autoload
require 'vendor/autoload.php';

// Include codes builder
use SanchoBBDO\Codes\CodesBuilder;

// Create a Codes instance using the CodesBuilder.
$codes = CodesBuilder::buildCodes(array(
    'offset'     => 100,         // Start from
    'limit'      => 1000,        // How many codes to generate
    'coder'      => array(       // Coder settings
        'secret_key' => 'secret-key',    // Coder secret key
        'key_length' => 4,               // Code's key length
        'mac_length' => 6,               // Code's mac length
        'algo'       => 'sha1'           // Hash hmac algorithm
    )
));
```

The default coder generates codes composed of a **key** and a **mac**; the **key** identifies codes while the **mac** is used to validate them. The `mac_length` and `key_length` determine how long the code will be.

The **mac** is generated using the [hash\_hmac](http://php.net/manual/en/function.hash-hmac.php) php function. The algorithm used is determined by `algo`. To see available algorithms check the [hash\_algos](http://www.php.net/manual/en/function.hash-algos.php) php function.

Validating codes
----------------

[](#validating-codes)

```
// Validate the given code using the coder
if ($codes->getCoder()->isValid($_POST['code'])) {
    // Do something on success
} else {
    // Do something on failure
}
```

Generating codes
----------------

[](#generating-codes)

From the command line:

Install [symfony/console](http://symfony.com/doc/current/components/console/index.html), [symfony/yaml](http://symfony.com/doc/current/components/yaml/introduction.html) and [sonata-project/exporter](https://github.com/sonata-project/exporter) packages using composer:

```
composer require symfony/console:2.2.* \
                 symfony/yaml:2.2.* \
                 sonata-project/exporter:1.2.*
```

Create a config file somewhere in your project:

```
# /path/to/your/project/codes.yml

offset: 0
limit: 1000
coder:
    secret_key: your-secret-key
    key_length: 4
    mac_length: 6
```

From your project root:

```
# Dump to csv
./vendor/bin/codes dump:csv file.csv your/config/file.yml

# Dump to xls
./vendor/bin/codes dump:xls file.xls your/config/file.yml
```

---

From code:

Here is an example using the [sonata-project/exporter](https://github.com/sonata-project/exporter) (you can roll you're own implementation):

```
// On top of your file include required classes
use Exporter\Handler;
use Exporter\Writer\CsvWriter;
use SanchoBBDO\Codes\CodesSource;
```

```
$codesSource = new CodesSource($codes);
$writer = new CsvWriter('your-file.csv');
Handler::create($codesSource, $writer)->export();
```

Disclaimer
----------

[](#disclaimer)

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.

License
-------

[](#license)

Licensed under the [MIT license](http://opensource.org/licenses/MIT).

###  Health Score

29

—

LowBetter than 57% of packages

Maintenance18

Infrequent updates — may be unmaintained

Popularity15

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 99.5% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~16 days

Total

4

Last Release

4797d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/5f7a850aaeadee659847754518d8a885eb95c03d9a2694b0526e9e42d6b7911f?d=identicon)[camagu](/maintainers/camagu)

---

Top Contributors

[![camagu](https://avatars.githubusercontent.com/u/1318482?v=4)](https://github.com/camagu "camagu (218 commits)")[![adrianengine](https://avatars.githubusercontent.com/u/1206844?v=4)](https://github.com/adrianengine "adrianengine (1 commits)")

---

Tags

code generatorcouponspromotionsweepstakecode validator

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/sanchobbdo-codes/health.svg)

```
[![Health](https://phpackages.com/badges/sanchobbdo-codes/health.svg)](https://phpackages.com/packages/sanchobbdo-codes)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
