「アマゾン商品の情報」から、「当プラグイン用の詳細な商品情報」を作成する

「アマゾン商品の情報」から、「当プラグイン用の詳細な商品情報」を作成する処理について、PHPソースコードを解説しています。

「当プラグイン用の詳細な商品情報」には、以下のものがあります。

  • 商品に関連付けられた投稿者。
  • 商品の製造者ラベル。
  • 商品の製造者。
  • 商品の種別。

「アマゾン商品の情報」から、「当プラグイン用の詳細な商品情報」を作成する

public static function makeProductionItem($searchItem): ProductionItem {

	$productionItem = new ProductionItem ();

	if (isset ( $searchItem->ItemInfo )) {
		;
	} else {
		return $productionItem;
	}

	$itemInfo = $searchItem->ItemInfo;

	if (isset ( $itemInfo->Classifications ) and isset ( $itemInfo->Classifications->Binding ) and
			isset ( $itemInfo->Classifications->Binding->DisplayValue )) {

		$bindingValue = $itemInfo->Classifications->Binding->DisplayValue;
		$productionItem->setBinding ( HTMLUtils::makePlainText ( $bindingValue ) );
	}

	if (isset ( $itemInfo->ByLineInfo )) {

		$byLineInfo = $itemInfo->ByLineInfo;

		if (isset ( $byLineInfo->Contributors )) {

			$contributors = $byLineInfo->Contributors; // Contributor[]
			$contributorArray = array ();
			foreach ( $contributors as $contributor ) {

				if (isset ( $contributor ) and isset ( $contributor->Role ) and
						isset ( $contributor->Name )) {
					;
				} else {
					continue;
				}

				$role = HTMLUtils::makePlainText ( $contributor->Role );
				$name = HTMLUtils::makePlainText ( $contributor->Name );
				$contributorValue = $name . "(" . $role . ")";
				array_push ( $contributorArray, $contributorValue );

				if (strcmp ( $role, "著" ) == 0 and
						empty ( $productionItem->getManufacturerLabel () )) {
					$productionItem->setManufacturerLabel ( "出版社" );
				}
			}

			$productionItem->setContributorArray ( $contributorArray );
		}

		if (isset ( $byLineInfo->Manufacturer ) and
				isset ( $byLineInfo->Manufacturer->DisplayValue )) {

			$manufacturer = $byLineInfo->Manufacturer;
			if (isset ( $manufacturer->Label ) and
					empty ( $productionItem->getManufacturerLabel () )) {

				$manufacturerLabel = HTMLUtils::makePlainText ( $manufacturer->Label );
				$manufacturerLabel = str_replace ( 
						ProductionResponse::MANUFACTURER_LABEL_SEARCH,
						ProductionResponse::MANUFACTURER_LABEL_REPLACE, $manufacturerLabel );
				$productionItem->setManufacturerLabel ( $manufacturerLabel );
			}

			$manufacturerValue = HTMLUtils::makePlainText ( $manufacturer->DisplayValue );
			$productionItem->setManufacturer ( $manufacturerValue );
		}
	}

	return $productionItem;
}

$searchItem変数は、「アマゾン商品の情報」を保持します。

その「アマゾン商品の情報」が保持する「商品情報」から、
「当プラグイン用の詳細な商品情報」を作成します。

作成された「当プラグイン用の詳細な商品情報」は、$productionItem変数が保持しています。

「当プラグイン用の詳細な商品情報」には、以下のものがあります。

  • 商品に関連付けられた投稿者。
  • 商品の製造者ラベル。
  • 商品の製造者。
  • 商品の種別。

「当プラグイン用の詳細な商品情報」を保持するProductionItemクラス

「当プラグイン用の詳細な商品情報」を保持する$productionItem変数は、ProductionItemクラスの変数です。

class ProductionItem {
	/* 投稿者(「役柄」と「人物名」) */
	private $contributorArray = array (); // 空のオブジェクト作成に対応するため、空の配列を設定する。

	/* 製造者(製造元、メーカー) */
	private $manufacturerLabel = "";
	private $manufacturer = "";

