/** * Decodes a class into a ClassObject class * @param string $classString The class to decode. The full namespace must be passed in * @return ClassObject * @throws ClassNotFoundException */ public function decodeClass($classString) { if (!class_exists($classString)) { throw new ClassNotFoundException("Class with name: '{$classString}' not found"); } // Check the cache first and return if it exists if ($this->cache !== null) { if (false !== ($cachedClassObject = $this->cache->getClass($classString))) { return $cachedClassObject; } } /** @var PropertyObject[] $properties */ $properties = []; $reflectedClass = new \ReflectionClass($classString); // Create class with config $classObject = $this->createClassObject($reflectedClass); // Decode public methods foreach ($reflectedClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) { if (!$this->decodeMethod($method, $properties)) { $this->decodeConstructor($method, $classObject); } } // Decode public properties /** @var \ReflectionProperty $property */ foreach ($reflectedClass->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) { $this->decodeProperty($property, $properties); } $classObject->setProperties($properties); // Set in the cache if ($this->cache !== null) { $this->cache->setClass($classString, $classObject); } // Return Object return $classObject; }
/** * @expectedException \PhpJsonMarshaller\Exception\CacheCollisionException */ public function testExceptionOnCacheCollision() { $classObject = $this->decoder->decodeClass('\\PhpJsonMarshallerTests\\ExampleClass\\User'); $this->cache->setClass('\\PhpJsonMarshallerTests\\ExampleClass\\User', $classObject); $this->cache->setClass('\\PhpJsonMarshallerTests\\ExampleClass\\User', $classObject); }