ICanBoogie/Accessor master
  • Namespace
  • Class

Namespaces

  • ICanBoogie
    • Accessor

Classes

  • AccessorReflection

Interfaces

  • HasAccessor

Traits

  • AccessorCamelTrait
  • AccessorSnakeTrait
  • AccessorTrait
  • FormatAsCamel
  • FormatAsSnake
  • SerializableTrait
  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 
<?php

/*
 * This file is part of the ICanBoogie package.
 *
 * (c) Olivier Laviale <olivier.laviale@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace ICanBoogie\Accessor;

/**
 * Provides methods to reflect on accessor.
 */
class AccessorReflection
{
    static private $private_properties_cache = [];
    static private $facade_properties_cache = [];

    static private function resolve_reference($reference)
    {
        if (is_object($reference))
        {
            return get_class($reference);
        }

        return $reference;
    }

    /**
     * Returns the private properties defined by the reference, this includes the private
     * properties defined by the whole class inheritance.
     *
     * @param string|object $reference Class name or instance.
     *
     * @return \ReflectionProperty[]
     */
    static public function resolve_private_properties($reference)
    {
        $reference = self::resolve_reference($reference);

        if (isset(self::$private_properties_cache[$reference]))
        {
            return self::$private_properties_cache[$reference];
        }

        $private_properties = [];
        $class_reflection = new \ReflectionClass($reference);

        while ($class_reflection)
        {
            $private_properties[] = $class_reflection->getProperties(\ReflectionProperty::IS_PRIVATE);
            $class_reflection = $class_reflection->getParentClass();
        }

        return self::$private_properties_cache[$reference] = $private_properties
            ? call_user_func_array('array_merge', $private_properties)
            : [];
    }

    /**
     * Returns the façade properties implemented by the specified reference.
     *
     * A façade property is a combination of a private property with the corresponding volatile
     * getter and setter.
     *
     * @param string|HasAccessor $reference Class name or instance implementing {@link HasAccessor}.
     *
     * @return \ReflectionProperty[]
     */
    static public function resolve_facade_properties($reference)
    {
        $reference = self::resolve_reference($reference);

        if (isset(self::$facade_properties_cache[$reference]))
        {
            return self::$facade_properties_cache[$reference];
        }

        $facade_properties = [];

        foreach (self::resolve_private_properties($reference) as $property)
        {
            $name = $property->name;

            if (!method_exists($reference, $reference::accessor_format($name, HasAccessor::ACCESSOR_TYPE_GETTER))
            || !method_exists($reference, $reference::accessor_format($name, HasAccessor::ACCESSOR_TYPE_SETTER)))
            {
                continue;
            }

            $facade_properties[$name] = $property;
        }

        return self::$facade_properties_cache[$reference] = $facade_properties;
    }
}
ICanBoogie/Accessor master API documentation generated by ApiGen