public function setMyPropertie($object = null) { if (null === $this->_meta_self_reflection) { $this->_meta_self_reflection = new \ReflectionObject($this); } $this->freeze = false; if (is_array($object) || is_object($object)) { // $access = \\Symfony\Component\PropertyAccess\PropertyAccess::getPropertyAccessor() ; foreach ($object as $property => $value) { if (null === $value) { continue; } $set = 'set_' . $property; try { if (method_exists($this, $set)) { if ($this->{$set}($value)) { $this->throwError("%s already seted", $property); } } else { if (!$this->_meta_self_reflection->hasProperty($property)) { $this->throwError("%s not exists", $property); } $_set = 'set' . $this->camelize($property); if (method_exists($this, $_set)) { throw new \Exception(sprintf("%s->%s method %s should change to %s", $this->getMeteTypeName(), $property, $_set, $set)); } if (0 === strpos($property, '_')) { $this->throwError("%s is private property", $property); } $_property = $this->_meta_self_reflection->getProperty($property); if ($_property->isStatic()) { $this->throwError("%s is static property", $property); } if ($_property->isPrivate()) { $this->throwError("%s is private property", $property); } /* $_property->setAccessible( true ) ; $_property->setValue( $this, $value ) ; */ $this->{$property} = $value; } $this->_annot_properties[$property] = true; } catch (\Symforce\AdminBundle\Compiler\MetaType\Exception $e) { if ($this instanceof \Symforce\AdminBundle\Compiler\MetaType\DoctrineType || $this instanceof \Symforce\AdminBundle\Compiler\MetaType\DoctrineProperty) { $this->throwPropertyError($property, $e->getMessage()); } else { throw $e; } } } } else { if (!empty($object)) { throw new \Exception(sprintf("%s can not setValue(%s)", $this->getClassName(), $value)); } } $this->freeze = true; }
/** * @param \ReflectionObject $reflectionObject * @param string $key * @return null|\ReflectionProperty */ private static function findProperty(\ReflectionObject $reflectionObject, $key) { if ($reflectionObject->hasProperty($key)) { return $reflectionObject->getProperty($key); } $camelized = lcfirst(join(array_map('ucfirst', explode('_', $key)))); if ($reflectionObject->hasProperty($camelized)) { return $reflectionObject->getProperty($camelized); } return null; }
/** * @param string $property * @param mixed $value */ private function setProperty($property, $value) { $reflection = new \ReflectionObject($this); if (!$reflection->hasProperty($property) || !$reflection->getProperty($property)->isPrivate()) { $this->{$property} = $value; } }
protected function __construct() { $config = Hayate_Config::getInstance(); if ((!$config->get('view', false) || !isset($config->core->view['smarty_dir'])) && !class_exists('Smarty', false)) { throw new Hayate_View_Exception(_('smarty_dir configuration parameter missing.')); } if (!class_exists('Smarty', false)) { require_once rtrim($config->core->view['smarty_dir'], '\\/') . '/Smarty.class.php'; } // finally we can instantiate $this->smarty = new Smarty(); // and set the properties values $ro = new ReflectionObject($this->smarty); foreach ($config->core->view as $prop => $val) { if ($ro->hasProperty($prop)) { $this->smarty->{$prop} = $val; } } /* if (version_compare($this->smarty->_version, '3.0') < 0) { $this->is_smarty_2 = true; } */ }
public static function getPropertyValue($object, $propertyName) { $refClass = new \ReflectionObject($object); if ($refClass->hasProperty($propertyName)) { $property = $refClass->getProperty($propertyName); $property->setAccessible(true); return $property->getValue($object); } }
/** * (non-PHPdoc) * @see CIUnit_Framework_ConstraintAbstract::matches() */ protected function matches($value) { $object = new ReflectionObject($value); if ($object->hasProperty($this->attribute)) { $attribute = $object->getProperty($this->attribute); return $attribute->isStatic(); } return FALSE; }
/** * iteratest through properties to set values on the stub that exists on the stub class. If $force is true, * will set the value regardless of whether or not the property exists on the element. * * @param $stub * @param array $properties * @param bool $force */ public static function propertiesSetAndMatch($stub, array $properties = array(), $force = false) { $rf_obj = new ReflectionObject($stub); foreach ($properties as $name => $value) { if ($force || $rf_obj->hasProperty($name)) { $stub->{$name} = $value; } } $stub->__phpunit_getInvocationMocker()->addMatcher(new ComponentStubMatcher($properties)); }
public function run() { global $request; $em = \Shared\DoctrineHelper::getEntityManager(); $idPlayer = $this->objPlayer->getId(); $codeVerif = $request->request->get("numeroVerif"); $objSuppressionPersonnage = \Site\SiteHelper::getSuppressionPersonnageRepository()->findByIdPlayerAndNumeroVerif($this->objPlayer->getId(), $codeVerif); if ($objSuppressionPersonnage !== null) { if (\Player\PlayerHelper::haveGuild($idPlayer) == false) { $objPlayerIndex = \Player\PlayerHelper::getPlayerIndexRepository()->find($this->objAccount->getId()); if ($objPlayerIndex->getPid1() == $idPlayer) { $func = "setPid1"; } else { if ($objPlayerIndex->getPid2() == $idPlayer) { $func = "setPid2"; } else { if ($objPlayerIndex->getPid3() == $idPlayer) { $func = "setPid3"; } else { if ($objPlayerIndex->getPid4() == $idPlayer) { $func = "setPid4"; } } } } $objPlayerIndex->{$func}(0); $em->persist($objPlayerIndex); \Player\PlayerHelper::getMarriageRepository()->deleteByIdPlayer($idPlayer); \Player\PlayerHelper::getItemRepository()->deleteByOwnerId($idPlayer, ["EQUIPMENT", "INVENTORY"]); $objPlayerDelete = new \Player\Entity\PlayerDeleted(); $oldReflection = new \ReflectionObject($this->objPlayer); $newReflection = new \ReflectionObject($objPlayerDelete); foreach ($oldReflection->getProperties() as $property) { if ($newReflection->hasProperty($property->getName())) { $newProperty = $newReflection->getProperty($property->getName()); $newProperty->setAccessible(true); $newProperty->setValue($objPlayerDelete, $property->getValue($this->objPlayer)); } } $em->persist($objPlayerDelete); \Player\PlayerHelper::getMessengerListRepository()->deleteByNamePlayer($this->objPlayer->getName()); $em->remove($this->objPlayer); $em->remove($objSuppressionPersonnage); $em->flush(); $Tableau_Retour_Json = array('result' => true); } else { $em->remove($objSuppressionPersonnage); $em->flush(); $Tableau_Retour_Json = array('result' => false, 'reasons' => "Le joueur est membre ou chef d'une guilde."); } } else { $Tableau_Retour_Json = array('result' => false, 'reasons' => "Le code de vérification n'est pas le bon."); } echo json_encode($Tableau_Retour_Json); }
/** * Sets the property of an object: It does not matter whether the property * is public, private or protected * * @param string $property * @param mixed $value * @param $object * @return void */ public static function set($property, $value, $object) { $reflection = new \ReflectionObject($object); if ($reflection->hasProperty($property)) { $property = $reflection->getProperty($property); $property->setAccessible(true); $property->setValue($object, $value); } else { $object->{$property} = $value; } }
/** * Implementation of __call * Adds getter and setter methods for all non-private properties. * If a set-Operation for a non-private property is called, the change * will be tracked. * * @param $name * @param $arguments * @return mixed */ function __call($name, $arguments) { if (strpos($name, 'get') === 0) { $propertyName = lcfirst(substr($name, 3)); if (property_exists($this, $propertyName)) { return $this->{$propertyName}; } return null; } if (strpos($name, 'set') === 0 && sizeof($arguments) == 1) { $propertyName = lcfirst(substr($name, 3)); // never allow setting of private or protected properties if (!$this->initialReflectionObject->hasProperty($propertyName) || $this->initialReflectionObject->getProperty($propertyName)->isPublic()) { // do nothing if nothing changed if (!property_exists($this, $propertyName) || $this->{$propertyName} != $arguments[0]) { $this->{$propertyName} = $arguments[0]; $this->propertyChanges[] = $propertyName; } } } }
/** * Заполнить сущность данными * * @param mixed $entity Сущность, которую нужно заполнить * @param array $data данные, которыми нужно заполнить сущность. Ключи - имена свойств, значения - значения */ public function fillEntity($entity, array $data) { $object = new \ReflectionObject($entity); foreach ($data as $key => $value) { $propertyName = $this->createPropertyName($key); if (!$object->hasProperty($propertyName)) { continue; } $property = $object->getProperty($propertyName); $property->setAccessible(true); $property->setValue($entity, $value); } }
/** * @inheritdoc */ public function getPostProcessInstance($postProcessName, array $parameters = []) { $postProcessClass = 'A7\\PostProcessors\\' . $postProcessName; $postProcessObject = new $postProcessClass(); $postProcessReflectionObject = new \ReflectionObject($postProcessObject); if ($postProcessReflectionObject->hasProperty('a7')) { $a7Property = $postProcessReflectionObject->getProperty('a7'); $a7Property->setAccessible(true); $a7Property->setValue($postProcessObject, $this->a7); } if ($postProcessReflectionObject->hasProperty('annotationManager')) { $annotationManagerProperty = $postProcessReflectionObject->getProperty('annotationManager'); $annotationManagerProperty->setAccessible(true); $annotationManagerProperty->setValue($postProcessObject, $this->annotationManager); } if ($postProcessReflectionObject->hasProperty('parameters')) { $annotationManagerProperty = $postProcessReflectionObject->getProperty('parameters'); $annotationManagerProperty->setAccessible(true); $annotationManagerProperty->setValue($postProcessObject, $parameters); } return $postProcessObject; }
/** * {@inheritdoc} */ protected function tryReadOption($name, &$out, $default = null) { $success = false; $name = $this->prefix . $name; $out = $default; if (!$this->classOrObj) { return false; } if (isset($this->cache[$name]) || array_key_exists($name, $this->cache)) { $out = $this->cache[$name]; $success = true; } elseif ($this->reflector->hasProperty($name)) { $out = $this->readFromProperty($name); $success = true; $this->cache[$name] = $out; } elseif ($this->reflector->hasMethod($name)) { $out = $this->readFromMethod($name); $success = true; $this->cache[$name] = $out; } return $success; }
public function assign($document, $object) { $reflector = new ReflectionObject($object); foreach ($document as $name => $value) { if ($reflector->hasProperty($name)) { $attribute = $reflector->getProperty($name); $attribute->setAccessible(TRUE); $attribute->setValue($object, $value); } else { $object->{$name} = $value; } } return $object; }
/** * @param array $data * @param $object * @return mixed */ public function hydrate(array $data, $object) { $reflection = new \ReflectionObject($object); foreach ($data as $name => $value) { $propertyName = $this->namingStrategy ? $this->namingStrategy->convert($name) : $name; if ($reflection->hasProperty($propertyName)) { $property = $reflection->getProperty($propertyName); if ($property->isPublic()) { $property->setValue($object, $value); } } } return $object; }
/** * Bind all the variables into this object, * but ONLY if it's already declared * * @param array $variables */ public function bind($variables) { $reflect = new ReflectionObject($this); $properties = $reflect->getProperties(); foreach ($variables as $key => $var) { // Protected from alteration! if ($key == 'constraints' || $key == 'requiredFields' || $key == 'searchableFields') { continue; } if ($reflect->hasProperty($key) != null) { $reflect->getProperty($key)->setValue($this, $var); } } }
/** * Internal helper function for mass-assignment of values to a doctrie object * * If the function declares a method named set */ protected function assignValuesToInst($oi, array $values = [], $checkIfDeclared = true) { $reflection = new \ReflectionObject($oi); foreach ($values as $p => $v) { $setter = 'set' . ucfirst($p); if ($reflection->hasMethod($setter)) { $oi->{$setter}($v); } elseif ($reflection->hasProperty($p) || !$checkIfDeclared) { $oi->{$p} = $v; } else { throw new \yii\base\InvalidConfigException('Doctrine object of class ' . get_class($oi) . ' does not declare ' . $setter . ' or $' . $p); } } }
public static final function convertSoapFault(SoapFault $e) { $r = new \ReflectionObject($e); if (!$r->hasProperty('detail') || !$e->detail instanceof \stdClass) { return $e; } $r = new \ReflectionObject($e->detail); if ($r->hasProperty('exception') && $e->detail->exception instanceof \SoapVar) { $exception = $e->detail->exception->enc_value; Assert::isInstance($exception, BaseException::class); return $exception; } return $e; }
/** * {@inheritDoc} */ protected function doUpdateModel($model) { $ro = new \ReflectionObject($model); if (false == $ro->hasProperty($this->idProperty)) { $model->{$this->idProperty} = null; } $rp = new \ReflectionProperty($model, $this->idProperty); $rp->setAccessible(true); $id = $rp->getValue($model); if (false == $id) { $rp->setValue($model, $id = uniqid()); } $rp->setAccessible(false); file_put_contents($this->storageDir . '/payum-model-' . $id, serialize($model)); }
/** * Object factory properties helper * * @param object $object * @param mixed $props (array|object) * @param boolean $use_prop_must_exist * @return void */ function factory_props(&$object, $props, $use_prop_must_exist = true) { if (is_object($props)) { $props = (array) $props; } foreach ($props as $k => $v) { if ($use_prop_must_exist) { $r = new \ReflectionObject($object); if (!$r->hasProperty($k) || !$r->getProperty($k)->isPublic()) { continue; } } $object->{$k} = $v; } }
public static function hydrateObject($entityClass, $data) { $reader = new AnnotationReader(); $reflectionObj = new \ReflectionObject(new $entityClass()); $aAnnotations = array(); foreach ($data as $key => $value) { $property = Inflector::camelize($key); if ($reflectionObj->hasProperty($property)) { $reflectionProp = new \ReflectionProperty($entityClass, $property); $relation = $reader->getPropertyAnnotation($reflectionProp, 'Weysan\\DoctrineImgBundle\\Annotations\\ImgResize'); if ($relation) { $aAnnotations[] = array(0 => $relation->getVars(), 1 => $property); } } } return $aAnnotations; }
/** * Find if XML node has any rewrites and if so append them into list * * @param Bronto_Verify_Model_Core_Config_Element $config XML Node * @param array &$rewrites existing rewrites * * @return void * @access protected */ protected function _findRewrites(Bronto_Verify_Model_Core_Config_Element $config, &$rewrites = array()) { $reflect = new ReflectionObject($config); $props = $reflect->getProperties(ReflectionProperty::IS_PUBLIC); foreach ($props as $prop) { $module = $prop->getName(); $reflect = new ReflectionObject($config->{$module}); if ($reflect->hasProperty('rewrite')) { $rewrite = new ReflectionObject($config->{$module}->rewrite); $properties = $rewrite->getProperties(ReflectionProperty::IS_PUBLIC); foreach ($properties as $property) { $class = $property->name; $rewrites[$this->_type][$module][$class][] = (string) $config->{$module}->rewrite->{$class}; } } } }
public function __invoke($var) { $data = file_get_contents($this->path); if (!$data) { throw new \RuntimeException(sprintf('Failed to read file at path %s!', $this->path)); } $yaml = Yaml::parse($data); $keys = array_keys($yaml); $yaml = $yaml[$keys[0]]; if (!$yaml) { throw new \InvalidArgumentException('No config at ' . $this->path); } $hasPolicy = array_key_exists('exclusion_policy', $yaml); if (!$hasPolicy || $hasPolicy && 'ALL' !== $yaml['exclusion_policy']) { throw new \RuntimeException(sprintf('This serializer supports only ALL, %s given!', $yaml['exclusion_policy'])); } $ref = new \ReflectionObject($var); $result = []; $properties = array_key_exists('properties', $yaml) ? $yaml['properties'] : array(); foreach ($properties as $key => $config) { if ($config['expose'] !== true) { continue; } $name = array_key_exists('serialized_name', $config) ? $config['serialized_name'] : $key; if ($ref->hasProperty($key)) { $prop = $ref->getProperty($key); $prop->setAccessible(true); $result[$name] = $prop->getValue($var); continue; } $method = 'get' . ucfirst($key); if (method_exists($var, $method)) { $result[$name] = call_user_func_array([$var, $method], []); continue; } // no method or property found, skip current key } $virtual = array_key_exists('virtual_properties', $yaml) ? $yaml['virtual_properties'] : array(); foreach ($virtual as $method => $config) { $result[$config['serialized_name']] = call_user_func_array([$var, $method], []); } return $result; }
public function thaw(array $frozenObject, $root = NULL, array &$objects = array()) { // Bail out if one of the required classes cannot be found. foreach ($frozenObject['objects'] as $object) { if (!class_exists($object['className'], $this->useAutoload)) { throw new RuntimeException(sprintf('Class "%s" could not be found.', $object['className'])); } } // By default, we thaw the root object and (recursively) // its aggregated objects. if ($root === NULL) { $root = $frozenObject['root']; } // Thaw object (if it has not been thawed before). if (!isset($objects[$root])) { $className = $frozenObject['objects'][$root]['className']; $state = $frozenObject['objects'][$root]['state']; // Use a trick to create a new object of a class // without invoking its constructor. $objects[$root] = unserialize(sprintf('O:%d:"%s":0:{}', strlen($className), $className)); // Handle aggregated objects. $this->thawArray($state, $frozenObject, $objects); $reflector = new \ReflectionObject($objects[$root]); foreach ($state as $name => $value) { if (strpos($name, '__php_object_freezer') !== 0) { if ($reflector->hasProperty($name)) { $attribute = $reflector->getProperty($name); $attribute->setAccessible(TRUE); $attribute->setValue($objects[$root], $value); } else { $objects[$root]->{$name} = $value; } } } // Store UUID. $objects[$root]->__php_object_freezer_uuid = $root; // Store hash. if (isset($state['__php_object_freezer_hash'])) { $objects[$root]->__php_object_freezer_hash = $state['__php_object_freezer_hash']; } } return $objects[$root]; }
/** * Return Widgets Insertion Plugin Window URL * * @param Varien_Object Editor element config * @return string */ public function getWidgetWindowUrl($config) { $params = array(); $skipped = is_array($config->getData('skip_widgets')) ? $config->getData('skip_widgets') : array(); if ($config->hasData('widget_filters')) { $all = Mage::getModel('Mage_Widget_Model_Widget')->getWidgetsXml(); $filtered = Mage::getModel('Mage_Widget_Model_Widget')->getWidgetsXml($config->getData('widget_filters')); $reflection = new ReflectionObject($filtered); foreach ($all as $code => $widget) { if (!$reflection->hasProperty($code)) { $skipped[] = $widget->getAttribute('type'); } } } if (count($skipped) > 0) { $params['skip_widgets'] = $this->encodeWidgetsToQuery($skipped); } return Mage::getSingleton('Mage_Backend_Model_Url')->getUrl('*/widget/index', $params); }
public function __construct($config) { $Ref = new ReflectionObject($this); foreach ($config as $k => $v) { if ($Ref->hasProperty($k)) { $this->{$k} = $v; } } if (!($this->tld || $this->Registry)) { throw new RegisterDomainAction_Exception('$tld or $Registry must be defined'); } if (!(isset($this->clid_list) || isset($this->contact_list))) { throw new RegisterDomainAction_Exception('$clid_list or $contact_list must be defined'); } if (!(isset($this->ns_list) || isset($this->nameserver_list))) { throw new RegisterDomainAction_Exception('$ns_list or $nameserver_list must be defined'); } $this->Init(); }
/** * @param object $name * @param string $name * @param mixed $value */ public static function setProperty($object, $name, $value) { if (!is_object($object)) { throw new \InvalidArgumentException('$object must be an object'); } // First, we try to use the getter $setter = 'set' . ucfirst($name); if (is_callable([$object, $setter])) { return $object->{$setter}($value); } // ... or we try to access the property if it's public $reflection = new \ReflectionObject($object); if ($reflection->hasProperty($name) && $reflection->getProperty($name)->isPublic()) { return $object->{$name} = $value; } // ... or we use _getProperty on DomainObjects if ($object instanceof AbstractDomainObject && $object->_hasProperty($name)) { return $object->_setProperty($name, $value); } throw new PropertyNotAccessibleException($name); }
/** * Injects $dependency into property $name of $target * * This is a convenience method for setting a protected or private property in * a test subject for the purpose of injecting a dependency. * * @param object $target The instance which needs the dependency * @param string $name Name of the property to be injected * @param mixed $dependency The dependency to inject – usually an object but can also be any other type * @return void * @throws \RuntimeException * @throws \InvalidArgumentException */ protected function inject($target, $name, $dependency) { if (!is_object($target)) { throw new \InvalidArgumentException('Wrong type for argument $target, must be object.'); } $objectReflection = new \ReflectionObject($target); $methodNamePart = strtoupper($name[0]) . substr($name, 1); if ($objectReflection->hasMethod('set' . $methodNamePart)) { $methodName = 'set' . $methodNamePart; $target->{$methodName}($dependency); } elseif ($objectReflection->hasMethod('inject' . $methodNamePart)) { $methodName = 'inject' . $methodNamePart; $target->{$methodName}($dependency); } elseif ($objectReflection->hasProperty($name)) { $property = $objectReflection->getProperty($name); $property->setAccessible(true); $property->setValue($target, $dependency); } else { throw new \RuntimeException('Could not inject ' . $name . ' into object of type ' . get_class($target)); } }
/** * Getter and Setters * * @param string $name * @param array $args */ public function __call($name, array $args) { if (in_array(substr($name, 0, 3), array('get', 'set'))) { $property = lcfirst(substr($name, 3)); $ref = new \ReflectionObject($this); if ($ref->hasProperty($property)) { $property = $ref->getProperty($property); $property->setAccessible(true); switch (substr($name, 0, 3)) { case 'get': return $property->getValue($this); case 'set': default: $property->setValue($this, $args[0]); return $this; } } throw new \InvalidArgumentException(sprintf('Property %s does not exists', $property)); } throw new \InvalidArgumentException('Method does not exists'); }
public function run() { global $request; $em = \Shared\DoctrineHelper::getEntityManager(); $idAccount = $request->request->get("idAccount"); if ($idAccount > 0) { $objAccount = \Account\AccountHelper::getAccountRepository()->find($idAccount); if ($objAccount !== null) { $objAccount->setStatus(\StatusHelper::ACTIF); $em->persist($objAccount); $objBannissementsActif = \Site\SiteHelper::getBannissementsActifsRepository()->findByIdAccount($objAccount->getId()); if ($objBannissementsActif !== null) { $objHistoriqueBannissement = new \Site\Entity\HistoriqueBanissements(); $oldReflection = new \ReflectionObject($objBannissementsActif); $newReflection = new \ReflectionObject($objHistoriqueBannissement); foreach ($oldReflection->getProperties() as $property) { if ($newReflection->hasProperty($property->getName())) { $newProperty = $newReflection->getProperty($property->getName()); $newProperty->setAccessible(true); $newProperty->setValue($objHistoriqueBannissement, $property->getValue($objBannissementsActif)); } } $objHistoriqueBannissement->setDebannPar($this->objAccount->getId()); $em->persist($objHistoriqueBannissement); $em->remove($objBannissementsActif); } $em->flush(); $template = $this->objTwig->loadTemplate("Debannissement.html5.twig"); $result = $template->render(["compte" => $objAccount->getLogin()]); $subject = 'VamosMt2 - Levé du bannissement de ' . $objAccount->getLogin() . ''; \EmailHelper::sendEmail($objAccount->getEmail(), $subject, $result); $result = array('result' => true, 'reasons' => ""); } else { $result = array('result' => false, 'reasons' => "Le compte sélectionné n'existe pas."); } } else { $result = array('result' => false, 'reasons' => "Problème de transmission d'identifiant."); } echo json_encode($result); }