root/trunk/src/main/php/net/stubbles/websites/variantmanager/types/stubRandomVariant.php

Revision 1452, 3.6 kB (checked in by mikey, 3 months ago)

removed toString() implementations, should used basic one

Line 
1 <?php
2 /**
3  * A variant chosen randomly based on the weight defined in the configuration.
4  *
5  * @author      Niels Schelbach <niels@schlund.de>
6  * @author      Stephan Schmidt <stephan.schmidt@schlund.de>
7  * @author      Frank Kleine <mikey@stubbles.net>
8  * @package     stubbles
9  * @subpackage  websites_variantmanager_types
10  */
11 stubClassLoader::load('net::stubbles::websites::variantmanager::types::stubAbstractVariant');
12 /**
13  * A variant chosen randomly based on the weight defined in the configuration.
14  *
15  * @package     stubbles
16  * @subpackage  websites_variantmanager_types
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 ?>
Note: See TracBrowser for help on using the browser.