Download the PHP package markhj/php-tendency without Composer
On this page you can find all versions of the php package markhj/php-tendency. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download markhj/php-tendency
More information about markhj/php-tendency
Files in markhj/php-tendency
Package php-tendency
Short Description A tool to make complex randomization scenarios readable and modular.
License MIT
Informations about the package php-tendency
PHP Tendency is a random value generator. But it's not the one you'd pick to find lottery numbers, rather it's the one you'd choose to calculate complex scenarios, where tens or hundreds of factors impact the likelihood of an outcome.
💫 Sounds cool, tell me more
PHP Tendency had its beginning as a "spin-off" from a political simulation game, in which the choices of people, countries and companies needed a degree of randomness, but never total randomness.
The risk that a person commits a crime isn't 50/50. It's determined by personality, criminal history, how the person was raised, life circumstances, social circles, and so forth.
Implementing such complex determination in code isn't easy, and without the proper tooling, it's outright cumbersome: Hard to test, hard to predict, and hard to keep in check.
This is the exact problem PHP Tendency set out to solve. Let's imagine a function which calculates the risk of someone committing a crime. For a prototype, most would probably do something like:
``
But... What if that same thing could look like this?
``
Much better, right? This is easier to test, easier to maintain, and easier to read.
"But where do all these methods like hasLow
and hasCriminalRecord
come from?" What a great question! They come from extensions. Think of them
as custom modules you write specifically for your project.
We'll get back to that later in this guide. But they basically provide reusability,
modularity, improved overview, and testability.
🍃 Features
- Extensible and modular: Create separate classes which are injected into a randomizer per an on-demand basis.
- Biased outcomes: Make certain outcomes more likely than others using a weight-based randomization (mean + standard deviation).
- Several types: Int, float or bool? Doesn't matter. You can also build your own custom randomizer which takes strings, arrays, or specific classes. The sky is the limit.
📌 Prerequisites
- PHP 8.3 or higher
📦 Installation
The library is easily installed with Composer:
`
🚀 Getting started
Let's have a look at the fundamental usage of PHP Tendency. Take note that the really fun stuff comes in the Extensions chapter.
There are a set of base classes called "randomizers".
Three are bundled out-of-the-box with PHP Tendency, but you can
easily build more on your own, simply by extending RandomBase
.
Boolean
To retrieve a random boolean value:
``
`
Float
Retrieve a random floating-point value between a min and max.
``
`bash composer test
### Linting
Linting is carried out with **Laravel Pint**.
Point your IDE to use the file ``pint.json``.