ICanBoogie/Accessor v1.0.2
  • 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 
<?php

namespace ICanBoogie\Accessor;

/**
 * Improves serialization of objects, exporting façade properties and removing properties for
 * which lazy getters are defined.
 *
 * @package ICanBoogie\Accessor
 */
trait SerializableTrait
{
    abstract /*static*/ public function accessor_format($property, $type, $lazy = HasAccessor::ACCESSOR_IS_NOT_LAZY);

    /**
     * @inheritdoc
     */
    public function __sleep()
    {
        return $this->accessor_sleep();
    }

    /**
     * @inheritdoc
     */
    public function __wakeup()
    {
        $this->accessor_wakeup();
    }

    /**
     * Whether an object has a method.
     *
     * @param string $method
     *
     * @return bool `true` if the object has a method, `false` otherwise.
     */
    abstract protected function has_method($method);

    /**
     * The method returns an array of key/key pairs.
     *
     * Properties for which a lazy getter is defined are discarded. For instance, if the property
     * `next` is defined and the class of the instance defines the getter `lazy_get_next()`, the
     * property is discarded.
     *
     * Note that façade properties are also included.
     *
     * @return array
     */
    private function accessor_sleep()
    {
        $properties = array_keys(get_object_vars($this));

        if ($properties)
        {
            $properties = array_combine($properties, $properties);

            foreach ($properties as $property)
            {
                if ($this->has_method(static::accessor_format($property, HasAccessor::ACCESSOR_TYPE_GETTER, HasAccessor::ACCESSOR_IS_LAZY)))
                {
                    unset($properties[$property]);
                }
            }
        }

        foreach (AccessorReflection::resolve_facade_properties($this) as $name => $property)
        {
            $properties[$name] = "\x00" . $property->class . "\x00" . $name;
        }

        return $properties;
    }

    /**
     * Unsets null properties for which a lazy getter is defined so that it is called when
     * the property is accessed.
     */
    public function accessor_wakeup()
    {
        $properties = get_object_vars($this);

        foreach ($properties as $property => $value)
        {
            if ($this->has_method(static::accessor_format($property, HasAccessor::ACCESSOR_TYPE_GETTER, HasAccessor::ACCESSOR_IS_LAZY)))
            {
                unset($this->$property);
            }
        }
    }
}
ICanBoogie/Accessor v1.0.2 API documentation generated by ApiGen