PHP code example of markhj / php-tendency

1. Go to this page and download the library: Download markhj/php-tendency library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

markhj / php-tendency example snippets


class Person
{
    public function shouldCommitCrime(): bool
    {
        $risk = 0;
        if ($this->personality->integrity < 0.3) {
            $risk += 0.1;
        }
        if ($this->criminalHistory->count() > 0) {
            $risk += 0.15;
        }
        // ... And hundreds more conditions
        return $risk > 0.5;
    }
}

public function shouldCommitCrime(): bool
{
    return $this->randomizer()
        ->hasLow(Personality::Integrity)
        ->hasCriminalRecord()
        // And so forth
        ->compute()
        ->result;
}

use Markhj\PhpTendency\RandomBool;

(new RandomBool())->compute();

use Markhj\PhpTendency\RandomInt;

(new RandomInt(15, 35))->compute();

use Markhj\PhpTendency\RandomInt;

(new RandomInt(-25.0, 25.0))->compute();

(new RandomBool())->changeMean(-0.25);  // More likely to be false
(new RandomBool())->changeMean(0.25);   // More like to be true
(new RandomInt(0, 100))->changeMean(0.25);  // More like to land around 75

(new RandomFloat(10.0, 25.0, 0.25))->compute();

class SimpleExtension implements Extension
{
    #[Expose]
    public function myFunc(Extendable $random, float $change): Extendable
    {
        return $random->changeMean($change);
    }
}

$randomizer = new RandomFloat(50.0, 75.0);
$randomizer->extend(new SimpleExtension());

$randomizer->myFunc(0.3);

class PersonTendency implements Extension
{
    public function __construct(
        private Person $person,
    ) {
    }

    #[Expose]
    public function hasCriminalRecord(Extendable $randomizer): Extendable
    {
        $records = getCriminalRecordFromDatabase($this->person);
        
        // Increase the likelihood 10% (+0.1) per record 
        $randomizer->changeMean(count($records) / 10);
        
        // You could even look at the severity of the records, and other stuff
        
        return $randomizer;
    }
}

$shouldCommitCrime = (new RandomBool())
    ->extend(new PersonTendency($somePerson))
    ->hasCriminalRecord()
    ->compute();
`bash
composer