xp-forge / partial

Partials: Compile-time metaprogramming
0 stars 0 forks source link

Explain how to extend #4

Open thekid opened 9 years ago

thekid commented 9 years ago

The README file should contain how to extend this library with custom transformations:

<?php namespace com\example\transform;

/**
 * Creates `getX()` and `setX()` accessors for all member variables.
 */
class MutableValueObject extends \lang\kind\Transformation {

  /**
   * Creates trait body
   *
   * @param  lang.mirrors.TypeMirror $mirror
   * @return string
   */
  protected function body($mirror) {
    $body= '';
    foreach ($this->instanceFields($mirror) as $field) {
      $name= $field->name();
      $body.= 'public function get'.ucfirst($name).'() { return $this->'.$name.'; }';
      $body.= 'public function set'.ucfirst($name).'($arg) { $this->'.$name.'= $arg; }';
    }
    return $body;
  }
}
thekid commented 9 years ago

Maybe think of a declaration helper:

$body.= $this->declare('get'.ucfirst($name), [], '{ return $this->name; }');
$body.= $this->declare('set'.ucfirst($name), ['arg' => $field->type()], '{ $this->name= $arg; }');