| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
stubClassLoader::load('net::stubbles::websites::variantmanager::types::stubAbstractVariant'); |
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
class stubRandomVariant extends stubAbstractVariant |
|---|
| 19 |
{ |
|---|
| 20 |
|
|---|
| 21 |
* weight of the variant |
|---|
| 22 |
* |
|---|
| 23 |
* @var int |
|---|
| 24 |
*/ |
|---|
| 25 |
private $weight = 0; |
|---|
| 26 |
|
|---|
| 27 |
* the choosen sibling |
|---|
| 28 |
* |
|---|
| 29 |
* @var array<string,int> |
|---|
| 30 |
*/ |
|---|
| 31 |
public static $random = array(); |
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
* Get the weight of the variant |
|---|
| 35 |
* |
|---|
| 36 |
* @return int |
|---|
| 37 |
*/ |
|---|
| 38 |
public function getWeight() |
|---|
| 39 |
{ |
|---|
| 40 |
return $this->weight; |
|---|
| 41 |
} |
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
* Set the weight of the variant |
|---|
| 45 |
* |
|---|
| 46 |
* @param int $weight |
|---|
| 47 |
*/ |
|---|
| 48 |
public function setWeight($weight) |
|---|
| 49 |
{ |
|---|
| 50 |
$this->weight = $weight; |
|---|
| 51 |
} |
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
* check whether the variant is an enforcing variant |
|---|
| 55 |
* |
|---|
| 56 |
* @param stubSession $session access to session |
|---|
| 57 |
* @param stubRequest $request access to request parameters |
|---|
| 58 |
* @return bool |
|---|
| 59 |
*/ |
|---|
| 60 |
public function isEnforcing(stubSession $session, stubRequest $request) |
|---|
| 61 |
{ |
|---|
| 62 |
return false; |
|---|
| 63 |
} |
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
* check whether the conditions for this variant are met |
|---|
| 67 |
* |
|---|
| 68 |
* @param stubSession $session access to session |
|---|
| 69 |
* @param stubRequest $request access to request parameters |
|---|
| 70 |
* @return bool |
|---|
| 71 |
*/ |
|---|
| 72 |
public function conditionsMet(stubSession $session, stubRequest $request) |
|---|
| 73 |
{ |
|---|
| 74 |
return true; |
|---|
| 75 |
} |
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 |
* check whether the variant is valid |
|---|
| 79 |
* |
|---|
| 80 |
* @param stubSession $session access to session |
|---|
| 81 |
* @param stubRequest $request access to request parameters |
|---|
| 82 |
* @return bool |
|---|
| 83 |
*/ |
|---|
| 84 |
public function isValid(stubSession $session, stubRequest $request) |
|---|
| 85 |
{ |
|---|
| 86 |
return $this->equals(self::getRandom($this)); |
|---|
| 87 |
} |
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 |
* finds a random variant |
|---|
| 91 |
* |
|---|
| 92 |
* @param stubRandomVariant $actual |
|---|
| 93 |
* @return stubRandomVariant |
|---|
| 94 |
*/ |
|---|
| 95 |
protected static function getRandom(self $actual) |
|---|
| 96 |
{ |
|---|
| 97 |
if ($actual->hasParent() == false) { |
|---|
| 98 |
return $actual; |
|---|
| 99 |
} |
|---|
| 100 |
|
|---|
| 101 |
$parentHash = $actual->getParent()->hashCode(); |
|---|
| 102 |
if (isset(self::$random[$parentHash]) == true) { |
|---|
| 103 |
return self::$random[$parentHash]; |
|---|
| 104 |
} |
|---|
| 105 |
|
|---|
| 106 |
$siblings = $actual->findSiblings(); |
|---|
| 107 |
if (count($siblings) == 1) { |
|---|
| 108 |
self::$random[$parentHash] = $actual; |
|---|
| 109 |
return self::$random[$parentHash]; |
|---|
| 110 |
} |
|---|
| 111 |
|
|---|
| 112 |
$choice = array(); |
|---|
| 113 |
foreach ($siblings as $sibling) { |
|---|
| 114 |
for ($i = $sibling->weight; $i > 0; $i--) { |
|---|
| 115 |
$choice[] = $sibling; |
|---|
| 116 |
} |
|---|
| 117 |
} |
|---|
| 118 |
|
|---|
| 119 |
self::$random[$parentHash] = $choice[rand(0, count($choice) - 1)]; |
|---|
| 120 |
return self::$random[$parentHash]; |
|---|
| 121 |
} |
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 |
* Find the siblings of this variant |
|---|
| 125 |
* |
|---|
| 126 |
* @return array<stubRandomVariant> |
|---|
| 127 |
*/ |
|---|
| 128 |
private function findSiblings() |
|---|
| 129 |
{ |
|---|
| 130 |
$siblings = array(); |
|---|
| 131 |
$parent = $this->getParent(); |
|---|
| 132 |
if (null != $parent) { |
|---|
| 133 |
$children = $parent->getChildren(); |
|---|
| 134 |
foreach ($children as $child) { |
|---|
| 135 |
if ($child instanceof self) { |
|---|
| 136 |
$siblings[] = $child; |
|---|
| 137 |
} |
|---|
| 138 |
} |
|---|
| 139 |
} |
|---|
| 140 |
|
|---|
| 141 |
return $siblings; |
|---|
| 142 |
} |
|---|
| 143 |
} |
|---|
| 144 |
?> |
|---|