	/* 種別(装丁、形式) */
	private $binding = "";

以下省略

ProductionItemクラスは、以下の「当プラグイン用の詳細な商品情報」を保持します。

ProductionItemクラスのメンバ変数ProductionItemクラスのメンバ変数の説明
$contributorArray商品に関連付けられた投稿者(配列)。

投稿者とは 、 「役柄」と「人物名」です 。
例:
「役柄」:翻訳
「人物名」:高木 XXXX
$manufacturerLabel商品の製造者ラベル(文字列)。

例:
製造元
出版社
$manufacturer商品の製造者(文字列)。

例:
XXXXジャパン株式会社
$binding商品の種別(バインディング)(文字列)。

バインディングとは、製品カテゴリーに似ている種別のようなものです 。
例:
大型本
ヘルスケア&ケア用品
「当プラグイン用の詳細な商品情報」を保持するProductionItemクラス

「商品の種別」を、「当プラグイン用の詳細な商品情報」を保持する$productionItem変数に設定する

$itemInfo = $searchItem->ItemInfo;

if (isset ( $itemInfo->Classifications ) and isset ( $itemInfo->Classifications->Binding ) and
		isset ( $itemInfo->Classifications->Binding->DisplayValue )) {

	$bindingValue = $itemInfo->Classifications->Binding->DisplayValue;
	$productionItem->setBinding ( HTMLUtils::makePlainText ( $bindingValue ) );
}

$searchItem->ItemInfo->Classifications->Bindingメンバー変数の例。

stdClass Object 
(
[ASIN] => XXXX000001

中略

[ItemInfo] => stdClass Object 
( 
[Classifications] => stdClass Object 
( 
  [Binding] => stdClass Object 
  ( 
  [DisplayValue] => 大型本 
  [Label] => Binding 
  [Locale] => ja_JP 
  ) 

中略

) 
)
)

「アマゾン商品の情報」を保持する$searchItem変数に、
「商品の種別」の文字列を保持するDisplayValueメンバ変数が存在する場合、

「当プラグイン用の詳細な商品情報」を保持する$productionItem変数に、「商品の種別」の文字列を設定します。

$bindingValue = $itemInfo->Classifications->Binding->DisplayValue;
$productionItem->setBinding ( HTMLUtils::makePlainText ( $bindingValue ) );

上記ソースコードでは、$bindingValue変数に「商品の種別」の文字列を代入します。

HTMLUtils::makePlainText()関数に$bindingValue変数を渡して、「商品の種別」の文字列をプレーンテキストに作り直します。

$productionItem->setBinding()関数を用いて、

プレーンテキストに作り直した「商品の種別」の文字列を、
「当プラグイン用の詳細な商品情報」を保持する$productionItem変数に、設定します。

$productionItem->bindingメンバー変数の例。

goodsmemo\item\ProductionItem Object
  (

中略

    [binding:goodsmemo\item\ProductionItem:private] => 大型本
  )

「商品に関連付けられた投稿者の配列」を、「当プラグイン用の詳細な商品情報」を保持する$productionItem変数に設定する

$itemInfo = $searchItem->ItemInfo;

中略

if (isset ( $itemInfo->ByLineInfo )) {

	$byLineInfo = $itemInfo->ByLineInfo;

	if (isset ( $byLineInfo->Contributors )) {

		$contributors = $byLineInfo->Contributors; // Contributor[]
		$contributorArray = array ();
		foreach ( $contributors as $contributor ) {

			if (isset ( $contributor ) and isset ( $contributor->Role ) and
					isset ( $contributor->Name )) {
				;
			} else {
				continue;
			}

			$role = HTMLUtils::makePlainText ( $contributor->Role );
			$name = HTMLUtils::makePlainText ( $contributor->Name );
			$contributorValue = $name . "(" . $role . ")";
			array_push ( $contributorArray, $contributorValue );

			if (strcmp ( $role, "著" ) == 0 and
					empty ( $productionItem->getManufacturerLabel () )) {
				$productionItem->setManufacturerLabel ( "出版社" );
			}
		}

		$productionItem->setContributorArray ( $contributorArray );
	}

中略

}

$searchItem->ItemInfo->ByLineInfo->Contributorsメンバ変数の例。

stdClass Object 
(
[ASIN] => XXXX000001

中略

[ItemInfo] => stdClass Object 
( 
[ByLineInfo] => stdClass Object 
( 
  [Contributors] => Array 
  ( 
  [0] => stdClass Object 
  ( 
  [Locale] => ja_JP 
  [Name] => Kevin XXXX 
  [Role] => 著 
  [RoleType] => author 
  ) 
  [1] => stdClass Object 
  ( 
  [Locale] => ja_JP 
  [Name] => Peter XXXX 
  [Role] => 著 
  [RoleType] => author 
  ) 
  [2] => stdClass Object 
  ( 
  [Locale] => ja_JP 
  [Name] => Rasmus XXXX 
  [Role] => 著 
  [RoleType] => author 
  ) 
  [3] => stdClass Object 
  ( 
  [Locale] => ja_JP 
  [Name] => 高木 XXXX 
  [Role] => 翻訳 
  [RoleType] => translator 
  ) 
  ) 

中略

)
)
)

「アマゾン商品の情報」を保持する$searchItem変数に、
「商品に関連付けられた投稿者の配列」を保持するContributorsメンバ変数が存在する場合、

「当プラグイン用の詳細な商品情報」を保持する$productionItem変数に、「商品に関連付けられた投稿者の配列」を設定します。

「アマゾン商品の情報」から、「商品に関連付けられた投稿者の配列」を作成する

$contributors = $byLineInfo->Contributors; // Contributor[]
$contributorArray = array ();
foreach ( $contributors as $contributor ) {

	if (isset ( $contributor ) and isset ( $contributor->Role ) and
			isset ( $contributor->Name )) {
		;
	} else {
		continue;
	}

	$role = HTMLUtils::makePlainText ( $contributor->Role );
	$name = HTMLUtils::makePlainText ( $contributor->Name );
	$contributorValue = $name . "(" . $role . ")";
	array_push ( $contributorArray, $contributorValue );

	if (strcmp ( $role, "著" ) == 0 and
			empty ( $productionItem->getManufacturerLabel () )) {
		$productionItem->setManufacturerLabel ( "出版社" );
	}
}

$productionItem->setContributorArray ( $contributorArray );

$contributors変数の例。

[Contributors] => Array 
( 
  [0] => stdClass Object 
  ( 
  [Locale] => ja_JP 
  [Name] => Kevin XXXX 
  [Role] => 著 
  [RoleType] => author 
  ) 
  [1] => stdClass Object 
  ( 
  [Locale] => ja_JP 
  [Name] => Peter XXXX 
  [Role] => 著 
  [RoleType] => author 
  ) 
  [2] => stdClass Object 
  ( 
  [Locale] => ja_JP 
  [Name] => Rasmus XXXX 
  [Role] => 著 
  [RoleType] => author 
  ) 
  [3] => stdClass Object 
  ( 
  [Locale] => ja_JP 
  [Name] => 高木 XXXX 
  [Role] => 翻訳 
  [RoleType] => translator 
  ) 
) 

「アマゾン商品の情報」の「投稿者」を保持する$contributors変数に、
「投稿者」のRoleメンバ変数と、「投稿者」のNameメンバ変数が存在する場合、

「商品に関連付けられた投稿者」の文字列を作成します。

「商品に関連付けられた投稿者」の文字列を、「商品に関連付けられた投稿者の配列」に追加します。

以上の処理を、「アマゾン商品の情報」の「投稿者」の個数だけ、ループ処理にて行います。

そのループ処理にて、「商品に関連付けられた投稿者の配列」を作成します。

$role = HTMLUtils::makePlainText ( $contributor->Role );
$name = HTMLUtils::makePlainText ( $contributor->Name );
$contributorValue = $name . "(" . $role . ")";
array_push ( $contributorArray, $contributorValue );

「商品に関連付けられた投稿者」は、以下を保持します。

