Giter Site home page Giter Site logo

meta's People

Contributors

jakubkulhan avatar jerryx-jx avatar klatys avatar mzstic avatar senfix avatar ssteklik avatar zemistr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

meta's Issues

Synthetic properties support

Beside properties, it should be possible to annotate also getters and setters with annotations like @PhpArrayOffset, @JsonProperty etc. to support non-trivial cases.

Use cases:

  • properties as a functions of other properties
class User
{
    /** @var string */
    public $firstName;

    /** @var string */
    public $lastName;

    /**
     * @return string
     *
     * @PhpArrayOffset("name")
     */
    public function getName()
    {
        return sprintf("%s %s", $this->firstName, $this->lastName);
    }
}

$user = new User();
$user->firstName = "Jakub";
$user->lastName = "Kulhan";

UserMeta::toArray($user);
// ["firstName" => "Jakub", "lastName" => "Kulhan", "name" => "Jakub Kulhan"]
  • legacy property names
class Product
{
    /** @var string */
    public $description;

    /**
     * @return string
     *
     * @PhpArrayOffset("descr")
     */
    public function getDescr()
    {
        return $this->description;
    }
}
class Product
{
    /** @var Variant */
    public $variants;

    /**
     * @return TermGroup[]
     *
     * @PhpArrayOffset("term_groups")
     */
    public function getTermGroups()
    {
        // converts Variants to TermGroups
    }

    /**
     * @var TermGroup[]
     * @return void
     *
     * @PhpArrayOffset("term_groups")
     */
    public function setTermGroups($termGroups)
    {
        // converts TermGroups to Variants
    }
}

MetaInterface::hash($object)

To generate fast object checksums.

namespace Skrz\Meta;

interface MetaInterface
{
    // ...
    public static function hash($object, $algoOrCtx = "md5", $raw = false);
    // ...
}
  • It should be possible to annotate a property to be ignored from checksum to be able to store once computed checksum in an object property, something like @Hash.
  • Should use hash_init(), hash_update() & hash_final() for incremenetal updates.
  • If passed algo name, creates hash context using hash_init() and then returns hash. If passed hash context, updates it, does not return anything.

Incompatibility with Doctrine's ArrayCollection

When using Doctrine entities there's a problem with transforming them to arrays.
Example:

/** @Doctrine\ORM\Mapping\Entity */
class Cart
{
    /** @var CartItem[] */
    public $cartItems;

    public function __construct()
    {
        $this->cartItems = new ArrayCollection();
    }
}

ArrayCollection serves as a helper when modifiying the array.
Problems occurs with this:

/** @var Cart $cart */
$arr = CartMeta::toArray($cart);

Because skrz/meta (PhpModule) applies casting to child array entities like this:

foreach ((array)$input['cartItems'] as $k0 => $v0) {
...
}

Quickfix is easy - remove the casting. Better solution would be to support ArrayCollections :)

Use Roave/BetterReflection & phpDocumentor/TypeResolver

Meta currently wraps PHP's type system (https://github.com/skrz/meta/tree/master/src/Skrz/Meta/Reflection) and uses standard PHP reflection. It has several downsides:

  • class has to be loaded in the memory -- there cannot be long-running deamon that would compile meta classes incrementally after the file change
  • because class can reference other classes, everything has to be saved on the filesystem (the problem arises when being used as protoc-gen-php)

Consider replacing standard PHP reflection with Roave/BetterReflection and type system wrapping with phpDocumentor/TypeResolver.

Add protoc-gen-php to bin list

If I add the repo as a dependency and then try and use protoc-gen-php it breaks saying it can't load autoload.php.

failed to open stream: No such file or directory in ..../vendor/skrz/meta/bin/protoc-gen-php on line 10
 require_once(): Failed opening required '....meta/bin/../vendor/autoload.php'

Could protoc-gen-php be added to the bin list in composer.json?

Edit: I realise now that won't even fix it. I found this stack chain which seems promising though:

http://stackoverflow.com/questions/30420629/php-autoloader-class-to-load-files-from-two-or-three-different-path

Self-describing objects

There is no way to determine what meta object represents given object. Convention <namespace>\Meta\<classname>Meta cannot be used because of proxy objects.

