forked from RY-Cafemedia/feature
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
168 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PabloJoan\Feature\Bucketing\Calculator; | ||
|
||
class Id | ||
{ | ||
/** | ||
* Map a hex value to the half-open interval between 0 and 1 while | ||
* preserving uniformity of the input distribution. | ||
*/ | ||
function number (string $idToHash) : float | ||
{ | ||
$hash = hash('haval192,3', $idToHash); | ||
$x = 0; | ||
for ($i = 0; $i < 47; ++$i) { | ||
$x = ($x * 2) + (hexdec($hash[$i]) < 8 ? 0 : 1); | ||
} | ||
|
||
$x = $x / 140737488355328; // ( 2 ** 47 ) is the max value of $x | ||
|
||
return $x * 100; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PabloJoan\Feature\Bucketing\Calculator; | ||
|
||
class Random | ||
{ | ||
function number () : float | ||
{ | ||
$x = random_int(0, PHP_INT_MAX - 1) / PHP_INT_MAX; | ||
return $x * 100; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PabloJoan\Feature\Bucketing; | ||
|
||
use PabloJoan\Feature\Value\User; | ||
use PabloJoan\Feature\Bucketing\Calculator\Random as Calculator; | ||
|
||
class Random implements Type | ||
{ | ||
function id (User $user) : string | ||
{ | ||
return $user->uaid() ?: 'no uaid'; | ||
} | ||
|
||
function number (string $idToHash) : float | ||
{ | ||
return (new Calculator)->number(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PabloJoan\Feature\Bucketing; | ||
|
||
use PabloJoan\Feature\Value\User; | ||
|
||
interface Type | ||
{ | ||
function id (User $user) : string; | ||
|
||
/** | ||
* A random-ish number between 0 and 100 based on the feature name and $id | ||
* unless we are bucketing completely at random | ||
*/ | ||
function number (string $idToHash) : float; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PabloJoan\Feature\Bucketing; | ||
|
||
use PabloJoan\Feature\Value\User; | ||
use PabloJoan\Feature\Bucketing\Calculator\Id as Calculator; | ||
|
||
class Uaid implements Type | ||
{ | ||
function id (User $user) : string | ||
{ | ||
return $user->uaid(); | ||
} | ||
|
||
function number (string $idToHash) : float | ||
{ | ||
return (new Calculator)->number($idToHash); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PabloJoan\Feature\Bucketing; | ||
|
||
use PabloJoan\Feature\Value\User as UserValue; | ||
use PabloJoan\Feature\Bucketing\Calculator\Id as Calculator; | ||
|
||
class User implements Type | ||
{ | ||
function id (UserValue $user) : string | ||
{ | ||
return $user->id(); | ||
} | ||
|
||
function number (string $idToHash) : float | ||
{ | ||
return (new Calculator)->number($idToHash); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.