* @license MIT */ class MagicSet extends MagicMethodGenerator { /** * Constructor * * @param ReflectionClass $originalClass * @param PropertyGenerator $initializerProperty * @param PropertyGenerator $valueHolderProperty * @param PublicPropertiesMap $publicProperties * * @throws \Zend\Code\Generator\Exception\InvalidArgumentException * @throws \InvalidArgumentException */ public function __construct( ReflectionClass $originalClass, PropertyGenerator $initializerProperty, PropertyGenerator $valueHolderProperty, PublicPropertiesMap $publicProperties ) { parent::__construct( $originalClass, '__set', [new ParameterGenerator('name'), new ParameterGenerator('value')] ); $hasParent = $originalClass->hasMethod('__set'); $initializer = $initializerProperty->getName(); $valueHolder = $valueHolderProperty->getName(); $callParent = ''; if (! $publicProperties->isEmpty()) { $callParent = 'if (isset(self::$' . $publicProperties->getName() . "[\$name])) {\n" . ' return ($this->' . $valueHolder . '->$name = $value);' . "\n}\n\n"; } $callParent .= $hasParent ? 'return $this->' . $valueHolder . '->__set($name, $value);' : PublicScopeSimulator::getPublicAccessSimulationCode( PublicScopeSimulator::OPERATION_SET, 'name', 'value', $valueHolderProperty ); $this->setBody( '$this->' . $initializer . ' && $this->' . $initializer . '->__invoke($this->' . $valueHolder . ', $this, ' . '\'__set\', array(\'name\' => $name, \'value\' => $value), $this->' . $initializer . ');' . "\n\n" . $callParent ); } }