Self-describing objects would implement Skrz\Meta\ObjectInterface:

namespace Skrz\Meta;

interface ObjectInterface
{

    /**
     * @return MetaInterface
     */
    public static function meta();

}

Filtered toArray/toObject/toJson/toArrayOfJson support

Useful for many kinds of API - REST, GraphQL, Falcor Model etc. to filter fields that will be sent to client.

Filter can be specified as array where keys are property names that should be exported. Only mentioned properties will be exported. Cannot include whole objects (only atomic properties can be filtered). Properties of sub-objects can be specified using sub-array filters.

class Product
{
    /** @var string */
    public $title;

    /** @var string */
    public $slug;

    /** @var Category[] */
    public $categories;
}

class Category
{
    /** @var string */
    public $title;

    /** @var string */
    public $slug;
}

$product = new Product();
$product->title = "iPhone 6";
$product->slug = "iphone-6";

$electronics = new Category();
$electronics->title = "Electronics";
$electronics->slug = "electronics";

$mobilePhones = new Category();
$mobilePhones->title = "Mobile Phones";
$mobilePhones->slug = "mobile-phones";

$product->categories = [$electronics, $mobilePhones];

ProductMeta::toArray($product, null, ["title" => true]); 
// ["title" => "iPhone 6"]

ProductMeta::toArray($product, null, ["slug" => true]); 
// ["slug" => "iphone-6"]

ProductMeta::toArray($product, null, ["categories" => true]); 
// throw InvalidArgumentException("An atomic property has to be specified.")

ProductMeta::toArray($product, null, ["categories" => ["title" => true]]); 
// ["categories" => [["title" => "Electronics"], ["title" => "Mobile Phones"]]]

Better error message for mixed type

class AnEntity
{
    /**garbage
     * @var int
     */
    protected $id;
}

Currently the message says "Property AnEntity::$id of type mixed. Either add @var, or @transient annotation." The user is confused, because there is @var annotation. The problem is caused by PHP not recognizing comment as doc comment. Error message should warn that there is no doc comment.

XML support

  • Support for fromXml to be passed XMLReader or DOMElement (use XMLReader::expand() or create separate implementations?)
  • toXml can be passed XMLWriter or creates DOMElement (needs separate implementations)
  • @XmlElement + @XmlElementWrapper annotations, e.g.
class Product
{
    /**
     * @var string[]
     *
     * @XmlElement("IMAGE")
     * @XmlElementWrapper("IMAGES")
     */
    public $images;
}

All following <PRODUCT>s should result in same object. Should be serialized as 1st option.

<PRODUCT>
    <IMAGES>
        <IMAGE>http://skrz.cz/storage/img/20150904/abcd.jpg</IMAGE> 
        <IMAGE>http://skrz.cz/storage/img/20150904/efgh.jpg</IMAGE> 
    </IMAGES>
</PRODUCT>
<PRODUCT>
    <IMAGE>http://skrz.cz/storage/img/20150904/abcd.jpg</IMAGE> 
    <IMAGE>http://skrz.cz/storage/img/20150904/efgh.jpg</IMAGE> 
</PRODUCT>
<PRODUCT>
    <IMAGES>
        <IMAGE>http://skrz.cz/storage/img/20150904/abcd.jpg</IMAGE> 
    </IMAGES>
    <IMAGE>http://skrz.cz/storage/img/20150904/efgh.jpg</IMAGE> 
</PRODUCT>
  • @XmlValue to map content of a node to object property:
class Product
{
    /**
     * @var Image[]
     *
     * @XmlElement("IMAGE")
     * @XmlElementWrapper("IMAGES")
     */
    public $images;
}

class Image
{
    /**
     * @var string
     *
     * @XmlValue
     */
    public $url;
}
  • @XmlAttribute to map attribute of a node to object property:
class Product
{
    /**
     * @var Param[]
     *
     * @XmlElement("param")
     */
    public $params;
}

class Param
{
    /**
     * @var string
     *
     * @XmlAttribute("name")
     */
    public $name;

    /**
     * @var string
     *
     * @XmlValue
     */
    public $value;
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.