예제 #1
0
 /**
  * Parse a property.
  *
  * @param \ReflectionProperty $property     The reflected property.
  * @param CachedClass         $cachedClass  Object we are building which contains all the data about this class.
  * @param Sources             $sources      Configured sources.
  * @param Conditionals        $conditionals Configured conditionals.
  *
  * @return CachedClass
  *
  * @throws AnnotationException If there is something wrong with the annotations.
  */
 protected function parseProperty(\ReflectionProperty $property, CachedClass $cachedClass, Sources $sources, Conditionals $conditionals) : CachedClass
 {
     $propertyName = $property->getName();
     $declaringClassName = $property->getDeclaringClass()->getName();
     $modifiers = $property->getModifiers();
     $castAs = null;
     $froms = [];
     $annotations = $this->getAnnotationReader()->getPropertyAnnotations($property);
     foreach ($annotations as $anno) {
         if (true === $anno instanceof AsBase) {
             if (null !== $castAs) {
                 throw new AnnotationException('A property can only have zero or one Cast options - ' . $declaringClassName . '::$' . $propertyName . ' has multiple');
             }
             // @todo add validation of cast (especially important for arrays with nested casts)
             $castAs = $anno;
         } elseif (true === $anno instanceof From) {
             /* @var From $anno */
             $anno->sources = $this->validateSources($anno->sources, $declaringClassName . '::$' . $propertyName, $sources);
             $anno->conditions = $this->validateConditions($anno->conditions, $declaringClassName . '::$' . $propertyName, $conditionals);
             $anno->arrayStyles = $this->validateArrayStyles($anno->arrayStyles, $declaringClassName . '::$' . $propertyName);
             if (true === empty($anno->field)) {
                 $anno->field = $propertyName;
             }
             $froms[] = $anno;
         }
         //end if
     }
     //end foreach
     if (false === empty($froms)) {
         /* @var From $from */
         foreach ($froms as $from) {
             $newProperty = new CachedProperty($declaringClassName, $propertyName, $from, $modifiers, $castAs);
             $cachedClass->add($newProperty);
         }
     }
     return $cachedClass;
 }
예제 #2
0
 /**
  * Tests the offsets.
  *
  * @test
  * @uses \Bairwell\Hydrator\CachedClass::__construct
  * @uses \Bairwell\Hydrator\CachedClass::getName
  * @uses \Bairwell\Hydrator\CachedClass::add
  * @covers \Bairwell\Hydrator\CachedClass::offsetSet
  * @uses \Bairwell\Hydrator\CachedClass::count
  * @covers \Bairwell\Hydrator\CachedClass::current
  * @covers \Bairwell\Hydrator\CachedClass::next
  * @covers \Bairwell\Hydrator\CachedClass::key
  * @covers \Bairwell\Hydrator\CachedClass::valid
  * @covers \Bairwell\Hydrator\CachedClass::rewind
  */
 public function testCachedClassIterator()
 {
     $sut = new CachedClass('test3');
     $this->assertEquals('test3', $sut->getName());
     $from = new From();
     $storedProperty = $first = new CachedProperty('test3', 'abc', $from);
     $second = new CachedProperty('test3', 'abc', $from);
     $third = new CachedProperty('test3', 'def', $from);
     $fourth = new CachedProperty('test3', 'def', $from);
     $sut->add($first);
     $sut->add($second);
     $sut->add($third);
     $sut->add($fourth);
     $this->assertCount(2, $sut);
     $iterations = 0;
     foreach ($sut as $k => $v) {
         if ('abc' === $k) {
             $this->assertEquals([$first, $second], $v);
             $iterations++;
         } elseif ('def' === $k) {
             $this->assertEquals([$third, $fourth], $v);
             $iterations++;
         } else {
             $this->fail('Unexpected key:' . $k);
         }
     }
     $this->assertEquals(2, $iterations);
 }