/** * Function to use other defined abstract methods to * use a standard naming-convention based method of * building classes by the factory * * @return mixed - anything a subclass factory can build */ public function build() { //get the driver to build $driver = $this->getDriverConfigValue(); //return null if there's nothing to build. if (is_null($driver)) { return null; } //get the prefix, suffix and namespace for the driver class $prefix = $this->getDriverNamePrefix(); $suffix = $this->getDriverNameSuffix(); $ns = $this->getDriverNamespace(); //use naming convention to convert the driver name //into a fully quantified class name $klass = Str::studly($driver); $fqcn = "{$ns}\\{$prefix}{$klass}{$suffix}"; try { $refl = new \ReflectionClass($fqcn); } catch (\ReflectionException $e) { throw new \InvalidArgumentException("Unsupported driver [{$driver}] to load [{$fqcn}]"); } //make sure the driver implements the interface properly $interface = $this->getDriverInterface(); if (!$refl->implementsInterface($interface)) { throw new \InvalidArgumentException("Unsupported interface [{$driver}] must implement [{$interface}]"); } $instance = $refl->newInstanceArgs($this->getDriverArgumentsArray()); if (is_null($instance)) { throw new \InvalidArgumentException("Driver [{$driver}] failed to create an instance"); } return $instance; }
public function testStudly() { $this->assertEquals("FooBarBizBang", Str::studly("foo_bar_biz_bang")); }