Object Property List (OPL)

From The Travis van der Font Wiki
Jump to: navigation, search
**
 * Class OPL (Object Property List)
 */
class OPL extends OPLDictionary
{
    /**
     * Creates a new object instance
     *
     * This method creates a new object instance from from the passed $className
     * and $arguments. The second param $arguments is optional.
     *
     * @param $className
     * @param array $arguments
     *
     * @return bool|mixed
     */
    private static function createInstance($className, array $arguments = [])
    {
        try {
            if (class_exists($className)) {
                return call_user_func_array([
                    new \ReflectionClass($className),
                    'newInstance'
                ],
                $arguments);
            }
        } catch(\ReflectionException $e) {
            return false;
        }

        return false;
    }

    /**
     * Dynamically calls static class method from ObjectProperties namespace
     *
     * @param $name
     * @param $arguments
     *
     * @return bool|mixed
     */
    public static function __callStatic($name, $arguments)
    {
        $className = vsprintf('%s\ObjectProperties\\%s', [
            __NAMESPACE__,
            $name
        ]);

        return self::createInstance($className, $arguments);
    }
}

$test = OPL::testListProjects([
    'elementValue' => 1,
    'keyId'        => 2
]);

$test->elementValue;
$test->keyId;
THIS IS WORK IN PROGRESS Note: this method isn't complete and is particularly lazy due to the fact that it relays on PHP DocBlocks for Property Collections.
namespace OLP
{
    /* @method static reportingTos reportingTos(array $x) */
    abstract class ListPropertyCollection {}
}

namespace OLP
{
    class Collection extends ListPropertyCollection
    {
        static function __callStatic ($name, $arguments)
        {
            foreach ($arguments as $method)
            {
                print_r($method); exit;
                
                $obj = [];
                
            }
        }
    }
}

class reportingTos extends OLP\Collection
{
    public $name;
    public $email;
}

$x = OLP::reportingTos([
    'name'  => ['name1'],
    'email' => ['email2']
]);

var_dump($x->name);
namespace Financings\Domain;

/**
 * Object Property List Dictionary
 *
 * @method static ObjectProperties\testListProjects testListProjects(array $objects)
 */
class OPLDictionary {}

namespace Financings\Domain\ObjectProperties;

/**
 * @property int    $keyId
 * @property string $elementValue
 */
class testListProjects extends __testConstructor {}

namespace Financings\Domain\ObjectProperties;

// ObjectAssigner
class __testConstructor
{
    public function __construct($objects)
    {
        foreach ($objects as $key => $element) {
            $this->{$key} = $element;
        }
    }
}