  • 「役柄」
  • 「人物名」

$role変数に、「商品に関連付けられた投稿者」の「役柄」を代入します。

$role変数の例。

$name変数に、「商品に関連付けられた投稿者」の「人物名」を代入します。

$name変数の例。
Kevin XXXX

なお、$role変数、$name変数、そのどちらも、HTMLUtils::makePlainText()関数によってプレーンテキストに作り直した文字列が、代入されます。

$contributorValue変数に、「役柄」と「人物名」を合わせた文字列を代入します。

$contributorValue変数の例。
Kevin XXXX(著)

$contributorValue変数は、当プラグイン用の「商品に関連付けられた投稿者」を保持します。

$contributorValue変数の例では、商品は書籍です。
本の「著者」は、「Kevin XXXX」であることを示しています。

array_push ( $contributorArray, $contributorValue );

「$contributorArray配列の変数」は、当プラグイン用の「商品に関連付けられた投稿者の配列」です。

その「$contributorArray配列の変数」に、
$contributorValue変数が保持する「商品に関連付けられた投稿者」を、追加します。

「商品に関連付けられた投稿者」の「役柄」が書籍の著者の場合、「商品の製造者ラベル」を「出版社」と設定する
if (strcmp ( $role, "著" ) == 0 and
		empty ( $productionItem->getManufacturerLabel () )) {
	$productionItem->setManufacturerLabel ( "出版社" );
}

「アマゾン商品の情報」が書籍を示す場合、「商品の製造者ラベル」を「出版社」にします。

  • $role変数が保持する、「商品に関連付けられた投稿者」の「役柄」が「著」である。
  • 「当プラグイン用の詳細な商品情報」を保持する$productionItem変数において、
    「商品の製造者ラベル」が未設定である(空文字である)。

以上の二つが成り立つ場合、$productionItem変数に対して、

「商品の製造者ラベル」を「出版社」と設定します。

二つの文字列を比較するのに、strcmp()関数を使っています

上記のソースコードでは、文字列の比較でstrcmp()関数を使っています。

strcmp()関数は、二つの文字列を辞書順に比較して、
同じなら0、違うなら0以外の値を返します。

なお、strcmp()関数で文字列を比較する場合、エンコーディングに注意する必要があります。

比較する文字列が、どちらもUTF-8でエンコーディングされていることなどに、注意する必要があります。

もしも、異なるエンコーディングの文字列を比較したい場合は、mb_strcmp()関数を使うと良いです。

mb_strcmp()関数の使い方は、以下の通りです。

mb_strcmp(比較する文字列1, 比較する文字列2, エンコーディング)
商品が書籍の場合、「商品の製造者ラベル」は「出版社」の方が良いと判断しました

当プラグインを開発していた当時(たぶん2018年当時)、アマゾン商品が書籍の場合、
「商品の製造者ラベル」が「Manufacturer」(製造元)になっていました。
※2023年3月時点でも、そうなっていました。

商品が書籍の場合、「商品の製造者ラベル」は「出版社」の方が良い、と判断しました。

なので当プラグインのプログラムにて、

「商品の製造者ラベル」を「出版社」と設定しています。

作成された「商品に関連付けられた投稿者の配列」を、「当プラグイン用の詳細な商品情報」を保持する$productionItem変数に設定する

foreach ( $contributors as $contributor ) {

中略

}

$productionItem->setContributorArray ( $contributorArray );

「アマゾン商品の情報」の「投稿者」の個数だけ、ループ処理を行なった後、
「商品に関連付けられた投稿者の配列」である「$contributorArray配列の変数」が、作成されています。

作成された「$contributorArray配列の変数」の例。

("Kevin XXXX(著)", "Peter XXXX(著)", "Rasmus XXXX(著)","高木 XXXX(翻訳)")

「当プラグイン用の詳細な商品情報」を保持する$productionItem変数に、
「商品に関連付けられた投稿者の配列」である「$contributorArray配列の変数」を、設定します。

$productionItem->contributorArrayメンバ変数の例。

goodsmemo\item\ProductionItem Object
    (
      [contributorArray:goodsmemo\item\ProductionItem:private] => Array
        (
          [0] => Kevin XXXX(著)
          [1] => Peter XXXX(著)
          [2] => Rasmus XXXX(著)
          [3] => 高木 XXXX(翻訳)
        )

以下省略

    )

「商品の製造者ラベル」と「商品の製造者」を、「当プラグイン用の詳細な商品情報」を保持する$productionItem変数に設定する

// str_replace()用 検索文字列と置換文字列
const MANUFACTURER_LABEL_SEARCH = array ("Manufacturer"
);
const MANUFACTURER_LABEL_REPLACE = array ("製造元"
);

中略

$itemInfo = $searchItem->ItemInfo;

中略

if (isset ( $itemInfo->ByLineInfo )) {

	$byLineInfo = $itemInfo->ByLineInfo;

中略

	if (isset ( $byLineInfo->Manufacturer ) and
			isset ( $byLineInfo->Manufacturer->DisplayValue )) {

		$manufacturer = $byLineInfo->Manufacturer;
		if (isset ( $manufacturer->Label ) and
				empty ( $productionItem->getManufacturerLabel () )) {

			$manufacturerLabel = HTMLUtils::makePlainText ( $manufacturer->Label );
			$manufacturerLabel = str_replace ( 
					ProductionResponse::MANUFACTURER_LABEL_SEARCH,
					ProductionResponse::MANUFACTURER_LABEL_REPLACE, $manufacturerLabel );
			$productionItem->setManufacturerLabel ( $manufacturerLabel );
		}

		$manufacturerValue = HTMLUtils::makePlainText ( $manufacturer->DisplayValue );
		$productionItem->setManufacturer ( $manufacturerValue );
	}
}

$searchItem->ItemInfo->ByLineInfo->Manufacturerメンバ変数の例。

stdClass Object 
(
[ASIN] => XXXX000001

中略

[ItemInfo] => stdClass Object 
( 
[ByLineInfo] => stdClass Object 
( 
  [Manufacturer] => stdClass Object 
  ( 
  [DisplayValue] => XXXXジャパン 
  [Label] => Manufacturer 
  [Locale] => ja_JP 
  ) 
) 

中略

)
)

「アマゾン商品の情報」を保持する$searchItem変数に、
「商品の製造者ラベル」を保持するLabelメンバ変数が存在する場合、

「当プラグイン用の詳細な商品情報」を保持する$productionItem変数に、「商品の製造者ラベル」を設定します。

同様に、「アマゾン商品の情報」を保持する$searchItem変数に、
「商品の製造者」を保持するDisplayValueメンバ変数が存在する場合、

「当プラグイン用の詳細な商品情報」を保持する$productionItem変数に、「商品の製造者」を設定します。

「商品の製造者ラベル」を、$productionItem変数に設定する

// str_replace()用 検索文字列と置換文字列
const MANUFACTURER_LABEL_SEARCH = array ("Manufacturer"
);
const MANUFACTURER_LABEL_REPLACE = array ("製造元"
);

中略

$manufacturer = $byLineInfo->Manufacturer;
if (isset ( $manufacturer->Label ) and
		empty ( $productionItem->getManufacturerLabel () )) {

	$manufacturerLabel = HTMLUtils::makePlainText ( $manufacturer->Label );
	$manufacturerLabel = str_replace ( 
			ProductionResponse::MANUFACTURER_LABEL_SEARCH,
			ProductionResponse::MANUFACTURER_LABEL_REPLACE, $manufacturerLabel );
	$productionItem->setManufacturerLabel ( $manufacturerLabel );
}

$searchItem->ItemInfo->ByLineInfo->Manufacturerメンバー変数の例。

[Manufacturer] => stdClass Object 
( 
[DisplayValue] => XXXXジャパン 
[Label] => Manufacturer 
[Locale] => ja_JP 
) 
  • $manufacturer変数に、「商品の製造者ラベル」を保持するLabelメンバ変数が存在する。
  • 「当プラグイン用の詳細な商品情報」を保持する$productionItem変数において、
    「商品の製造者ラベル」が未設定である(空文字である)。

以上の二つが成り立つ場合、

$productionItem変数に「商品の製造者ラベル」を設定します。

$productionItem変数の「商品の製造者ラベル」においては、すでに「商品の製造者ラベル」が設定済みの場合があります。

当記事の「見出し」
「商品に関連付けられた投稿者」の「役柄」が書籍の著者の場合、「商品の製造者ラベル」を「出版社」と設定する
を、参照してください。

// str_replace()用 検索文字列と置換文字列
const MANUFACTURER_LABEL_SEARCH = array ("Manufacturer"
);
const MANUFACTURER_LABEL_REPLACE = array ("製造元"
);

中略

$manufacturerLabel = HTMLUtils::makePlainText ( $manufacturer->Label );
$manufacturerLabel = str_replace ( 
		ProductionResponse::MANUFACTURER_LABEL_SEARCH,
		ProductionResponse::MANUFACTURER_LABEL_REPLACE, $manufacturerLabel );
$productionItem->setManufacturerLabel ( $manufacturerLabel );

$manufacturerLabel変数に、「商品の製造者ラベル」の文字列を代入します。

$manufacturerLabel変数の例。
Manufacturer

$manufacturerLabel変数の文字列に対して、str_replace()関数を用いて、文字列の置換を試みます。

例えば、「Manufacturer」を「製造元」に置き換えます。

文字列が置換された$manufacturerLabel変数の例。
製造元

「商品の製造者ラベル」において、
英単語の「Manufacturer」よりも、日本語の「製造元」の方がわかりやすい、と判断しました。

よって、文字列を上記のように置換しました。

その後、「当プラグイン用の詳細な商品情報」を保持する$productionItem変数に、
文字列が置換された「商品の製造者ラベル」を、設定します。

$productionItem->manufacturerLabelメンバ変数の例。

goodsmemo\item\ProductionItem Object
  (

中略

    [manufacturerLabel:goodsmemo\item\ProductionItem:private] => 製造元

中略

  )
「商品の製造者ラベル」の文字列を置換するstr_replace()関数について
// str_replace()用 検索文字列と置換文字列
const MANUFACTURER_LABEL_SEARCH = array ("Manufacturer"
);
const MANUFACTURER_LABEL_REPLACE = array ("製造元"
);

中略

$manufacturerLabel = str_replace ( 
		ProductionResponse::MANUFACTURER_LABEL_SEARCH,
		ProductionResponse::MANUFACTURER_LABEL_REPLACE, $manufacturerLabel );

str_replace()関数の引数の説明は、以下の通りです。

