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: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 
<?php
/**
 * MIT License
 *
 * Copyright (c) 2018, ArrayIterator
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

declare(strict_types=1);

namespace ArrayIterator\Extension;

/**
 * Class ExtensionInfo
 * @package ArrayIterator\Extension
 *
 * ExtensionInfo as reference of default information for extension
 * before extension loaded.
 */
final class ExtensionInfo
{
    /**
     * Class name property.
     *
     * @var string
     */
    protected $className;

    /**
     * Real path of class file placed.
     *
     * @var string
     */
    protected $classPath;

    /**
     * Extension name.
     *
     * @var string
     */
    protected $name;

    /**
     * Extension description.
     *
     * @var string
     */
    protected $description;

    /**
     * Extension version string.
     *
     * @var string
     */
    protected $version;

    /**
     * Strict mode.
     *
     * @var bool
     */
    protected $strictMode;

    /**
     * ExtensionInfo constructor.
     * @param \ReflectionClass $reflection <p>
     * <b>ReflectionClass</b> for reference about object class instance.
     * </p>
     * @param bool $strictMode [optional] <p>
     * Information about use <b>Strict Mode</b> or not.
     * </p>
     */
    public function __construct(\ReflectionClass $reflection, bool $strictMode = false)
    {
        $this->strictMode = $strictMode;
        $this->parseForInfo($reflection);
    }

    /**
     * Parse Info for Reflection to fill the default properties information.
     *
     * @param \ReflectionClass $reflection <p>
     * <b>ReflectionClass</b> for reference about object class instance.
     * </p>
     * @return void
     * @throws \InvalidArgumentException if object <b>ReflectionClass</b>
     * is not a valid object contains instanceof @uses ExtensionInterface.
     */
    protected function parseForInfo(\ReflectionClass $reflection)
    {
        if (!$reflection->isSubclassOf(ExtensionInterface::class)) {
            throw new \InvalidArgumentException(
                sprintf(
                    'ReflectionClass must be object contain of sub class %s.',
                    ExtensionInterface::class
                )
            );
        }

        if (! $reflection->isInstantiable()) {
            throw new \InvalidArgumentException(
                sprintf(
                    'ReflectionClass must be as an instantiable object of %s.',
                    ExtensionInterface::class
                )
            );
        }

        if ($reflection->isAnonymous()) {
            throw new \InvalidArgumentException(
                'ReflectionClass can not be an anonymous object.'
            );
        }

        $this->className = $reflection->getName();
        $this->classPath = $reflection->getFileName();
        $this->name = $reflection->getShortName();
        $this->version = '';
        $this->description = '';

        $prop = $reflection->getDefaultProperties();
        unset($reflection);

        if (isset($prop['extensionVersion'])
            && (is_string($prop['extensionVersion']) || is_numeric($prop['extensionVersion']))
        ) {
            $this->version = (string) $prop['extensionVersion'];
        } elseif (isset($prop['version'])
            && (is_string($prop['version']) || is_numeric($prop['version']))
        ) {
            $this->version = (string) $prop['version'];
        }

        if (isset($prop['extensionDescription']) && is_string($prop['extensionDescription'])) {
            $this->description = $prop['extensionDescription'];
        } elseif (isset($prop['description']) && is_string($prop['description'])) {
            $this->description = $prop['description'];
        }

        if (!empty($prop['extensionName']) && is_string($prop['extensionName'])) {
            $this->name = $prop['extensionName'];
        } elseif (!empty($prop['name']) && is_string($prop['name'])) {
            $this->name = $prop['name'];
        }

        unset($prop);
    }

    /**
     * Check if on Strict Mode.
     *
     * @return bool TRUE if in Strict Mode.
     */
    public function isStrictMode() : bool
    {
        return $this->strictMode;
    }

    /**
     * Check if provide valid reflection.
     *
     * @return bool
     */
    public function isValid() : bool
    {
        return $this->className !== null;
    }

    /**
     * Get default description.
     *
     * @return string extension description.
     */
    public function getDescription() : string
    {
        return $this->description;
    }

    /**
     * Get default version.
     *
     * @return string version string.
     */
    public function getVersion() : string
    {
        return $this->version;
    }

    /**
     * Get default version.
     *
     * @return string extension name.
     */
    public function getName() : string
    {
        return $this->name;
    }

    /**
     * Get class class file path.
     *
     * @return string full class path.
     */
    public function getClassPath() : string
    {
        return $this->classPath;
    }

    /**
     * Get class name.
     *
     * @return string class name.
     */
    public function getClassName() : string
    {
        return $this->className;
    }

    /**
     * Magic Method __sleep keep the data when object being serialize.
     *
     * @return array represent as object properties need to be keep.
     */
    public function __sleep() : array
    {
        return [
            'className',
            'strictMode'
        ];
    }

    /**
     * Magic Method __wakeup() process when serialized object being unserialize.
     *
     * @return void
     */
    public function __wakeup()
    {
        if (!$this->name) {
            try {
                $this->parseForInfo(new \ReflectionClass($this->className));
            } catch (\Exception $e) {
                throw new \InvalidArgumentException(
                    sprintf(
                        'ReflectionClass must be object contain of sub class %s',
                        ExtensionInterface::class
                    )
                );
            }
        }
    }
}