/** * Converts a blueprint array into a persisted object * @param array $blueprint * @return object */ public function create($blueprint) { $name = $blueprint->name; $type = $blueprint->type; if (!isset($this->count[$name])) { $this->count[$name] = 0; } $count = ++$this->count[$name]; $values = $blueprint->values(); $strings = array_map(function ($value) use($count) { return str_replace('#{sn}', str_pad($count, 4, '0', STR_PAD_LEFT), $value); }, $blueprint->strings()); $relationships = $blueprint->relationships(); foreach ($relationships as $key => $relationship) { $relationships[$key] = $relationship->create($blueprint->persisted); } $values = array_merge($values, $strings, $relationships); $dependencies = array_map(function ($value) use($values) { return $value->meet($values); }, $blueprint->dependencies()); $values = array_merge($values, $dependencies); //$object = $this->toObject($name, $values); $object = $this->to_object($name, $values); // @deprecated Backwards compatibility if (false == $blueprint->persisted) { return $object; } \Phactory::triggers()->beforeSave($name, $type, $object); //$object = $this->saveObject($name, $object); $object = $this->save_object($name, $object); // @deprecated Backwards compatibility \Phactory::triggers()->afterSave($name, $type, $object); return $object; }
/** * @covers \Phactory\Triggers::before_save * @covers \Phactory\Triggers::after_save */ public function testTriggers() { Phactory::builder(new DeprecatedCustomBuilder()); Phactory::triggers(new DeprecatedFrameworkTriggers()); Phactory::fixtures()->set_fixture('employer', 'unknown', array('first_name' => 'Anonymous', 'last_name' => 'Employer', 'email' => '*****@*****.**')); $employer = Phactory::employer(); $this->assertTrue($employer->beforeSave); $this->assertTrue($employer->afterSave); }
public function testTriggersWithVariation() { Phactory::reset(); Phactory::triggers(new FrameworkTriggers()); $account = Phactory::account('system'); $this->assertEquals($account->id, 1); $this->assertTrue($account->beforeSave); $this->assertTrue($account->afterSave); $this->assertTrue($account->systemBeforeSave); $this->assertTrue($account->systemAfterSave); }
/** * Get triggers events caller. If it is not defined, it will be set. * @param object|null $triggers * @return \Phactory\Triggers */ public static function triggers($triggers = null) { if (is_object($triggers)) { self::$triggers = new Triggers($triggers); } return isset(self::$triggers) ? self::$triggers : (self::$triggers = new Triggers()); }