  • ProductionResponse::MANUFACTURER_LABEL_SEARCHは、置き換える対象となる文字列の配列です。
  • ProductionResponse::MANUFACTURER_LABEL_REPLACEは、置き換える文字列の配列です。
  • $manufacturerLabel変換は、検索と置き換えを行う文字列です。

str_replace()関数の戻り値は、文字列を置換した結果の文字列です。

なお、str_replace()関数については、以下の関連記事でも考察しています。

※関連記事:「表示するHTML」を作成する、検索・置換対象の文字列を配列で指定するstr_replace()関数

「商品の製造者」を、$productionItem変数に設定する

$manufacturerValue = HTMLUtils::makePlainText ( $manufacturer->DisplayValue );
$productionItem->setManufacturer ( $manufacturerValue );

$searchItem->ItemInfo->ByLineInfo->Manufacturerメンバー変数の例。

[Manufacturer] => stdClass Object 
( 
[DisplayValue] => XXXXジャパン 
[Label] => Manufacturer 
[Locale] => ja_JP 
) 

$manufacturerValue変数に、「商品の製造者」の文字列を代入します。

HTMLUtils::makePlainText()関数に$manufacturer->DisplayValueメンバ変数を渡して、「商品の製造者」の文字列をプレーンテキストに作り直します。

プレーンテキストに作り直した「商品の製造者」の文字列を、$manufacturerValue変数に代入します。

$manufacturerValue変数が保持する「商品の製造者」の文字列を、
「当プラグイン用の詳細な商品情報」を保持する$productionItem変数に、設定します。

$productionItem->manufacturerメンバ変数の例。

goodsmemo\item\ProductionItem Object
  (

中略

    [manufacturer:goodsmemo\item\ProductionItem:private] => XXXXジャパン

中略

  )

「アマゾン商品の情報」に「商品情報」が存在しない場合、「詳細な商品情報」が未設定の「当プラグイン用の詳細な商品情報」を作成する

public static function makeProductionItem($searchItem): ProductionItem {

	$productionItem = new ProductionItem ();

	if (isset ( $searchItem->ItemInfo )) {
		;
	} else {
//「アマゾン商品の情報」に「商品情報」が存在しない場合
		return $productionItem;
	}

//「商品の種別」が存在する場合、「当プラグイン用の詳細な商品情報」に「商品の種別」を設定する。
//「商品に関連付けられた投稿者」が存在する場合、「当プラグイン用の詳細な商品情報」に「商品に関連付けられた投稿者」を設定する。
//「商品の製造者ラベル」が存在する場合、「当プラグイン用の詳細な商品情報」に「商品の製造者ラベル」を設定する。
//「商品の製造者」が存在する場合、「当プラグイン用の詳細な商品情報」に「商品の製造者」を設定する。

//「詳細な商品情報」が未設定の「当プラグイン用の詳細な商品情報」
//または
//「詳細な商品情報」が設定済みの「当プラグイン用の詳細な商品情報」
//を、return文で返す。
	return $productionItem;
}

$searchItem変数の例。

stdClass Object 
(
[ASIN] => XXXX000001

中略

[ItemInfo] => stdClass Object 
( 

中略

)

)

「アマゾン商品の情報」に「商品情報」が存在しない場合、
「詳細な商品情報」が未設定の「当プラグイン用の詳細な商品情報」を、作成します。

「詳細な商品情報」が未設定の「当プラグイン用の詳細な商品情報」とは、
「商品の種別」などの「詳細な商品情報」を設定していない$productionItem変数のことです。

if (isset ( $searchItem->ItemInfo )) {
	;
} else {
//「アマゾン商品の情報」に「商品情報」が存在しない場合
	return $productionItem;
}

「アマゾン商品の情報」に「商品情報」が存在しない場合、
「詳細な商品情報」が未設定の$productionItem変数を、return文で返します。

上記ソースコードのisset()関数において、
$searchItem->ItemInfoメンバ変数が設定されている。

以上の条件が成り立つなら、ここでは何もせず、次の処理に進みます。

以上の条件が成り立たない場合は、

「アマゾン商品の情報」を保持する$searchItem変数に、「商品情報」が存在しない場合です。

この場合は、「詳細な商品情報」が未設定の$productionItem変数を、return文で返します。

//「詳細な商品情報」が未設定の「当プラグイン用の詳細な商品情報」
//または
//「詳細な商品情報」が設定済みの「当プラグイン用の詳細な商品情報」
//を、return文で返す。
	return $productionItem;
}

