namespace Moussaka\Bin\Core\ORM; use Moussaka\Model\Entity\Entity; use Moussaka\Bin\Core\Container\Container; use Moussaka\Bin\Core\ORM\ORM; use Moussaka\Bin\Core\Helper\Helpers; use Moussaka\Bin\Core\Helper\Helpers as H; Class SOAP { /** * yml soap model absolute path */ private $modelAbsolutePath; /** * Entities,datas */ private $datas = [], $entities = []; /** * CONST fill if no value */ const NO_VALUE_FILL = ''; /** * YMD */ private $dateFormat = 'Y-M-D'; /** * @param type $dateFormat (eg YMD => 19870119) * @return $this */ public function setDateFormat($dateFormat) { $this->dateFormat = $dateFormat; return $this; } /** * @return $this, chaining. * @param String $model yml model relative path */ public function addModel($model) { $c = Container::get(); $this->modelAbsolutePath = get_include_path() . '/' . $c['conf']['db']['SOAP_entity_path'] . '/' . $model; return $this; } /** * @param Array of entities */ public function addEntity(Array $entities) { $this->entities = array_merge($this->entities, $entities); /** * TODO: determiner ou mettre ceci ! */ // $this->entities['garantie_prets'] = &$this->entities['prets']; return $this; } /** * */ public function addDatas(Array $datas) { $this->datas = array_merge($this->datas, $datas); return $this; } public function hydrateEntities() { return $this; } /** * @return Array datas; */ public function outputArray() { /* * Get translation yml into Array */ $modelArray = yaml_parse_file($this->modelAbsolutePath); /** * First pass to do array stuff! */ // var_dump($modelArray);die; /* * ********************************************************************************************** * ON FAIT UN PREMIER PASSAGE POUR DESTACKER LES [][]...n DU YML ET LES TRAITER COMME DES [] * ********************************************************************************************* */ Helpers::arrayWalkRecursive($modelArray, function($i, $key, $parents, $cont) use (&$modelArray) { if (true) { if (count($parents) === 1) { $meta = &$modelArray; } if (count($parents) === 2) { $meta = &$modelArray[$parents[0]]; } if (count($parents) === 3) { $meta = &$modelArray[$parents[0]][$parents[1]]; } if (count($parents) === 4) { $meta = &$modelArray[$parents[0]][$parents[1]][$parents[2]]; } } $parentNodeRaw = array_pop($parents); $parentNode = substr($parentNodeRaw, 0, -4); if (substr($parentNodeRaw, -4) == '[][]') { /** * On étale $cont en fonction du tableau dans entities du nom de parentNodeRaw */ $arrayNode = []; if (isset($this->datas[$parentNode])) { for ($i = 1; isset($this->datas[$parentNode][$i]); $i++) { for ($j = 1; isset($this->datas[$parentNode][$i][$j]); $j++) { $childName = rtrim($parentNode, 's') . '__' . $i . $j; /** * Hack étrange mais necessaire, c'est un bug de php */ $vlumux = $cont; $contCopy = $vlumux; unset($vlumux); $arrayNode[$childName] = $contCopy; foreach ($arrayNode[$childName] as $kc => &$vc) { if (trim($vc) === 'i') { // echo "on transforme \$i $kc en $ii" . PHP_EOL; unset($arrayNode[$childName][$kc]); $arrayNode[$childName] = array_reverse($arrayNode[$childName]); $arrayNode[$childName][$kc] = '_"' . $i . '"'; $arrayNode[$childName] = array_reverse($arrayNode[$childName]); //$vc = '_"' . rand(1,1111) . '"'; // $vc = str_replace('i', "toto[{$k}]", $vc); }elseif(trim($vc) === 'j'){ // unset($arrayNode[$childName][$kc]); // $arrayNode[$childName] = array_reverse($arrayNode[$childName]); $arrayNode[$childName][$kc] = '_"' . $j . '"'; // $arrayNode[$childName] = array_reverse($arrayNode[$childName]); } else { $ii = $i-1; $jj = $j-1; $vc = str_replace(['[i]','[j]'], ["[{$ii}]","[{$jj}]"], $vc); } } } } $m1 = array_slice($meta, 0, array_search($parentNodeRaw, array_keys($meta)), TRUE); $m2 = array_slice($meta, array_search($parentNodeRaw, array_keys($meta)), NULL, TRUE); $meta = array_merge($m1, $arrayNode, $m2); // array_splice($meta, array_search($parentNodeRaw, $meta),0,$arrayNode); //$meta = array_merge($arrayNode,$meta); $meta[$parentNodeRaw] = []; unset($meta[$parentNodeRaw]); } // die('prourpout'); /** * */ // var_dump($meta); // var_dump($cont); // $meta[$parentNodeRaw] = []; // unset($meta[$parentNodeRaw]); // var_dump($meta[$parentNodeRaw]);die; } }, [], 0); Helpers::arrayWalkRecursive($modelArray, function($i, $key, $parents, $cont) use (&$modelArray) { /** * @todo surement les 10 lignes de code les plus horribles de ma vie, trouver comment faire ça propre! */ if (count($parents) === 1) { $meta = &$modelArray; } if (count($parents) === 2) { $meta = &$modelArray[$parents[0]]; } if (count($parents) === 3) { $meta = &$modelArray[$parents[0]][$parents[1]]; } if (count($parents) === 4) { $meta = &$modelArray[$parents[0]][$parents[1]][$parents[2]]; } /** * fin du horrible */ // die('//let comment yeah!'); $parentNodeRaw = array_pop($parents); $parentNode = rtrim($parentNodeRaw, '[]'); // echo($parentNodeRaw . PHP_EOL); // print_r(@$this->entities[$parentNode]); if (substr($parentNodeRaw, -4) == '[][]') {//gogogadjetounstack $deep = 2; //die($parentNodeRaw); $datas = isset($this->entities[$parentNode]) ? $this->entities[$parentNode] : $this->datas[$parentNode]; //var_dump($this->unstackArray($parentNodeRaw, $cont, $datas));die; } // var_dump($this->entities); // die('333333'); if (substr($parentNodeRaw, -2) === '[]' && (is_array($this->entities[$parentNode]) || $this->entities[$parentNode] instanceof \Countable)) { $arrayNode = []; //print_r($this->entities[$parentNode]);die; foreach ($this->entities[$parentNode] as $k => $v) { $ii = $k + 1; $childName = rtrim($parentNode, 's') . '__' . $ii; // var_dump($childName); /** * Hack étrange mais necessaire, c'est un bug de php */ $vlumux = $cont; $contCopy = $vlumux; unset($vlumux); $arrayNode[$childName] = $contCopy; foreach ($arrayNode[$childName] as $kc => &$vc) { //$i = rand(1, 200); /** * ça ne change rien aux opérations (correspondance) * / + - a */ if (trim($vc) === 'i') { // echo "on transforme \$i $kc en $ii" . PHP_EOL; unset($arrayNode[$childName][$kc]); $arrayNode[$childName] = array_reverse($arrayNode[$childName]); $arrayNode[$childName][$kc] = '_"' . $ii . '"'; $arrayNode[$childName] = array_reverse($arrayNode[$childName]); //$vc = '_"' . rand(1,1111) . '"'; // $vc = str_replace('i', "toto[{$k}]", $vc); } else { $vc = str_replace('[i]', "[{$k}]", $vc); } } } $m1 = array_slice($meta, 0, array_search($parentNodeRaw, array_keys($meta)), TRUE); $m2 = array_slice($meta, array_search($parentNodeRaw, array_keys($meta)), NULL, TRUE); $meta = array_merge($m1, $arrayNode, $m2); // array_splice($meta, array_search($parentNodeRaw, $meta),0,$arrayNode); //$meta = array_merge($arrayNode,$meta); $meta[$parentNodeRaw] = []; unset($meta[$parentNodeRaw]); } }); /* * Translate each value recursivly (allow tree) */ // print_r($modelArray);die; H::varDump($modelArray); Helpers::arrayWalkRecursive($modelArray, function(&$i, $key, $parents, &$cont) use (&$modelArray) { /* * is it a litteral? _"hello" or _'hello' */ if (preg_match('/_["\'](.*)["\']/', $i, $matches)) { $i = $matches[1]; return; } if (preg_match('/(?:\/|\*|\+|\-)\s*(-?[0-9]+\.[0-9]*)/', $i, $matches)) { $i = preg_replace("/^(.*-?[0-9]+)\.([0-9]*)$/", "$1,$2", $i); } $required = \FALSE; if (substr(trim($i), -5) === '(REQ)') { $i = substr(trim($i), 0, -5); $required = \TRUE; } $orm = ORM::getInstance(); $fieldInfos = explode('.', $i); /* * if EntityName.value => it must be an entity * Else it's a plain value (implent Callable?) */ if (count($fieldInfos) < 2) { //echo('on le parse ici'); if(preg_match('/(.*)\[(\d+)\]\[(\d+)\]/i', $i,$matches)){ $i = $this->datas[$matches[1]][$matches[2] +1][$matches[3]+1]; return; } $i = isset($this->datas[$i]) ? (string) $this->datas[$i] : $this::NO_VALUE_FILL; return; } $fieldName = $fieldInfos[1]; $arithmeticArray = []; if (preg_match('/(\w*)\s*(\/|\*|\+|\-)\s*(-?[0-9]+,?[0-9]*)\s*$/', $fieldInfos[1], $matches)) { // var_dump((float)$matches[3]); // var_dump($matches);die; $fieldName = $matches[1]; $arithmeticArray = $arithmeticArray = ['op' => $matches[2], 'val' => (float) str_replace(',', '.', $matches[3])]; ; } $transformArray = []; if (preg_match('/(\w*)\s\((.*)\)/', $fieldInfos[1], $matches)) { /* * my first group is just the field name */ $fieldName = $matches[1]; /* * I have to interpret my second group (toto=>tata) to translate. */ /** * @todo Externaliser l'explode associatif dans un helper */ $tempArray = explode(',', $matches[2]); foreach ($tempArray as $assocVal) { $tempAssocVal = explode('=>', $assocVal); $transformArray[$tempAssocVal[0]] = $tempAssocVal[1]; } } $getter = 'get' . $orm->camelize($fieldName, TRUE); //die('babar'); if (preg_match('/(.*)\[(\d+)\]/', $fieldInfos[0], $matches)) { $entity = &$this->entities[$matches[1]][$matches[2]]; } else { $entity = &$this->entities[$fieldInfos[0]]; } if (@method_exists($entity, $getter)) { /** * @todo If (not sure) user input value, check for injection */ $i = $entity->$getter(); if ($required === TRUE && empty($i)) { if (count($parents) === 1) { unset($modelArray[$parents[0]]); unset($this->entities[$matches[1]][$matches[2]]); } if (count($parents) === 2) { unset($modelArray[$parents[0]][$parents[1]]); unset($this->entities[$matches[1]][$matches[2]]); } if (count($parents) === 3) { unset($modelArray[$parents[0]][$parents[1]][$parents[2]]); unset($this->entities[$matches[1]][$matches[2]]); } // $cont = NULL; // unset($cont); return; } //var_dump($i);die; if (preg_match('/([0-9]{4})-([0-9]{2})-([0-9]{2})/', $i, $matches)) { /* * TODO injecter un format de date */ $i = $matches[1] . $matches[2] . $matches[3]; } } else { if ($required === TRUE) { if (count($parents) === 1) { unset($modelArray[$parents[0]]); $entity = []; unset($this->entities[$matches[1]][$matches[2]]); } if (count($parents) === 2) { unset($modelArray[$parents[0]][$parents[1]]); $entity = []; $this->entities[$matches[1]][$matches[2]] = []; unset($this->entities[$matches[1]][$matches[2]]); // die('rr' .$matches[1]); } if (count($parents) === 3) { unset($modelArray[$parents[0]][$parents[1]][$parents[2]]); $entity = []; unset($this->entities[$matches[1]][$matches[2]]); } return; } $i = $this::NO_VALUE_FILL; } /** * Tranformation !!! */ if (isset($transformArray[$i])) { $i = $transformArray[$i]; } elseif (isset($transformArray['~'])) { $i = $transformArray['~']; } /** * arithmetic !! */ if (count($arithmeticArray) > 0) { switch ($arithmeticArray['op']) { case '+': $i += $arithmeticArray['val']; break; case '-': $i -= $arithmeticArray['val']; break; case '*': $i = (int) $i * $arithmeticArray['val']; break; case '/': $i = (int) $i / $arithmeticArray['val']; break; } } }); // //yup that's pretty much it :) return $modelArray; } /** * * @return stdObject, recursivly casted outputArray */ public function outputObject() { return json_decode(json_encode($this->outputArray())); } /** * output a string of raw XML string */ public function outputXML($escape = TRUE) { return Helpers::arrayToXml($this->outputArray()); } /** * */ function hydrateEntitesFromDatas(Array $datas) { /* * Get translation yml into Array */ $modelArray = yaml_parse_file($this->modelAbsolutePath); return $modelArray; /* * Translate each value recursivly (allow tree) */ array_walk_recursive($modelArray, function($i, $key) use ($datas) { $orm = ORM::getInstance(); $fieldInfos = explode('.', $i); /* * if EntityName.value => it must be an entity * Else it's a plain value (implent Callable?) */ if (count($fieldInfos) < 2) { $this->datas[$i] = $datas[$key]; return; } $setter = 'set' . $orm->camelize($fieldInfos[1], TRUE); if (@method_exists($this->entities[$fieldInfos[0]], $setter)) { /** * @todo If (not sure) user input value, check for injection */ $this->entities[$fieldInfos[0]]->$setter($datas[$key]); } }); // //yup that's pretty much it :) return $this; } /** * @return Array */ public function __debug() { $modelArray = yaml_parse_file($this->modelAbsolutePath); $entNeeded = []; foreach ($modelArray as $field) { $entNeeded[strstr($field, '.', TRUE)] = strstr($field, '.', TRUE); } return ['entities Needed' => $entNeeded]; //yay } /** * @return Array de dimension +n */ private function unstackArray($key, $array, $datas) { var_dump($key); var_dump($array); var_dump($datas); return; } }