Skip to content

Instantly share code, notes, and snippets.

@waelio
Last active August 13, 2021 08:31
Show Gist options
  • Save waelio/1e4641570550deb24466 to your computer and use it in GitHub Desktop.
Save waelio/1e4641570550deb24466 to your computer and use it in GitHub Desktop.
autonymous object
<?php
/**
* Created by PhpStorm.
* User: wahbehw
* Date: 2/1/2016
* Time: 9:26 AM
*/
//namespace entity_object;
class Entity
{
private $initiated;
public $_id;
private $library = array('directions' => array('root', 'up', 'right', 'down', 'left'));
public $creator;
public $creator_location;
public $my_location;
public $hasCreation;
public $Creation;
function __construct($params){
/**
* possibilities??
* 1. COMMAND: initiation
* this command will create the object but requires few things:
* a. the creator relation [up,down,right,left]
* b. the creator ID
* c. set of commands [initiate,destroy[commands],call[],identify,bounce[],pass[]]
* d. listen
* e. whisper
*/
return $this->initiate($params);
}
/**
* @param int $length
*
* @return string
*/
function generate_code($length = 64){
$box_array = array('A', 'B', 'C', 'D', 'E', 'F', '1', '2', '3', '7', '9', '5');
$password = '';
for ($x = 0; $x < $length; $x++) {
$password .= $box_array[intval(array_rand($box_array))];
}
return $password;
}
/**
* @return string
*/
function identify()
{
return (isset($this->_id)) ? $this->_id : $this->_id = $this->generate_code(12);
}
function initiate($params){
$new_data = (isset($params['data'])) ? $params['data'] : null;
$new_functions = (isset($params['absorb'])) ? $params['absorb'] :null;
if ($this->initiated){
if (isset($new_data['direction'])){
if (in_array($new_data['direction'],$this->library['directions'])){
if ($new_data['direction']== 'root'){
foreach ($new_data as $k => $v) {
$this->$k = $v;
}
foreach ($new_functions as $function_x) {
foreach($function_x as $name => $args){
$test = 'function ' . $name . "({$args['args']}) { ".$args['code']." }";
$this->{$name} = eval($test);
}
}
}else{
return new Entity($params);
}
}
}
}else{
if (isset($new_data['initiator'])){
$this->_id = $this->generate_code(64);
$this->creator_location = $new_data['location'];
$this->creator = $new_data['initiator'];
$this->my_location = $new_data['direction'];
$this->initiated=true;
return 'Created self: '.$this->_id;
}
}
return true;
}
function create($params){
$creation = new Entity($params);
$this->hasCreation = true;
$this->Creation =
array(
'created_location'=>$creation->my_location,
'created_id'=>$creation->_id
);
return $creation;
}
function _call($params){
$new_commands = (isset($params['commands'])) ? $params['commands'] :null;
foreach ($new_commands as $command) {
foreach ($command as $function => $args) {
if (isset($this->{$function})) {
return call_user_func_array($this->{$function}, $args);
}
}
}
return true;
}
}
/**
* Testing
*/
function debug_this($val){
echo "<pre>".print_r($val,true)."</pre>";
}
$params=array(
'data'=>array(
'direction'=>'root',
'initiator'=>'user',
'location'=>'terminal'
),
'absorb'=>array(
array(
'name'=>'dance',
'args'=>''
)
),
'commands'=>
array(
'name'=>'dance',
'args'=>array(
'args'=>'$string',
'code'=>'echo $string'
)
)
);
$bot1 = new Entity($params);
debug_this($bot1);
$bot2 = $bot1->create(
array('data'=>
array(
'direction'=>'top',
'initiator'=>$bot1->identify(),
'location'=>$bot1->my_location
)
)
);
debug_this($bot2);
debug_this($bot1);
echo $bot1->_call(array(
'commands'=>
array(
'name'=>'dance',
'args'=>'Hello'
)));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment