ICanBoogie/Prototype v2.3.0
  • Namespace
  • Class

Namespaces

  • ICanBoogie
    • Prototype

Classes

  • Prototype
  • Prototyped

Traits

  • PrototypeTrait
  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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 
<?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;

use ICanBoogie\Accessor\AccessorTrait;
use ICanBoogie\Prototype\MethodNotDefined;
use ICanBoogie\Prototype\MethodOutOfScope;

/**
 * A trait for classes wishing to implement prototype methods.
 *
 * @property-read Prototype $prototype The prototype associated with the class.
 */
trait PrototypeTrait
{
    use AccessorTrait
    {
        AccessorTrait::has_property as private accessor_has_property;
    }

    /**
     * @var Prototype
     */
    private $prototype;

    /**
     * If a property exists with the name specified by `$method` and holds an object which class
     * implements `__invoke` then the object is called with the arguments. Otherwise, calls are
     * forwarded to the {@link $prototype}.
     *
     * @param string $method
     * @param array $arguments
     *
     * @return mixed
     */
    public function __call($method, $arguments)
    {
        if (isset($this->$method) && is_callable([ $this->$method, '__invoke' ]))
        {
            return call_user_func_array($this->$method, $arguments);
        }

        array_unshift($arguments, $this);

        try
        {
            $prototype = $this->prototype ?: $this->get_prototype();

            return call_user_func_array($prototype[$method], $arguments);
        }
        catch (MethodNotDefined $e)
        {
            if (method_exists($this, $method))
            {
                throw new MethodOutOfScope($method, $this);
            }

            throw $e;
        }
    }

    /**
     * Checks if the object has the specified property.
     *
     * The difference with the `property_exists()` function is that this method also checks for
     * getters defined by the class or the prototype.
     *
     * @param string $property The property to check.
     *
     * @return bool true if the object has the property, false otherwise.
     */
    public function has_property($property)
    {
        if ($this->accessor_has_property($property))
        {
            return true;
        }

        $success = false;
        $this->last_chance_get($property, $success);

        return $success;
    }

    /**
     * Checks whether this object supports the specified method.
     *
     * The method checks for methods defined by the class and the prototype.
     *
     * @param string $method Name of the method.
     *
     * @return bool `true` if the method is defined, `false` otherwise.
     */
    public function has_method($method)
    {
        if (method_exists($this, $method))
        {
            return true;
        }

        $prototype = $this->prototype ?: $this->get_prototype();

        return isset($prototype[$method]);
    }

    /**
     * Returns the prototype associated with the class.
     *
     * @return Prototype
     */
    protected function get_prototype()
    {
        return $this->prototype ?: $this->prototype = Prototype::from($this);
    }

    /**
     * @inheritdoc
     */
    protected function accessor_get($property)
    {
        $method = 'get_' . $property;

        if (method_exists($this, $method))
        {
            return $this->$method();
        }

        $method = 'lazy_get_' . $property;

        if (method_exists($this, $method))
        {
            return $this->$property = $this->$method();
        }

        #
        # we didn't find a suitable method in the class, maybe the prototype has one.
        #

        $prototype = $this->prototype ?: $this->get_prototype();

        $method = 'get_' . $property;

        if (isset($prototype[$method]))
        {
            return call_user_func($prototype[$method], $this, $property);
        }

        $method  = 'lazy_get_' . $property;

        if (isset($prototype[$method]))
        {
            return $this->$property = call_user_func($prototype[$method], $this, $property);
        }

        $success = false;
        $value = $this->last_chance_get($property, $success);

        if ($success)
        {
            return $value;
        }

        $this->assert_property_is_readable($property);
    } //@codeCoverageIgnore

    /**
     * @inheritdoc
     */
    protected function accessor_set($property, $value)
    {
        $method = 'set_' . $property;

        if ($this->has_method($method))
        {
            $this->$method($value);

            return;
        }

        $method = 'lazy_set_' . $property;

        if ($this->has_method($method))
        {
            $this->$property = $this->$method($value);

            return;
        }

        $success = false;
        $this->last_chance_set($property, $value, $success);

        if ($success)
        {
            return;
        }

        $this->assert_property_is_writable($property);

        $this->$property = $value;
    }

    /**
     * The method is invoked as a last chance to get a property,
     * just before an exception is thrown.
     *
     * @param string $property Property to get.
     * @param bool $success If the _last chance get_ was successful.
     *
     * @return mixed
     */
    protected function last_chance_get($property, &$success)
    {
        $success = false;
    }

    /**
     * The method is invoked as a last chance to set a property,
     * just before an exception is thrown.
     *
     * @param string $property Property to set.
     * @param mixed $value Value of the property.
     * @param bool $success If the _last chance set_ was successful.
     */
    protected function last_chance_set($property, $value, &$success)
    {
        $success = false;
    }
}
ICanBoogie/Prototype v2.3.0 API documentation generated by ApiGen