Hoa central
Public Member Functions | Protected Attributes | List of all members
Hoa\Praspel\Preambler\EncapsulationShunter Class Reference

Public Member Functions

 __construct (Praspel\AssertionChecker $assertionChecker=null)
 
 __invoke (Handler $preambler)
 
 setAssertionChecker (Praspel\AssertionChecker $assertionChecker)
 
 getAssertionChecker ()
 

Protected Attributes

 $_assertionChecker = null
 

Detailed Description

Class .

Shunt encapsulation: instanciate a class and set its state by using invariants and not methods calls.

Definition at line 50 of file EncapsulationShunter.php.

Constructor & Destructor Documentation

Hoa\Praspel\Preambler\EncapsulationShunter::__construct ( Praspel\AssertionChecker  $assertionChecker = null)

Constructor.

Parameters
\Hoa\Praspel\AssertionChecker$assertionCheckerAssertion checker.
Returns
void

Definition at line 70 of file EncapsulationShunter.php.

71  {
72  if (null !== $assertionChecker) {
73  $this->setAssertionChecker($assertionChecker);
74  }
75 
76  return;
77  }
setAssertionChecker(Praspel\AssertionChecker $assertionChecker)

Here is the call graph for this function:

Member Function Documentation

Hoa\Praspel\Preambler\EncapsulationShunter::__invoke ( Handler  $preambler)

Invoke the encapsulation shunter.

Parameters
\Hoa\Praspel\Preambler\Handler$preamblerPreambler.
Returns
void
Exceptions

Definition at line 86 of file EncapsulationShunter.php.

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)) {
96  throw new Praspel\Exception\Preambler(
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  }
static extractFromComment($comment)
Definition: Praspel.php:99
static getRegistry()
Definition: Praspel.php:121
static interpret($praspel, $bindToClass=null)
Definition: Praspel.php:69

Here is the call graph for this function:

Hoa\Praspel\Preambler\EncapsulationShunter::getAssertionChecker ( )

Get the assertion checker.

Returns

Definition at line 199 of file EncapsulationShunter.php.

Here is the caller graph for this function:

Hoa\Praspel\Preambler\EncapsulationShunter::setAssertionChecker ( Praspel\AssertionChecker  $assertionChecker)

Set an assertion checker.

Parameters
\Hoa\Praspel\AssertionChecker$assertionCheckerAssertion checker.
Returns

Definition at line 186 of file EncapsulationShunter.php.

187  {
189  $this->_assertionChecker = $assertionChecker;
190 
191  return $old;
192  }

Here is the caller graph for this function:

Member Data Documentation

Hoa\Praspel\Preambler\EncapsulationShunter::$_assertionChecker = null
protected

Definition at line 59 of file EncapsulationShunter.php.


The documentation for this class was generated from the following file: