PayPal\Core\PPUtils::propertyAnnotations PHP Method

propertyAnnotations() public static method

Get property annotations for a certain property in a class
public static propertyAnnotations ( string $class, string $propertyName ) : string
$class string
$propertyName string
return string
    public static function propertyAnnotations($class, $propertyName)
    {
        $class = is_object($class) ? get_class($class) : $class;
        if (!class_exists('ReflectionProperty')) {
            throw new \RuntimeException("Property type of " . $class . "::{$propertyName} cannot be resolved");
        }
        if ($annotations =& self::$propertiesType[$class][$propertyName]) {
            return $annotations;
        }
        if (!($refl =& self::$propertiesRefl[$class][$propertyName])) {
            $refl = new \ReflectionProperty($class, $propertyName);
        }
        // todo: smarter regexp
        if (!preg_match_all('~\\@([^\\s@\\(]+)[\\t ]*(?:\\(?([^\\n@]+)\\)?)?~i', $refl->getDocComment(), $annots, PREG_PATTERN_ORDER)) {
            return null;
        }
        foreach ($annots[1] as $i => $annot) {
            $annotations[strtolower($annot)] = empty($annots[2][$i]) ? true : rtrim($annots[2][$i], " \t\n\r)");
        }
        return $annotations;
    }

Usage Example

 /**
  * @param string $property
  * @param PPXmlMessage|string $value
  * @param string $namespace
  * @return string
  */
 private function buildProperty($property, $value, $namespace = 'ebl')
 {
     $annotations = PPUtils::propertyAnnotations($this, $property);
     if (!empty($annotations['namespace'])) {
         $namespace = $annotations['namespace'];
     }
     if (!empty($annotations['name'])) {
         $property = $annotations['name'];
     }
     if ($namespace === true) {
         $el = '<' . $property;
     } else {
         $el = '<' . $namespace . ':' . $property;
     }
     if (!is_object($value)) {
         $el .= '>' . PPUtils::escapeInvalidXmlCharsRegex($value);
     } else {
         if (substr($value = $value->toXMLString(), 0, 1) === '<' || $value == '') {
             $el .= '>' . $value;
         } else {
             $el .= ' ' . $value;
         }
     }
     if ($namespace === true) {
         return $el . '</' . $property . '>';
     } else {
         return $el . '</' . $namespace . ':' . $property . '>';
     }
 }
All Usage Examples Of PayPal\Core\PPUtils::propertyAnnotations