all files / src/context/reflection/ class.js

10.38% Statements 11/106
0% Branches 0/48
0% Functions 0/13
11.11% Lines 11/99
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                      1×                                                             1×                                   1×                             1×                           1×                                           1×                   1×                   1×                   1×                   1×                                                                                                             1×  
/*!
 * Copyright (C) 2017 Glayzzle (BSD3 License)
 * @authors https://github.com/glayzzle/php-transpiler/graphs/contributors
 * @url http://glayzzle.com
 */
'use strict';
 
/**
 * Define the reflection wrapper
 * @constructor {Class}
 */
var Class = function(name) {
  this.abstract = false;
  this.final = false;
  this.implements = [];
  this.extends = null;
  this._proto = null;
 
  /** STORE DEFAULT PROPERTIES (WITH THEIR SCOPE) **/
  this.public = {};
  this.protected = {};
  this.private = {};
  this.static = {
    public: {},
    protected: {},
    private: {},
    constant: {}
  };
 
 
  if (typeof name === 'function') {
    this.name = name.prototype.constructor.name;
  } else {
    this.name = name || 'stdClass';
  }
 
};
 
 
/**
 * Extends the specified prototype
 */
Class.prototype.extends = function(parent) {
  if (typeof parent.getClass === 'function') {
    // passed a class object instance, so retrieve its reflection object
    this._extends = parent.getClass();
    // stores the reflection object
    if ( this._extends._final ) {
      this._extends = null;
      throw new Error('Could not inherit from a final class !');
    }
  } else {
    throw new Error('Could not extend from the specified object, expects a class declaration interface !');
  }
  return this;
};
/**
 * This class is final
 * => Cannont be extended
 */
Class.prototype.final = function() {
  if (this._proto) throw new Error('ILLEGAL operation, class prototype is already defined !');
  if (arguments.length == 1) {
    this._final = arguments[0];
  } else this._final = true;
  if ( this._final && this._abstract ) {
    this._final = false;
    throw new Error('A class could not be both final and abstract !');
  }
  return this;
};
/**
 * This class is an abstract class
 * => Cannot be dirrectly instanciated
 */
Class.prototype.abstract = function() {
  if (this._proto) throw new Error('ILLEGAL operation, class prototype is already defined !');
  if (arguments.length == 1) {
    this._abstract = arguments[0];
  } else this._abstract = true;
  if ( this._final && this._abstract ) {
    this._abstract = false;
    throw new Error('A class could not be both final and abstract !');
  }
  return this;
};
/**
 * Defines the class interface
 */
Class.prototype.static = function(properties) {
  if (this._proto) throw new Error('ILLEGAL operation, class prototype is already defined !');
  if (properties.hasOwnProperty('public')) {
    for(var i in properties.public) {
      this._static.public[i] = properties.public[i];
    }
  }
  if (properties.hasOwnProperty('protected')) {
    for(var i in properties.protected) {
      this._static.protected[i] = properties.protected[i];
    }
  }
  if (properties.hasOwnProperty('private')) {
    for(var i in properties.private) {
      this._static.private[i] = properties.private[i];
    }
  }
  return this;
};
/**
 * Define some constants
 */
Class.prototype.constants = function(properties) {
  if (this._proto) throw new Error('ILLEGAL operation, class prototype is already defined !');
  for(var i in properties) {
    this._static.constant[i] = properties[i];
  }
  return this;
};
/**
 * Define public functions and properties
 */
Class.prototype.public = function(properties) {
  if (this._proto) throw new Error('ILLEGAL operation, class prototype is already defined !');
  for(var i in properties) {
    this._public[i] = properties[i];
  }
  return this;
};
/**
 * Define protected functions and properties
 */
Class.prototype.protected = function(properties) {
  if (this._proto) throw new Error('ILLEGAL operation, class prototype is already defined !');
  for(var i in properties) {
    this._protected[i] = properties[i];
  }
  return this;
};
/**
 * Define private functions and properties
 */
Class.prototype.private = function(properties) {
  if (this._proto) throw new Error('ILLEGAL operation, class prototype is already defined !');
  for(var i in properties) {
    this._private[i] = properties[i];
  }
  return this;
};
/**
 * Makes the prototype
 */
Class.prototype.getPrototype = function() {
  if (!this._proto) {
    var reflection = this;
    this._proto = function() {
      // use the public constructor
      if (reflection._public.hasOwnProperty('__construct')) {
        this.__construct.apply(this, arguments);
      }
    };
    this._proto.toString = function() {
      return reflection._name ? reflection._name : 'stdClass';
    };
    var __self = {};
    // handle inheritance
    if (this._extends) {
      // declare public elements
      for(var i in this._extends._public) {
        __self[i] = this._extends._public[i];
      }
      // declare static elements
      for(var i in this._extends._static.public) {
        this._proto[i] = this._extends._static.public[i];
      }
    }
    // declare public elements
    for(var i in this._public) {
      __self[i] = this._public[i];
    }
    // declare protected elements
    for(var i in this._protected) {
      Object.defineProperty(__self, i, {
        value: this._public[i],
        enumerable: false,
        configurable: false,
        writable: true
      });
    }
    // declare static elements
    for(var i in this._static.public) {
      this._proto[i] = this._static.public[i];
    }
    // declare static constants
    for(var i in this._static.constant) {
      this._proto[i] = this._static.constant[i];
    }
    // reflection handler, like the java getClass helper
    this._proto.getClass = __self.getClass = function() {
      return reflection;
    };
    this._proto.prototype = __self;
    this._proto.prototype.constructor = this._proto;
  }
  return this._proto;
};
 
module.exports = Class;