PayPal\Common\ReflectionUtil::isPropertyClassArray PHP Method

isPropertyClassArray() public static method

Checks if the Property is of type array or an object
public static isPropertyClassArray ( $class, $propertyName ) : null | boolean
$class
$propertyName
return null | boolean
    public static function isPropertyClassArray($class, $propertyName)
    {
        // If the class doesn't exist, or the method doesn't exist, return null.
        if (!class_exists($class) || !method_exists($class, self::getter($class, $propertyName))) {
            return null;
        }
        if (($annotations = self::propertyAnnotations($class, $propertyName)) && isset($annotations['return'])) {
            $param = $annotations['return'];
        }
        if (isset($param)) {
            return substr($param, -strlen('[]')) === '[]';
        } else {
            throw new PayPalConfigurationException("Getter function for '{$propertyName}' in '{$class}' class should have a proper return type.");
        }
    }

Usage Example

Example #1
0
 /**
  * Fills object value from Array list
  *
  * @param $arr
  * @return $this
  */
 public function fromArray($arr)
 {
     if (!empty($arr)) {
         // Iterate over each element in array
         foreach ($arr as $k => $v) {
             // If the value is an array, it means, it is an object after conversion
             if (is_array($v)) {
                 // Determine the class of the object
                 if (($clazz = ReflectionUtil::getPropertyClass(get_class($this), $k)) != null) {
                     // If the value is an associative array, it means, its an object. Just make recursive call to it.
                     if (empty($v)) {
                         if (ReflectionUtil::isPropertyClassArray(get_class($this), $k)) {
                             // It means, it is an array of objects.
                             $this->assignValue($k, array());
                             continue;
                         }
                         $o = new $clazz();
                         //$arr = array();
                         $this->assignValue($k, $o);
                     } elseif (ArrayUtil::isAssocArray($v)) {
                         /** @var self $o */
                         $o = new $clazz();
                         $o->fromArray($v);
                         $this->assignValue($k, $o);
                     } else {
                         // Else, value is an array of object/data
                         $arr = array();
                         // Iterate through each element in that array.
                         foreach ($v as $nk => $nv) {
                             if (is_array($nv)) {
                                 $o = new $clazz();
                                 $o->fromArray($nv);
                                 $arr[$nk] = $o;
                             } else {
                                 $arr[$nk] = $nv;
                             }
                         }
                         $this->assignValue($k, $arr);
                     }
                 } else {
                     $this->assignValue($k, $v);
                 }
             } else {
                 $this->assignValue($k, $v);
             }
         }
     }
     return $this;
 }