public function testValidateAndConvertValidIsbns() { foreach ($this->validIsbns as $isbn10 => $isbn13) { $this->assertTrue(Isbn::validate($isbn10)); $this->assertTrue(Isbn::validate($isbn13)); $this->assertTrue(Isbn::validate10($isbn10)); $this->assertTrue(Isbn::validate13($isbn13)); $this->assertFalse(Isbn::validate10($isbn13)); $this->assertFalse(Isbn::validate13($isbn10)); $this->assertEquals($isbn10, Isbn::to10($isbn10)); $this->assertEquals($isbn10, Isbn::to10($isbn13)); $this->assertEquals($isbn13, Isbn::to13($isbn13)); $this->assertEquals($isbn13, Isbn::to13($isbn10)); } }
protected function makeNewItem($sectionId, $details, $packageId = null, $status = null) { // undefined index errors if we have high error level reporting on. we // really don't care about these here, it's fine if they're interpreted as // falsy $oldLevel = error_reporting(0); if ($details['requiredStatus'] < SectionHasItem::GO_TO_CLASS_FIRST) { return true; } if (Isbn::validate(Isbn::clean($details['isbn']))) { $details['isbn'] = Isbn::to13(Isbn::clean($details['isbn'])); } else { $details['isbn'] = null; } $item = $this->getItemQuery()->filterByProductId($details['productId'])->filterByPartNumber($details['partNumber'])->filterByIsbn($details['isbn'])->findOne(); if (!$item) { $item = $this->newItem()->setIsbn($details['isbn'])->setProductId($details['productId'])->setPartNumber($details['partNumber']); } $item->setTitle($details['title'])->setAuthor($details['author'])->setEdition($details['edition'])->setPublisher($details['publisher'])->setImageUrl($details['imageUrl'])->setBNew($details['newPrice'])->setBUsed($details['usedPrice'])->setIsPackage($details['isPackage'] || $details['components'])->setPackageId($packageId)->save(); if (isset($details['components'])) { foreach ($details['components'] as $component) { $this->makeNewItem($sectionId, $component, $item->getId(), $details['requiredStatus']); } } if ($packageId === null) { $sectionHasItem = $this->getSectionHasItemQuery()->filterByItem($item)->filterBySectionId($sectionId)->findOne(); if (!$sectionHasItem) { $sectionHasItem = $this->newSectionHasItem()->setItem($item)->setSectionId($sectionId); } $sectionHasItem->setRequiredStatus($details['requiredStatus'] ?: $status)->setTouched(1)->save(); } error_reporting($oldLevel); }
protected static function processItemText($item) { $details = array(); if (preg_match('/Author\\:\\ \\;([^<]*)\\</', $item, $matches)) { $details['author'] = ucname(trim($matches[1])); } if (preg_match('/Edition\\:\\ \\;([^<]*)\\</', $item, $matches)) { $details['edition'] = trim($matches[1]); } if (preg_match('/src\\="(http[^"]+([X0-9]{10,13}|noBookImage)[^"]+?)"[^>]*?alt\\="([^"]+?)"/', $item, $matches)) { if (strpos($matches[1], 'noBookImage')) { $details['imageUrl'] = false; $details['isbn'] = null; } else { $details['imageUrl'] = trim($matches[1]); $details['isbn'] = Isbn::to13(Isbn::clean($matches[2])); } $details['title'] = titleCase(trim(html_entity_decode($matches[3]))); } $details['newPrice'] = $details['usedPrice'] = null; if (!strpos($item, 'Buy Digital') && preg_match('/buynewradio[^>]+?\\>\\s*?\\$([\\d\\.]+)/', $item, $matches)) { $details['newPrice'] = $matches[1]; } if (preg_match('/buyusedradio[^>]+?\\>\\s*?\\$([\\d\\.]+)/', $item, $matches)) { $details['usedPrice'] = $matches[1]; } if (preg_match('/ISBN:\\s*(?:\\ \\;)?(\\d{10,13})/', $item, $matches)) { $details['isbn'] = Isbn::to13(Isbn::clean($matches[1])); } // lowercase c in catEntry ensures that the rental item id is not selected, because it is like rentalCatEntry. // the new item id seems to always be exactly one less than the used // note: we don't use this as a unique identifier because we don't want to deal with which // is used and which is new, and because availability changes if (preg_match_all('/catEntryId_\\d+[^>]+?value\\="(\\d*)"/', $item, $matches)) { //$details['productId'] = count($matches[1] == 1) ? $matches[1][0] : call_user_func_array('min', $matches[1]); } $details['partNumber'] = null; $details['publisher'] = null; $details['isPackage'] = false; $details['components'] = array(); return $details; }
/** * todo: this should be changed to use Amazon::getMultiAmazonData so only * one call is made if there are multiple isbns. however, that makes it * difficult to keep isbn10/isbn13 distinction. */ public function book($isbn) { global $app; $isbns = explode("-", $isbn); $ret = array(); foreach ($isbns as $isbn) { if (Isbn::validate($isbn) && ($data = Amazon::getAmazonData($isbn))) { // Occasionally Amazon returns data for the book we want, but // with a different ISBN. This preserves the original ISBN. if ($data['isbn'] != Isbn::to13($isbn)) { $itemId = $data['isbn']; } else { $itemId = $isbn; } $ret[] = array('itemId' => $itemId, 'slug' => $itemId, 'data' => $data); } else { $ret[] = array('itemId' => $isbn, 'slug' => $isbn, 'error' => true); } } echo json_encode($ret); $app->stop(); Results::fetchPrices($isbns); }