同様に、

  • 「商品の種別が存在しない」
  • そして「商品に関連付けられた投稿者が存在しない」
  • そして「商品の製造者ラベルが存在しない」
  • そして「商品の製造者が存在しない」

上記の四つの条件が成り立つ場合、

「アマゾン商品の情報」を保持する$searchItem変数に、「商品情報」が存在しない場合です。

この場合も、「詳細な商品情報」が未設定の$productionItem変数を、return文で返します。

ソースコードのインテンドがやや深くて、「正常処理の流れ」がわかりにくいです

public static function makeProductionItem($searchItem): ProductionItem {

中略

	if (isset ( $itemInfo->ByLineInfo )) {

		$byLineInfo = $itemInfo->ByLineInfo;

		if (isset ( $byLineInfo->Contributors )) {

			$contributors = $byLineInfo->Contributors; // Contributor[]
			$contributorArray = array ();
			foreach ( $contributors as $contributor ) {

				if (isset ( $contributor ) and isset ( $contributor->Role ) and
						isset ( $contributor->Name )) {
					;
				} else {
					continue;
				}

				$role = HTMLUtils::makePlainText ( $contributor->Role );
				$name = HTMLUtils::makePlainText ( $contributor->Name );
				$contributorValue = $name . "(" . $role . ")";
				array_push ( $contributorArray, $contributorValue );

				if (strcmp ( $role, "著" ) == 0 and
						empty ( $productionItem->getManufacturerLabel () )) {
					$productionItem->setManufacturerLabel ( "出版社" );
				}
			}

			$productionItem->setContributorArray ( $contributorArray );
		}

以下省略

}

上記のソースコードにおいて、インテンドがやや深いです。

例えば、foreach文の中にあるif文は、インテンドが深くなっています。
そのため、「正常処理の流れ」がわかりにくくなっています。

ソースコードのインテンドを浅くするために、
foreach文のループ処理を、別の関数にまとめた方が良かったかもしれません。

※関連記事:垂直な一本の線、Javaプログラムの正常処理の流れ

プログラムの構造をわかりやすくする、ソースコードのインテンド

ソースコードのインテンドとは、コードの行頭に挿入した空白タブのことです。

インテンドは、コードのブロックや階層を視覚的に表現する役割を果たします。

そんなインテンドは、プログラムの構造や可読性を向上させるために用いられます。

インテンドの方法は、言語や環境によって異なります。

一般的には、同じレベルのコードには同じ幅のインテンドを適用します。
ネストされたコードにはインテンドを増やす、という原則があります。

ソースコードの複雑さを示す、深すぎるインテンド

インテンドの深さは、一般的に4段階以下にすることが推奨されています。

ソースコードのインテンドが深い状態は、ソースコードの構造や意味を表すために、行頭に空白やタブを入れることが多い、という状態です。

深すぎるインテンドは、逆にソースコードの「複雑さ」や「冗長さ」を示す可能性があります。

インテンドは、ソースコードの意味や動作に直接影響するものではありません。
しかし、インテンドの統一性や規約の遵守は、ソースコードの品質やメンテナンス性に大きく関わります。

インテンドが深いソースコードを改善する方法としては、以下のものがあります。

  • 関数やクラスに分割する。
  • 条件分岐やループを簡略化する。

ProductionResponse.phpのソースコード

<?php

namespace goodsmemo\amazon\withoutsdk;

use goodsmemo\item\ProductionItem;
use goodsmemo\item\html\HTMLUtils;

require_once GOODS_MEMO_DIR . "item/ProductionItem.php";
require_once GOODS_MEMO_DIR . "item/html/HTMLUtils.php";

class ProductionResponse {
	// str_replace()用 検索文字列と置換文字列
	const MANUFACTURER_LABEL_SEARCH = array ("Manufacturer"
	);
	const MANUFACTURER_LABEL_REPLACE = array ("製造元"
	);

	public static function makeProductionItem($searchItem): ProductionItem {

		$productionItem = new ProductionItem ();

		if (isset ( $searchItem->ItemInfo )) {
			;
		} else {
			return $productionItem;
		}

		$itemInfo = $searchItem->ItemInfo;

		if (isset ( $itemInfo->Classifications ) and isset ( $itemInfo->Classifications->Binding ) and
				isset ( $itemInfo->Classifications->Binding->DisplayValue )) {

			$bindingValue = $itemInfo->Classifications->Binding->DisplayValue;
			$productionItem->setBinding ( HTMLUtils::makePlainText ( $bindingValue ) );
		}

		if (isset ( $itemInfo->ByLineInfo )) {

			$byLineInfo = $itemInfo->ByLineInfo;

			if (isset ( $byLineInfo->Contributors )) {

				$contributors = $byLineInfo->Contributors; // Contributor[]
				$contributorArray = array ();
				foreach ( $contributors as $contributor ) {

					if (isset ( $contributor ) and isset ( $contributor->Role ) and
							isset ( $contributor->Name )) {
						;
					} else {
						continue;
					}

					$role = HTMLUtils::makePlainText ( $contributor->Role );
					$name = HTMLUtils::makePlainText ( $contributor->Name );
					$contributorValue = $name . "(" . $role . ")";
					array_push ( $contributorArray, $contributorValue );

					if (strcmp ( $role, "著" ) == 0 and
							empty ( $productionItem->getManufacturerLabel () )) {
						$productionItem->setManufacturerLabel ( "出版社" );
					}
				}

				$productionItem->setContributorArray ( $contributorArray );
			}

			if (isset ( $byLineInfo->Manufacturer ) and
					isset ( $byLineInfo->Manufacturer->DisplayValue )) {

				$manufacturer = $byLineInfo->Manufacturer;
				if (isset ( $manufacturer->Label ) and
						empty ( $productionItem->getManufacturerLabel () )) {

					$manufacturerLabel = HTMLUtils::makePlainText ( $manufacturer->Label );
					$manufacturerLabel = str_replace ( 
							ProductionResponse::MANUFACTURER_LABEL_SEARCH,
							ProductionResponse::MANUFACTURER_LABEL_REPLACE, $manufacturerLabel );
					$productionItem->setManufacturerLabel ( $manufacturerLabel );
				}

				$manufacturerValue = HTMLUtils::makePlainText ( $manufacturer->DisplayValue );
				$productionItem->setManufacturer ( $manufacturerValue );
			}
		}

		return $productionItem;
	}
}

【まとめ記事】へのリンク

【まとめ記事】アマゾンの商品を表示する場合、「アフィリエイト商品表示・WordPressプラグイン」のPHPソースコード解説

「アフィリエイト商品表示・WordPressプラグイン」を用いて、アマゾンの商品を表示する処理について、PHPソースコードを解説しています。