ホームページ >バックエンド開発 >PHPチュートリアル >問題の確認を手伝ってください
プログラムは、2 つの txt ファイルを json 形式で入力し、2 つのファイルの同じインデックスを一致させることによって、新しい txt ファイルを json 形式で出力します。例:
これは 2 つの入力ファイルの形式です:
1. Product
{
"product_name": String // 製品の一意の ID
"manufacturer": String
"family": String // オプションのグループ化製品の数
"model": String
"announced-date": String // ISO-8601 形式の日付文字列、例: 2011-04-28T19:00:00.000-05:00
}
2. リスト
{
"title " : String // 販売する製品の説明
"manufacturer": String // 販売する製品の製造者 "currency": String // 通貨コード、例: USD、CAD、GBP
"price": String / / 価格、例: 19.99、100.00
}
出力ファイルの形式は次のとおりです:
{
"product_name": String
"listings": Array[Listing]
}
まず、コード:
BaseItem.php
<?phpabstract class BaseItem{ /** * @param array $fields */ public function __construct(array $fields) { $this->buildFromArray($fields); } /** * @param array $fields */ abstract public function buildFromArray(array $fields); /** * @return array */ abstract public function convertToArray();}?>
<?phpinclude_once 'BaseItem.php';class Listing extends BaseItem{ protected $title; protected $manufacturer; protected $currency; protected $price; /** * @inheritDoc */ public function convertToArray() { return array( 'title' => $this->title, 'manufacturer' => $this->manufacturer, 'currency' => $this->currency, 'price' => $this->price, ); } /** * @inheritDoc */ public function buildFromArray(array $fields) { $this->title = $fields['title']; $this->manufacturer = $fields['manufacturer']; $this->currency = $fields['currency']; $this->price = $fields['price']; } public function getCurrency() { return $this->currency; } public function getManufacturer() { return $this->manufacturer; } public function getPrice() { return $this->price; } public function getTitle() { return $this->title; }}
<?phpinclude_once 'BaseItem.php';class Product extends BaseItem{ protected $productName; protected $manufacturer; protected $family; protected $model; protected $announcedDate; /** * @inheritDoc */ public function buildFromArray(array $fields) { $this->productName = $fields['product_name']; $this->manufacturer = $fields['manufacturer']; $this->family = isset($fields['family']) ? $fields['family'] : null; $this->model = $fields['model']; $this->announcedDate = $fields['announced-date']; } /** * @inheritDoc */ public function convertToArray() { return array( 'product_name' => $this->productName, 'manufacturer' => $this->manufacturer, 'family' => $this->family, 'model' => $this->model, 'announced-date' => $this->announcedDate, ); } public function getAnnouncedDate() { return $this->announcedDate; } public function getFamily() { return $this->family; } public function getManufacturer() { return $this->manufacturer; } public function getModel() { return $this->model; } public function getProductName() { return $this->productName; }}?>
<?phpinclude './model/Product.php';include './model/Listing.php';include './model/BaseItem.php';class ProListMatcher{ const TYPE_PRODUCT = 1; const TYPE_LISTING = 2; /** @var Product[] */ protected $products; /** @var Listing[] */ protected $listings; /** * @param array $products * @param array $listings */ public function __construct(array $products, array $listings) { $this->products = $this->normalizeRawData($products, self::TYPE_PRODUCT); $this->listings = $this->normalizeRawData($listings, self::TYPE_LISTING); } /** * Matches up listings to products * * @return array */ public function getMatches() { $ret = array(); // loop through all products foreach ($this->products as $product) { // reset matching listings $matchingListings = array(); // loop through all listings foreach ($this->listings as $key => $listing) { $matches = 0; // match on manufacturer if (strtolower($product->getManufacturer()) == strtolower($listing->getManufacturer())) { $matches++; } // match on model in listing title $result = strpos(strtolower($listing->getTitle()), strtolower($product->getModel())); if (false !== $result) { $matches++; } if (2 === $matches) { $matchingListings[] = $listing; // has been matched, remove listing from future searches unset($this->listings[$key]); } } if (count($matchingListings)) { // 1 or more matches were found, add to output $ret[] = array( 'product' => $product, 'listings' => $matchingListings ); } } return $ret; } /** * Convert plain nested array to array of objects * * @param array $data * @param int $type * @return array * @throws \InvalidArgumentException */ protected function normalizeRawData(array $data, $type) { // build model for each item return array_map(function($value) use ($type) { switch ($type) { case ProListMatcher::TYPE_PRODUCT: return new Product($value); case ProListMatcher::TYPE_LISTING: return new Listing($value); default: throw new \InvalidArgumentException(sprintf('Type "%s" is not valid', $type)); } }, $data ); }}
<?phpinclude './model/Listing.php';include 'ProListMatcher.php';if($argc != 3){ die("Usage: Match.php <Products> <Listing>");}array_shift($argv);/** * @param string $data * @return array */function jsonToArray($value){ $ret = explode("\n", trim($value)); return array_map(function($value){ return json_decode($value, true); }, $ret);}$matcher = new ProListMatcher( jsonToArray(file_get_contents($argv[0])), jsonToArray(file_get_contents($argv[1])));$matches = $matcher->getMatches();$matches = array_map(function($value){ return json_encode(array( 'product_name' => $value['product']->getProductName(), 'listing' => array_map(function(Listing $value){ return $value->convertToArray(); }, $value['listing']) ) ); }, $matches);$output = fopen("output.txt", "w+");file_put_contents($output, implode("\n", $matches));fclose($output);?>
chrisfus-MacBook-Pro:test chrisfu$ php Match.php products.txt listings.txt PHP Fatal error: Cannot redeclare class Listing in /Users/chrisfu/Sites/test/model/Listing.php on line 53Fatal error: Cannot redeclare class Listing in /Users/chrisfu/Sites/test/model/Listing.php on line 53
include './model/Listing.php';
include 'ProListMatcher.php' が含まれます
ProListMatcher.php には
include './model/Product. php';
include './model/Listing.php';
クラス Listing が繰り返し定義されていませんか?
Match.php には
ProListMatcher.php には
include './model/Product.php' が含まれます
include '; /Listing.php';
include './model/Base...
Match.php でこれら 2 つのファイルを使用する必要がある場合は、 use を使用する必要がありますか?
では、Product.php と Listing.php で include _once 'BaseItem.php' を使用するのはなぜですか。 php と Listing.php は include_once 'BaseItem.php'; を使用します
include_once がロードされている場合、ロードされません
はい、以前は include と include_once の違いにあまり注意を払いませんでした。勉強中