Skip to content
Browse files

initial import

  • Loading branch information...
0 parents commit 04c41e746756c65dc8ce03c2a4d5ecebe0fae0ae @gonzalo123 committed Aug 7, 2011
Showing with 267 additions and 0 deletions.
  1. +28 −0 lib/HClass/Fct.php
  2. +51 −0 lib/HClass/HClass.php
  3. +22 −0 lib/HClass/HCls.php
  4. +166 −0 tests/HclassTest.php
28 lib/HClass/Fct.php
@@ -0,0 +1,28 @@
+<?php
+namespace HClass;
+
+class Fct
+{
+ static function create($name, \Closure $fct)
+ {
+ return new self($name, $fct);
+ }
+
+ protected $_name;
+ function __construct($name, \Closure $fct)
+ {
+ $this->_name = $name;
+ $this->_fct = $fct;
+ }
+
+ function getName()
+ {
+ return $this->_name;
+ }
+
+ protected $_fct;
+ function getCallBack()
+ {
+ return $this->_fct;
+ }
+}
51 lib/HClass/HClass.php
@@ -0,0 +1,51 @@
+<?php
+namespace HClass;
+
+include_once('Fct.php');
+include_once('HCls.php');
+
+use HClass\Fct;
+
+class HClass
+{
+ const __construct = '__construct';
+ static function define(HClass $extends=null)
+ {
+ return is_null($extends) ? new self($extends) : clone $extends;
+ }
+
+ private $_extends;
+ function __construct(HClass $extends=null)
+ {
+ $this->_extends = $extends;
+ }
+
+ public $constructor;
+ public $fcs = array();
+ public $fields = array();
+
+ function fct($name, \Closure $fct)
+ {
+ return $this->add(Fct::create($name, $fct));
+ }
+
+ function add($item)
+ {
+ if ($item instanceof Fct) {
+ $this->fcs[$item->getName()] = $item;
+ } else {
+ throw new \Exception("undefined element");
+ }
+ return $this;
+ }
+
+ function create()
+ {
+ $obj = new HCls($this);
+
+ if (isset($this->fcs[self::__construct])) {
+ call_user_func_array($this->fcs[self::__construct]->getCallBack(), array_merge(array('self' => $obj), func_get_args()));
+ }
+ return $obj;
+ }
+}
22 lib/HClass/HCls.php
@@ -0,0 +1,22 @@
+<?php
+namespace HClass;
+
+class HCls
+{
+ private $_conf;
+
+ function __construct(HClass $conf)
+ {
+ $this->_conf = $conf;
+ }
+
+ public function __call($methodName, $args=array())
+ {
+ if (array_key_exists($methodName, $this->_conf->fcs)) {
+ $method = $this->_conf->fcs[$methodName];
+ return call_user_func_array($method->getCallBack(), array_merge(array('self' => $this), $args));
+ } else {
+ throw new \Exception("ERROR Method '{$methodName}' does not exits");
+ }
+ }
+}
166 tests/HclassTest.php
@@ -0,0 +1,166 @@
+<?php
+require_once __DIR__ . '/../lib/HClass/HClass.php';
+use HClass\HClass;
+
+class HclassTest extends PHPUnit_Framework_TestCase
+{
+ function testSimpleUsage()
+ {
+ $human = HClass::define()
+ ->fct(HClass::__construct, function($self, $name) {$self->name = $name;})
+ ->fct('hello', function($self) {
+ return "{$self->name} says hello";
+ });
+
+ $gonzalo = $human->create('Gonzalo');
+ $peter = $human->create('Peter');
+
+ $this->assertEquals("Gonzalo says hello", $gonzalo->hello());
+ $this->assertEquals("Peter says hello", $peter->hello());
+ }
+
+ function testCallingUndefinedFunctions()
+ {
+ $human = HClass::define()
+ ->fct(HClass::__construct, function($self, $name) {$self->name = $name;})
+ ->fct('hello', function($self) {
+ return "{$self->name} says hello";
+ });
+
+ $gonzalo = $human->create('Gonzalo');
+ $this->setExpectedException('Exception', "ERROR Method 'goodbye' does not exits");
+ $gonzalo->goodbye();
+ }
+
+ function testInheritance()
+ {
+ $human = HClass::define()
+ ->fct(HClass::__construct, function($self, $name) {$self->name = $name;})
+ ->fct('hello', function($self) {
+ return "{$self->name} says hello";
+ });
+
+ $shyHuman = HClass::define($human)
+ ->fct('hello', function($self) {
+ return "{$self->name} is shy and don't says hello";
+ });
+
+ $gonzalo = $human->create('Gonzalo');
+ $peter = $shyHuman->create('Peter');
+
+ $this->assertEquals("Gonzalo says hello", $gonzalo->hello());
+ $this->assertEquals("Peter is shy and don't says hello", $peter->hello());
+ }
+
+ function testDinamicallyFunctionCreation()
+ {
+ $human = HClass::define()
+ ->fct(HClass::__construct, function($self, $name) {$self->name = $name;})
+ ->fct('hello', function($self) {
+ return "{$self->name} says hello";
+ });
+
+ $gonzalo = $human->create('Gonzalo');
+ $this->assertEquals("Gonzalo says hello", $gonzalo->hello());
+
+ try {
+ $gonzalo->goodbye();
+ } catch (Exception $e) {
+ $this->assertEquals("ERROR Method 'goodbye' does not exits", $e->getMessage());
+ }
+
+ $human->fct('goodbye', function($self) {
+ return "{$self->name} says goodbye";
+ });
+
+ $this->assertEquals("Gonzalo says goodbye", $gonzalo->goodbye());
+ }
+
+ function testFizzBuzz()
+ {
+ $fizzBuzz = HClass::define()
+ ->fct('run', function($self, $elems = 100) {
+ list($fizz, $buzz) = array('Fizz', 'Buzz');
+ return array_map(function ($element) use ($fizz, $buzz) {
+ $out = array();
+ if ($element % 3 == 0 || strpos((string) $element, '3') !== false ) {
+ $out[] = $fizz;
+ }
+ if ($element % 5 == 0 || strpos((string) $element, '5') !== false ) {
+ $out[] = $buzz;
+ }
+ return (count($out) > 0) ? implode('', $out) : $element;
+ }, range(0, $elems-1));
+ });
+ $fizzBuzz = $fizzBuzz->create();
+ $arr = $fizzBuzz->run();
+
+ $this->assertEquals(count($arr), 100);
+ $this->assertEquals($arr[1], 1);
+ $this->assertEquals($arr[3], 'Fizz');
+ $this->assertEquals($arr[4], 4);
+ $this->assertEquals($arr[5], 'Buzz');
+ $this->assertEquals($arr[6], 'Fizz');
+ $this->assertEquals($arr[20], 'Buzz');
+ $this->assertEquals($arr[13], 'Fizz');
+ $this->assertEquals($arr[15], 'FizzBuzz');
+ $this->assertEquals($arr[53], 'FizzBuzz');
+ }
+
+ function testAnotherFizzBuzzImplementationWithDependencyInjection()
+ {
+ $fizzBuzz = HClass::define();
+ $fizzBuzz->fct(HClass::__construct, function($self, $fizzBuzzElement) {
+ $self->fizzBuzzElement = $fizzBuzzElement;
+ });
+ $fizzBuzz->fct('run', function($self, $elems = 100) {
+ $out = array();
+ foreach (range(1, $elems) as $elem) {
+ $out[$elem] = $self->fizzBuzzElement->render($elem);
+ }
+ return $out;
+ });
+
+ $fizzBuzzElement = HClass::define()
+ ->fct('render', function($self, $element) {
+ list($fizz, $buzz) = array('Fizz', 'Buzz');
+ $out = array();
+ if ($element % 3 == 0 || strpos((string) $element, '3') !== false ) {
+ $out[] = $fizz;
+ }
+
+ if ($element % 5 == 0 || strpos((string) $element, '5') !== false ) {
+ $out[] = $buzz;
+ }
+ return (count($out) > 0) ? implode('', $out) : $element;
+ });
+
+ $fbe = $fizzBuzzElement->create();
+
+ $this->assertEquals($fbe->render(1), 1);
+
+ $this->assertEquals($fbe->render(2), 2);
+ $this->assertEquals($fbe->render(3), 'Fizz');
+ $this->assertEquals($fbe->render(4), 4);
+ $this->assertEquals($fbe->render(5), 'Buzz');
+ $this->assertEquals($fbe->render(6), 'Fizz');
+ $this->assertEquals($fbe->render(20), 'Buzz');
+ $this->assertEquals($fbe->render(13), 'Fizz');
+ $this->assertEquals($fbe->render(15), 'FizzBuzz');
+ $this->assertEquals($fbe->render(53), 'FizzBuzz');
+
+ $fb = $fizzBuzz->create($fbe);
+ $arr = $fb->run();
+
+ $this->assertEquals(count($arr), 100);
+ $this->assertEquals($arr[1], 1);
+ $this->assertEquals($arr[3], 'Fizz');
+ $this->assertEquals($arr[4], 4);
+ $this->assertEquals($arr[5], 'Buzz');
+ $this->assertEquals($arr[6], 'Fizz');
+ $this->assertEquals($arr[20], 'Buzz');
+ $this->assertEquals($arr[13], 'Fizz');
+ $this->assertEquals($arr[15], 'FizzBuzz');
+ $this->assertEquals($arr[53], 'FizzBuzz');
+ }
+}

0 comments on commit 04c41e7

Please sign in to comment.
Something went wrong with that request. Please try again.