Beispiel #1
0
 public function toArray(HydratableInterface &$obj, $map_id = null, $hints_only = false)
 {
     $this->assertHydratable($obj);
     $hints = $obj->__toArray($map_id) ?: [];
     if (!$hints_only) {
         $properties = [];
         foreach (get_class_methods($obj) as $method) {
             if ($propertyName = $this->findPropertyName($method, ['get', 'is'])) {
                 $properties[$propertyName] = $propertyName;
             }
         }
         $hints = array_merge($properties, $hints);
     }
     $array = [];
     foreach ($hints as $p_name => $p_hint) {
         try {
             $value = $this->getProperty($obj, $p_hint);
             if (is_object($value)) {
                 if (!$value instanceof HydratableInterface) {
                     $valueClass = get_class($value);
                     throw new HydratorException("Hydrated class {$valueClass} must implement Corelib\\Data\\Hydratable interface.");
                 }
                 $value = $value->toArray();
             }
             $array[$p_hint] = $value;
         } catch (HydratorException $hex) {
             Console::warn($hex->getMessage());
             continue;
         }
     }
     return $array;
 }