PHPackages                             swindon/code-generator - 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. swindon/code-generator

ActiveLibrary

swindon/code-generator
======================

Random code generator

v1.0.2(4y ago)149MITPHPPHP &gt;=7

Since Feb 18Pushed 4y ago1 watchersCompare

[ Source](https://github.com/swindon/code-generator)[ Packagist](https://packagist.org/packages/swindon/code-generator)[ RSS](/packages/swindon-code-generator/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (3)DependenciesVersions (3)Used By (0)

Random Code Generator
=====================

[](#random-code-generator)

[![Buy me a coffee](https://camo.githubusercontent.com/15a6ed52d752fef0b554810fd58b4334eb79f4ea662abf5b5dacb711c7fe6e85/68747470733a2f2f657863656c6f6666746865677269642e636f6d2f77702d636f6e74656e742f75706c6f6164732f323032302f30322f626d632d627574746f6e2e706e67)](https://www.buymeacoffee.com/scottwindon)

Install:
--------

[](#install)

`composer require swindon/code-generator`

Usage:
------

[](#usage)

### Single Code Generation:

[](#single-code-generation)

```
/**
 * Generates a random code
 *
 * @param   $length             int
 * @return string
 */
->generate(int $length = 8) : string
```

**Example #1**Generates 12 character long string

```
(new CodeGenerator)->generate(12);
// output: 4CF9O7XP
```

**Example #2**Generates 8 character long string, with alpha characters only (lower and upper case)

```
(new CodeGenerator)->setCharacters(CodeGenerator::CHAR_ALPHA)->generate(8);
// output: JTFRujQJ
```

**Example #3**Generates 10 character long string, with all characters and symbols, removing ambiguous characters (eg S, 5, 0, O etc.)

```
(new CodeGenerator)->setCharacters(CodeGenerator::CHAR_ALL)->generate(10);
// output: J\TTu@setCharacters('ABCXYZabcxyz')->generate(6);
// output: XcZxbc
```

---

### Batch Code Generation:

[](#batch-code-generation)

```
/**
 * Generates an array of random codes
 *
 * @param   $maxNum             int
 * @param   $length             int
 * @return  array
 */
->bulk(int $maxNum, int $length = 8) : array
```

**Example #1**Generates array of 6 x 8 character long unique codes

```
(new CodeGenerator)->bulk(6)
// output: [
//   "d3MEJqNq",
//   "Dr4mYKxP",
//   "J0RqiCJ9",
//   "NZuaPUVC",
//   "aZgMjn2m",
//   "EnyQKrat",
// ];
```

**Example #2**Generates array of 6 x 6 character long unique codes appended to original array of codes

```
(new CodeGenerator)->bulk(3, 6)
// output: [
//   "OsphQo",
//   "LPKCs2",
//   "DCv0QS",
// ];
```

**Example #3**Generates array of 6 x 8 character long unique codes, using only characters stated

```
(new CodeGenerator)->setCharacters('ABCXYZabcxyz')->bulk(6)
// output: [
//   "xcXAcYbb",
//   "YZXAYBBX",
//   "ZZzYxZyy",
//   "ZYZacACa",
//   "BYzzBYaB",
//   "xZxbacAz",
// ];
```

---

Exceptions
----------

[](#exceptions)

### CodeGeneratorException

[](#codegeneratorexception)

The CodeGeneratorException can be thrown for multiple reasons, on main reason for this will be from the bulk generation method. If the amount to codes required to be generated exceeds the amount which can be generated (based on character set and code length) then an exception will be thrown.

**Example**Try generating array of 100 x 3 character long unique codes, using only characters stated

```
(new CodeGenerator)->setCharacters('ABC')->bulk(100, 3);
// throws: 'Cannot generate more than 27 possible unique codes. Try increasing the code length.'
```

---

### Show some support!

[](#show-some-support)

[![Buy me a coffee](https://camo.githubusercontent.com/15a6ed52d752fef0b554810fd58b4334eb79f4ea662abf5b5dacb711c7fe6e85/68747470733a2f2f657863656c6f6666746865677269642e636f6d2f77702d636f6e74656e742f75706c6f6164732f323032302f30322f626d632d627574746f6e2e706e67)](https://www.buymeacoffee.com/scottwindon)

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity50

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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 ~1 days

Total

2

Last Release

1548d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/cde4f0894b7aa82287f3412bf60f70af473f73fd1e8e1a0a561453307e5f5deb?d=identicon)[Swindon](/maintainers/Swindon)

---

Top Contributors

[![swindon](https://avatars.githubusercontent.com/u/7947471?v=4)](https://github.com/swindon "swindon (8 commits)")

### Embed Badge

![Health badge](/badges/swindon-code-generator/health.svg)

```
[![Health](https://phpackages.com/badges/swindon-code-generator/health.svg)](https://phpackages.com/packages/swindon-code-generator)
```

PHPackages © 2026

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