Ejemplo n.º 1
0
 public function testHydrate()
 {
     $dest = new stdClass();
     $dest->nullify = 'Please';
     $source = new PropertyClass();
     $source->name = 'Test';
     $source->age = 19;
     $source->nullify = null;
     Objects::hydrate($dest, $source, [null]);
     $this->assertEquals('Please', $dest->nullify);
     Objects::hydrate($dest, $source, ['nullify'], false);
     $this->assertEquals('Please', $dest->nullify);
     Objects::hydrate($dest, $source, ['nullify'], true);
     $this->assertNull($dest->nullify);
     Objects::hydrate($dest, $source, ['name']);
     $this->assertObjectHasAttribute('name', $dest);
     $this->assertEquals('Test', $dest->name);
     $this->assertObjectNotHasAttribute('age', $dest);
     Objects::hydrate($dest, $source, ['age']);
     $this->assertObjectHasAttribute('age', $dest);
     $this->assertEquals('19', $dest->age);
     $dest->name = null;
     Objects::hydrate($dest, $source);
     $this->assertObjectHasAttribute('name', $dest);
     $this->assertEquals('Test', $dest->name);
     $this->setExpectedException("Exception");
     Objects::hydrate(['' => ''], $source, []);
 }
Ejemplo n.º 2
0
 /**
  * @param ApiCallData $data
  *
  * @return ApiResponseInterface
  *
  * @throws \Exception
  */
 public static function create(ApiCallData $data)
 {
     $type = $data->getResponseType();
     if (!class_exists($type)) {
         throw new \Exception("Type Class '" . $type . "', could not be loaded");
     }
     $interfaces = class_implements($type);
     if ($type == '\\Exception' || $type === '\\Packaged\\Api\\Exceptions\\ApiException' || in_array('\\Packaged\\Api\\Exceptions\\ApiException', $interfaces) || array_key_exists('Exception', class_parents($type))) {
         $code = $data->getStatusCode();
         if (!is_numeric($code)) {
             $code = 500;
         }
         $exception = new $type($data->getStatusMessage(), $code);
         $rawData = $data->getRawResult();
         if (is_object($rawData)) {
             Objects::hydrate($exception, $rawData);
         }
         throw $exception;
     } else {
         if (in_array('Packaged\\Api\\Interfaces\\ApiResponseInterface', $interfaces)) {
             $class = new $type();
             /**
              * @var $class \Packaged\Api\Interfaces\ApiResponseInterface
              */
             $class->setApiCallData($data);
             return $class;
         } else {
             throw new ApiException("An invalid message type was used '" . $type . "'");
         }
     }
 }
Ejemplo n.º 3
0
 /**
  * Hydrate properties from the source object, into the destination
  *
  * @param object $destination object to write data to
  * @param object $source      object to read data from
  * @param array  $properties  properties to read
  * @param bool   $copyNull    Copy null values from source to destination
  *
  * @return void
  *
  * @throws Exception
  *
  * @deprecated
  */
 function hydrate($destination, $source, array $properties, $copyNull = true)
 {
     \Packaged\Helpers\Objects::hydrate($destination, $source, $properties, $copyNull);
 }
 /**
  * Hydrate the public properties
  *
  * @param $data
  *
  * @return $this
  * @throws \Exception
  */
 public function hydrate($data)
 {
     Objects::hydrate($this, ValueAs::obj($data), Objects::properties($this), false);
     return $this;
 }