Hoa central
EncapsulationShunter.php
Go to the documentation of this file.
1 <?php
2 
38 
39 use Hoa\Praspel;
40 
51 {
59  protected $_assertionChecker = null;
60 
61 
62 
70  public function __construct(Praspel\AssertionChecker $assertionChecker = null)
71  {
72  if (null !== $assertionChecker) {
73  $this->setAssertionChecker($assertionChecker);
74  }
75 
76  return;
77  }
78 
86  public function __invoke(Handler $preambler)
87  {
88  $callable = $preambler->__getCallable();
89  $reflection = $callable->getReflection();
90  $registry = Praspel::getRegistry();
91 
92  if ($reflection instanceof \ReflectionClass) {
93  $_object = $reflection->newInstance();
94  $preambler->__setCallable(xcallable($_object, '__construct'));
95  } elseif (!($reflection instanceof \ReflectionMethod)) {
97  'The callable must be a class and a (dynamic) method name.',
98  0
99  );
100  } else {
101  $callback = $callable->getValidCallback();
102 
103  if (!is_object($callback[0])) {
104  $reflectionClass = $reflection->getDeclaringClass();
105  $_reflectionClass = $reflectionClass;
106 
107  while (
108  (null === $constructor = $_reflectionClass->getConstructor()) &&
109  (false !== $_reflectionClass = $_reflectionClass->getParentClass())
110  );
111 
112  if (null === $constructor) {
113  $_object = $reflectionClass->newInstance();
114  } else {
115  $className = $_reflectionClass->getName();
116  $id = $className . '::__construct';
117 
118  if (!isset($registry[$id])) {
119  $registry[$id] = Praspel::interpret(
121  $constructor->getDocComment()
122  ),
123  $className
124  );
125  }
126 
127  $assertionChecker = $this->getAssertionChecker();
128 
129  if (null === $assertionChecker) {
130  $assertionChecker = '\Hoa\Praspel\AssertionChecker';
131  }
132 
133  $arguments = $assertionChecker::generateData($registry[$id]);
134  $_object = $reflectionClass->newInstanceArgs($arguments);
135  }
136 
137  $preambler->__setCallable(xcallable($_object, $callback[1]));
138  }
139  }
140 
141  $reflectionObject = $preambler->__getReflectionObject($object);
142  $className = $reflectionObject->getName();
143  $properties = $reflectionObject->getProperties();
144 
145  foreach ($properties as $property) {
146  $propertyName = $property->getName();
147  $id = $className . '::$' . $propertyName;
148 
149  if (false === isset($registry[$id])) {
150  $registry[$id] = Praspel::interpret(
152  $property->getDocComment()
153  ),
154  $className
155  );
156  }
157 
158  $specification = $registry[$id];
159 
160  if (false === $specification->clauseExists('invariant')) {
161  throw new Praspel\Exception\Preambler(
162  'Cannot generate a value from %s because it has no ' .
163  '@invariant clause.',
164  1,
165  $id
166  );
167  }
168 
169  $preambler->$propertyName =
170  $specification
171  ->getClause('invariant')
172  ->getVariable($propertyName)
173  ->sample();
174  }
175 
176  return;
177  }
178 
186  public function setAssertionChecker(Praspel\AssertionChecker $assertionChecker)
187  {
189  $this->_assertionChecker = $assertionChecker;
190 
191  return $old;
192  }
193 
199  public function getAssertionChecker()
200  {
202  }
203 }
static extractFromComment($comment)
Definition: Praspel.php:99
__setCallable(Core\Consistency\Xcallable $callable)
Definition: Handler.php:164
static getRegistry()
Definition: Praspel.php:121
__construct(Praspel\AssertionChecker $assertionChecker=null)
setAssertionChecker(Praspel\AssertionChecker $assertionChecker)
static interpret($praspel, $bindToClass=null)
Definition: Praspel.php:69