示例#1
0
 function cast($asType = "stdClass")
 {
     if ($asType == "stdClass") {
         return $this->o;
     } else {
         if (!class_exists($asType)) {
             $asType = "O\\" . $asType;
         }
         if (class_exists($asType)) {
             if (is_object($this->o)) {
                 $a = (array) $this->o;
                 /** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
                 $refl = new \O\ReflectionClass($asType);
                 $props = $refl->getProperties(ReflectionProperty::IS_STATIC | ReflectionProperty::IS_PUBLIC);
                 $result = new $asType();
                 // convert properties to the right type
                 foreach ($props as $prop) {
                     $propName = $prop->getName();
                     if (isset($a[$propName])) {
                         $result->{$propName} = convertType($a[$propName], $prop->getType());
                     }
                 }
                 return $result;
             } else {
                 return NULL;
             }
         } else {
             throw new \Exception("Unrecognized type: " . $asType);
         }
     }
 }
示例#2
0
文件: Validator.php 项目: naozone/phx
 /**
  * Validate a property value according to the rules on its comment
  *
  * Note: for array types (e.g. int[]) the validation is applied to each element,
  * but only if it cannot be applied to the array as a whole. For example,
  * @Min can be used on an int[] to validate each element,
  * but @Size cannot be used on a string[] except to validate the array's size.
  * @Valid will validate an object property recursively,
  * or validate each element in an object[].
  *
  * @param string|\O\ReflectionClass $class
  * @param string|\O\ReflectionProperty $property
  * @param mixed $value
  * @return \O\ConstraintViolation[]
  */
 static function validateValue($class, $property, $value)
 {
     $result = array();
     if (is_string($property)) {
         $class = new ReflectionClass($class);
         $property = $class->getProperty($property);
     }
     $converted = convertType($value, $property->getType());
     if (gettype($converted) != gettype($value) || $converted != $value) {
         $result[] = new ConstraintViolation("Property type mismatch", "type", NULL, $property->getName(), $value);
     }
     $constraints = self::getAnnotations($property->getDocComment(TRUE));
     foreach ($constraints as $constraint => $param) {
         if ($constraint == "Valid") {
             // recursive validation
             if (is_object($value)) {
                 $violations = self::validate($value);
                 foreach ($violations as $violation) {
                     $violation->propertyPath = $property->getName() . "." . $violation->propertyPath;
                     $result[] = $violation;
                 }
             } else {
                 if (is_array($value)) {
                     foreach ($value as $i => $item) {
                         if (is_object($item)) {
                             $violations = self::validate($item);
                             foreach ($violations as $violation) {
                                 $violation->propertyPath = $property->getName() . "[{$i}]." . $violation->propertyPath;
                                 $result[] = $violation;
                             }
                         }
                     }
                 }
             }
         } else {
             $fn = self::$constraints[$constraint];
             if (function_exists($fn)) {
                 if (!call_user_func($fn, $value, $param)) {
                     $msg = $constraint . " constraint violated";
                     if (function_exists($fn . "_Message")) {
                         $msg = call_user_func($fn . "_Message", $param);
                     }
                     $result[] = new ConstraintViolation($msg, $constraint, NULL, $property->getName(), $value);
                 }
             }
         }
     }
     return $result;
 }
示例#3
0
<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
include_once '../modele/functionSeeker.php';
include_once '../modele/layout.php';
include_once '../controleur/translator.php';
include_once '../controleur/interpreter.php';
if (isset($_POST['editor'])) {
    $langage = $_POST['langage'];
    $structure = getTranslationLayout(1, $langage);
    $structure = interpreting($structure);
    $codeComplet = '';
    if (isset($_POST['userFonction'])) {
        $listeFonction = $_POST['userFonction'];
        foreach ($listeFonction as $l) {
            $c = functionSeeker($l);
            $codeComplet = $codeComplet . $c[0]['contenu'];
        }
    }
    $currentCode = translation($codeComplet, $structure);
    $currentCode = convertType($currentCode, $langage);
    echo nl2br($currentCode);
}