首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > PHP >

请大家帮忙看看有关问题

2013-04-09 
请大家帮忙看看问题程序是要输入两个json格式的txt文件,然后通过匹配两个文件的相同的index,然后输出一个

请大家帮忙看看问题
程序是要输入两个json格式的txt文件,然后通过匹配两个文件的相同的index,然后输出一个新的json格式的txt文件。 比如:
这是输入的两个文件的格式:
1. Product
{
  "product_name": String   // A unique id for the product
  "manufacturer": String
  "family": String         // optional grouping of products
  "model": String
  "announced-date": String // ISO-8601 formatted date string, e.g. 2011-04-28T19:00:00.000-05:00
}
2. Listing
{
  "title": String         // description of product for sale
  "manufacturer":  String // who manufactures the product for sale
  "currency": String      // currency code, e.g. USD, CAD, GBP, etc.
  "price": String         // price, e.g. 19.99, 100.00
}

输出的文件格式为:
{
  "product_name": String
  "listings": Array[Listing]
}

先上代码:
BaseItem.php


<?php
abstract 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();
}
?>


Listing.php

<?php
include_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;
    }
}



Product.php

<?php
include_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;
    }
}
?>



ProListMatcher.php

<?php
include './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
        );
    }
}



Match.php

<?php
include './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);

?>


这是我的代码,然后我在terminal运行:

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 53

Fatal error: Cannot redeclare class Listing in /Users/chrisfu/Sites/test/model/Listing.php on line 53

为什么会出现这样的错误,我没有两个叫Listing的 class,请大家帮忙看看,程序都贴上去了,大家可以运行试试。
[解决办法]
Match.php 中有
include './model/Listing.php';
include 'ProListMatcher.php';

ProListMatcher.php 中有
include './model/Product.php';
include './model/Listing.php';
include './model/BaseItem.php';

class Listing 不是被重复定义了吗?

热点排行