Hoa central
RealdomString.php
Go to the documentation of this file.
1 <?php
2 
37 namespace Hoa\Realdom;
38 
39 use Hoa\Math;
40 use Hoa\Ustring;
41 
51  extends Realdom
52  implements IRealdom\Nonconvex,
54 {
60  const NAME = 'string';
61 
67  protected $_arguments = [
68  'Constinteger|Conststring codepointMin' => 0x20,
69  'Constinteger|Conststring codepointMax' => 0x7e,
70  'Integer length'
71  ];
72 
78  protected $_discredited = [];
79 
80 
81 
87  protected function construct()
88  {
89  if ($this['codepointMin'] instanceof Conststring) {
90  $char = mb_substr($this['codepointMin']->getConstantValue(), 0, 1);
91  $this['codepointMin'] = new Constinteger(Ustring::toCode($char));
92  }
93 
94  if ($this['codepointMax'] instanceof Conststring) {
95  $char = mb_substr($this['codepointMax']->getConstantValue(), 0, 1);
96  $this['codepointMax'] = new Constinteger(Ustring::toCode($char));
97  }
98 
99  return;
100  }
101 
108  protected function _predicate($q)
109  {
110  if (!is_string($q)) {
111  return false;
112  }
113 
114  $length = mb_strlen($q);
115 
116  if (false === $this['length']->predicate($length)) {
117  return false;
118  }
119 
120  if (0 === $length) {
121  return true;
122  }
123 
124  $split = preg_split('#(?<!^)(?!$)#u', $q);
125  $out = true;
126  $handle = 0;
127  $min = $this['codepointMin']->getConstantValue();
128  $max = $this['codepointMax']->getConstantValue();
129 
130  foreach ($split as $letter) {
131  $handle = Ustring::toCode($letter);
132  $out = $out && ($min <= $handle) && ($handle <= $max);
133  }
134 
135  return $out;
136  }
137 
144  protected function _sample(Math\Sampler $sampler)
145  {
146  $string = null;
147  $min = $this['codepointMin']->getConstantValue();
148  $max = $this['codepointMax']->getConstantValue();
149  $length = $this['length']->sample($sampler);
150 
151  if (0 > $length) {
152  return false;
153  }
154 
155  for ($i = 0; $i < $length; ++$i) {
156  $string .= Ustring::fromCode($sampler->getInteger(
157  $min,
158  $max,
159  $this->_discredited
160  ));
161  }
162 
163  return $string;
164  }
165 
172  public function discredit($value)
173  {
174  $_value = Ustring::toCode($value);
175 
176  if (true === in_array($_value, $this->_discredited) ||
177  false === $this->predicate($value)) {
178  return $this;
179  }
180 
181  $this->_discredited[] = $_value;
182 
183  return $this;
184  }
185 
191  public function getSize()
192  {
193 
194  // @TODO : this is only for length=1.
195  return
196  $this['codepointMax']->getConstantValue() -
197  $this['codepointMin']->getConstantValue() -
198  count($this->_discredited) + 1;
199  }
200 }
static toCode($char)
Definition: Ustring.php:943
static fromCode($code)
Definition: Ustring.php:928
_sample(Math\Sampler $sampler)