Exemplo n.º 1
0
 /**
  * Test castAs..
  *
  * @test
  * @covers \Bairwell\Hydrator\CachedProperty::setFrom
  * @covers \Bairwell\Hydrator\CachedProperty::getFrom
  */
 public function testFrom()
 {
     $from = new \Bairwell\Hydrator\Annotations\From();
     $fromTwo = new \Bairwell\Hydrator\Annotations\From();
     $sut = new CachedProperty('testClassName', 'testingName', $from);
     $this->assertSame($from, $sut->getFrom());
     $this->assertSame($sut, $sut->setFrom($fromTwo));
     $this->assertSame($fromTwo, $sut->getFrom());
 }
Exemplo n.º 2
0
 /**
  * Add a property onto our list.
  *
  * @param \Bairwell\Hydrator\CachedProperty $value The property we are adding/stacking.
  *
  * @return CachedClass
  *
  * @throws \TypeError If class of property does not match this class.
  */
 public function add(CachedProperty $value) : self
 {
     $offset = $value->getName();
     if (false === isset($this->properties[$offset])) {
         $this->properties[$offset] = [];
     }
     $this->properties[$offset][] = $value;
     return $this;
 }
Exemplo n.º 3
0
 /**
  * Hydrate a single property via single source.
  *
  * @param mixed          $currentValue Current value of the property.
  * @param array|callable $source       The source we are using.
  * @param string         $sourceName   Name of the source.
  * @param string         $fromField    Which field should we be reading from.
  * @param CachedProperty $property     The property we are working on.
  * @param FailureList    $failureList  Referenced list of failures.
  *
  * @return mixed The new value.
  * @throws \TypeError If source is not callable or array.
  */
 private function hydrateSinglePropertyViaSource($currentValue, $source, string $sourceName, string $fromField, CachedProperty $property, FailureList &$failureList)
 {
     if (true === is_array($source)) {
         $data = null;
         if (true === array_key_exists($fromField, $source)) {
             $data = $source[$fromField];
         }
     } elseif (true === is_callable($source)) {
         $data = call_user_func($source, $fromField);
     } else {
         throw new \TypeError('Source must be an array or callable: got ' . gettype($source));
     }
     $isValid = true;
     if (null === $data || true === is_array($data) && 0 === count($data)) {
         $isValid = false;
     }
     if (true === $isValid) {
         $arrayStyles = $property->getFrom()->arrayStyles;
         if (false === empty($arrayStyles)) {
             $data = $this->extractFromArray($data, $arrayStyles);
         }
         if (false === $property->hasCastAs()) {
             $currentValue = $data;
             if (null !== $this->logger) {
                 $this->logger->debug('Hydrator: No cast setting for field {fromField}: {currentValue}', ['fromField' => $fromField, 'currentValue' => $currentValue]);
             }
         } else {
             $castAs = $property->getCastAs();
             $newValue = $castAs->cast($data);
             if (true === $castAs->hasErrored()) {
                 $failure = new Failure();
                 $failure->setInputField($fromField)->setInputValue($data)->setMessage($castAs->getErrorMessage())->setTokens($castAs->getErrorTokens())->setSource($sourceName);
                 $failureList->add($failure);
                 if (null !== $this->logger) {
                     $this->logger->debug('Hydrator: Cast failed for field {fromField}: {currentValue}: {castErrorMessage}', ['fromField' => $fromField, 'currentValue' => $currentValue, 'castErrorMessage' => $castAs->getErrorMessage()]);
                 }
             } else {
                 $currentValue = $newValue;
             }
         }
         //end if
     }
     //end if
     return $currentValue;
 }