/** * @param DataMapper $mapper * @param UnitType[] $types * @return StockUnit[] */ public function createDefaultUnit(DataMapper $mapper, array $types) { $this->assert($mapper->getEntityClass() == StockUnit::class); $unitGroup = ['Berat' => [['name' => 'kg', 'description' => 'Kilogram', 'ratio' => 1], ['name' => 'g', 'description' => 'Gram', 'ratio' => 0.001], ['name' => 'mg', 'description' => 'Milligram', 'ratio' => 1.0E-6], ['name' => 'pon', 'description' => 'Pon', 'ratio' => 0.453592], ['name' => 'ons', 'description' => 'Ons', 'ratio' => 0.0283495], ['name' => 'kuintal', 'description' => 'Kuintal', 'ratio' => 100], ['name' => 'ton', 'description' => 'Ton', 'ratio' => 1000]], 'Volume' => [['name' => 'l', 'description' => 'Liter', 'ratio' => 1], ['name' => 'ml', 'description' => 'Milliliter', 'ratio' => 0.001], ['name' => 'gallon', 'description' => 'Gallon', 'ratio' => 19]], 'Unit' => [['name' => 'unit', 'description' => 'Unit', 'ratio' => 1], ['name' => 'lusin', 'description' => 'Lusin', 'ratio' => 12], ['name' => 'box', 'description' => 'Box', 'ratio' => 1], ['name' => 'btl', 'description' => 'Botol', 'ratio' => 1], ['name' => 'kaleng', 'description' => 'Kaleng', 'ratio' => 1], ['name' => 'bungkus', 'description' => 'Bungkus', 'ratio' => 1], ['name' => 'pak', 'description' => 'Pak', 'ratio' => 1], ['name' => 'sak', 'description' => 'Sak', 'ratio' => 1], ['name' => 'pcs', 'description' => 'Pcs', 'ratio' => 1], ['name' => 'saset', 'description' => 'Saset', 'ratio' => 1], ['name' => 'item', 'description' => 'Item', 'ratio' => 1], ['name' => 'buah', 'description' => 'Buah', 'ratio' => 1], ['name' => 'biji', 'description' => 'Biji', 'ratio' => 1], ['name' => 'sdm', 'description' => 'sdm', 'ratio' => 1], ['name' => 'sdt', 'description' => 'sdt', 'ratio' => 1]]]; $units = []; foreach ($types as $type) { if (isset($unitGroup[$type->getName()])) { foreach ($unitGroup[$type->getName()] as $spec) { $units[] = $mapper->create($spec + compact('type')); } } } return $units; }
/** * @param DataMapper $accountMapper * @param DataMapper $userMapper * @param FirstRunForm $form * @return Account */ public function setupFirstRun(DataMapper $accountMapper, DataMapper $userMapper, FirstRunForm $form) { $this->assert($accountMapper->getEntityClass() == Account::class); $this->assert($userMapper->getEntityClass() == User::class); /** @var PasswordInterface $hasher */ $hasher = $this->getServiceLocator()->get(PasswordInterface::class); $data = $form->getData(); $user = $userMapper->create(['name' => $data['user_name'], 'email' => $data['user_email'], 'password_changed' => true, 'password' => $hasher->create($data['password']), 'status' => User::STATUS_ACTIVE]); /** @var Account $account */ $account = $accountMapper->getHydrator()->hydrate($data, new Account()); $account->setOwner($user); $account->setBillingUser($user); $accountMapper->create($account); return $account; }
/** * @param DataMapper $mapper * @param User $user * @param string $type * @return Token */ public function createToken(DataMapper $mapper, User $user, $type) { $this->assert($mapper->getEntityClass() == Token::class); $code = preg_replace('/[^a-zA-Z0-9\\_\\-]+/', null, Rand::getString(100)); return $mapper->create(['user' => $user, 'code' => sprintf('%d.%d.%s', $user->getId(), time(), $code), 'type' => $type]); }