ShopMemberFactory::create PHP 메소드

create() 공개 메소드

Data must contain unique identifier.
public create ( $data ) : Member | boolean
$data - map of member data
리턴 Member | boolean - new member (not saved to db), or false if there is an error.
    public function create($data)
    {
        $result = ValidationResult::create();
        if (!Checkout::member_creation_enabled()) {
            $result->error(_t("Checkout.MembershipIsNotAllowed", "Creating new memberships is not allowed"));
            throw new ValidationException($result);
        }
        $idfield = Config::inst()->get('Member', 'unique_identifier_field');
        if (!isset($data[$idfield]) || empty($data[$idfield])) {
            $result->error(_t('Checkout.IdFieldNotFound', 'Required field not found: {IdentifierField}', 'Identifier is the field that holds the unique user-identifier, commonly this is \'Email\'', array('IdentifierField' => $idfield)));
            throw new ValidationException($result);
        }
        if (!isset($data['Password']) || empty($data['Password'])) {
            $result->error(_t("Checkout.PasswordRequired", "A password is required"));
            throw new ValidationException($result);
        }
        $idval = $data[$idfield];
        if ($member = ShopMember::get_by_identifier($idval)) {
            // get localized field labels
            $fieldLabels = $member->fieldLabels(false);
            // if a localized value exists, use this for our error-message
            $fieldLabel = isset($fieldLabels[$idfield]) ? $fieldLabels[$idfield] : $idfield;
            $result->error(_t('Checkout.MemberExists', 'A member already exists with the {Field} {Identifier}', '', array('Field' => $fieldLabel, 'Identifier' => $idval)));
            throw new ValidationException($result);
        }
        $member = Member::create(Convert::raw2sql($data));
        // 3.2 changed validate to protected which made this fall through the DataExtension and error out
        $validation = $member->hasMethod('doValidate') ? $member->doValidate() : $member->validate();
        if (!$validation->valid()) {
            //TODO need to handle i18n here?
            $result->error($validation->message());
        }
        if (!$result->valid()) {
            throw new ValidationException($result);
        }
        return $member;
    }

Usage Example

 public function testPlaceOrder()
 {
     //place items in cart
     $this->shoppingcart->add($this->mp3player, 2);
     $this->shoppingcart->add($this->socks);
     $order = $this->shoppingcart->current();
     $factory = new ShopMemberFactory();
     $member = $factory->create(array('FirstName' => 'James', 'Surname' => 'Brown', 'Email' => '*****@*****.**', 'Password' => 'jbrown'));
     $this->assertTrue((bool) $member);
     $member->write();
     $order->calculate();
     //submit checkout page
     $this->assertTrue($this->placeOrder('James', 'Brown', '*****@*****.**', '23 Small Street', 'North Beach', 'Springfield', 'Waikato', '1234567', 'NZ', 'jbrown', 'jbrown', $member), "Order placed sucessfully");
     $order = Order::get()->byID($order->ID);
     //update order variable to db-stored version
     $this->assertFalse($this->shoppingcart->current(), "Shopping cart is empty");
     $this->assertNotNull($order);
     $this->assertEquals(408, $order->GrandTotal(), 'grand total');
     $this->assertEquals(408, $order->TotalOutstanding(), 'total outstanding');
     $this->assertEquals(0, $order->TotalPaid(), 'total outstanding');
     $this->assertEquals($order->Status, 'Unpaid', 'status is "unpaid"');
     $this->assertEquals(false, $order->IsSent());
     $this->assertEquals(false, $order->IsProcessing());
     $this->assertEquals(false, $order->IsPaid());
     $this->assertEquals(false, $order->IsCart());
     $this->assertEquals('James', $order->FirstName, 'order first name');
     $this->assertEquals('Brown', $order->Surname, 'order surname');
     $this->assertEquals('*****@*****.**', $order->Email, 'order email');
     $shippingaddress = $order->ShippingAddress();
     $this->assertEquals('23 Small Street', $shippingaddress->Address, 'order address');
     $this->assertEquals('North Beach', $shippingaddress->AddressLine2, 'order address2');
     $this->assertEquals('Springfield', $shippingaddress->City, 'order city');
     $this->assertEquals('1234567', $shippingaddress->PostalCode, 'order postcode');
     $this->assertEquals('NZ', $shippingaddress->Country, 'order country');
     $billingaddress = $order->BillingAddress();
     $this->assertEquals('23 Small Street', $billingaddress->Address, 'order address');
     $this->assertEquals('North Beach', $billingaddress->AddressLine2, 'order address2');
     $this->assertEquals('Springfield', $billingaddress->City, 'order city');
     $this->assertEquals('1234567', $billingaddress->PostalCode, 'order postcode');
     $this->assertEquals('NZ', $billingaddress->Country, 'order country');
     $this->assertTrue($order->Member()->exists(), 'member exists now');
     $this->assertEquals('James', $order->Member()->FirstName, 'member first name matches');
     $this->assertEquals('Brown', $order->Member()->Surname, 'surname matches');
     $this->assertEquals('*****@*****.**', $order->Member()->Email, 'email matches');
     //not finished...need to find out how to encrypt the same
     //$this->assertEquals($order->Member()->Password, Security::encrypt_password('jbrown'),'password matches');
 }
All Usage Examples Of ShopMemberFactory::create
ShopMemberFactory