프로그래밍/Php

[디자인패턴] php 로 알아보는 Builder pattern (주식 정보 가져오기, 주식 정보 API만들기- kospi, kosdaq, krx)

소행성왕자 2020. 4. 23. 14:25

디자인 패턴중 Builder pattern 을 알아보도록 합시다.

Builder pattern 이 무엇이지는 여기 를 참고하시면

많은 도움이 되실겁니다. 

 

아래 소스는 제가 사용하는 실제 주식정보(kospi, kosdaq, krx) 가져오는 API 일부 소스입니다.

현재는 종합주가지수 (kospi, kosdaq, krx) 정보만 가져오지만

개별 주식 종목 크롤링 하는것도 가능합니다.

기회가 되면 개별 주식 종목 API 알려드리겠습니다.

 

Builder.php

<?php
/**
 * Created by PhpStorm.
 * User: naya
 * Date: 2020/04/22
 * Time: 09:18 PM
 *
 * builder parrten : https://stackoverflow.com/questions/10961673/php-builder-pattern-without-inner-classes
 *
 */

namespace Naya;

use Carbon\Carbon;

class Builder
{
    private $searchDate;
    private $otpUrl;

    private $post = [];

    /**
     * @param String $searchDate
     */
    public function setSearchDate(String $searchDate) {
        $this->searchDate = $searchDate;
        return $this;
    }

    /**
     * @return mixed
     */
    public function getSearchDate() {
        return $this->searchDate;
    }

    /**
     * @param mixed $otpUrl
     */
    public function setOtpUrl($otpUrl) {
        $this->otpUrl = $otpUrl;
        return $this;
    }

    /**
     * @return mixed
     */
    public function getOtpUrl() {
        return $this->otpUrl;
    }

    public function build(): Config3 {
        return new Config3($this);
    }
}

Config3.php

<?php
/**
 * Created by PhpStorm.
 * User: naya
 * Date: 2020/04/22
 * Time: 09:18 PM
 *
 * builder parrten : https://stackoverflow.com/questions/10961673/php-builder-pattern-without-inner-classes
 */

namespace Naya;

use Carbon\Carbon;

class Config3
{
    private $searchDate;
    private $otpUrl;
    
    private $post;
    
    static function createBuilder() {
        return new Builder();
    }

    public function __construct(Builder $builder) {

        $this->searchDate = $builder->getSearchDate();
        $this->otpUrl = $builder->getOtpUrl();
        $this->post = $builder->getPost();
    }


    /**
     * @return mixed
     */
    public function getSearchDate() {
        return $this->searchDate;
    }

    /**
     * @return mixed
     */
    public function getOtpUrl() {
        return $this->otpUrl;
    }
    
    /**
     * @return array
     */
    public function getPost(): array {
        return $this->post;
    }

}

 

<?php

$config = Config3::createBuilder()
    ->setSearchDate($searchDate)
    ->setOtpUrl("http://naver.com")
    ->setPost(['key1' => "val1"])
    ->build();

$obj = new MarketDataKrxCoKrMarketId3($config);
$par = new Parse($obj);
$par->crawling();

Reresult

[data] => ,지수명,시작일기준가,종료일종가,대비,등락률(%),거래량(주),거래대금(원)
,코스피,"1,914.53","1,898.36","16.17","-0.84","1,087,463,349","11,915,932,209,602"
,코스피 200,"255.02","252.14","2.88","-1.13","211,000,353","7,086,991,401,554"
,코스피 100,"1,959.86","1,935.86","24.00","-1.22","126,979,906","5,587,918,565,269"
,코스피 50,"1,767.39","1,744.75","22.64","-1.28","72,186,737","4,645,119,869,479"
,제조업,"4,737.45","4,684.51","52.94","-1.12","763,756,842","8,537,493,458,637"
,음식료품,"3,331.04","3,308.29","22.75","-0.68","42,204,826","121,885,794,362"
,섬유의복,"231.75","234.85","3.10","1.34","28,027,721","68,541,329,376"
,종이목재,"304.38","303.90","0.48","-0.16","19,348,627","68,055,847,755"
,화학,"3,971.76","3,955.47","16.29","-0.41","31,401,169","882,778,847,790"
,의약품,"12,854.11","12,967.02","112.91","0.88","190,818,077","3,303,129,469,594"
,비금속광물,"1,328.47","1,345.69","17.22","1.30","12,567,206","203,235,901,225"
,철강금속,"2,947.16","2,970.26","23.10","0.78","48,177,000","259,098,376,795"
,기계,"545.33","565.95","20.62","3.78","48,107,444","313,795,909,873"
,전기전자,"18,551.29","18,119.34","431.95","-2.33","76,120,322","2,428,676,131,981"
,의료정밀,"1,825.83","1,874.54","48.71","2.67","191,815,912","43,064,781,391"
,운수장비,"1,128.83","1,120.21","8.62","-0.76","54,673,185","760,630,706,254"
,유통업,"309.02","310.04","1.02","0.33","132,915,799","610,389,251,626"
,전기가스업,"655.14","675.91","20.77","3.17","4,057,910","87,673,825,695"
,건설업,"78.24","79.92","1.68","2.15","42,601,907","245,759,823,968"
,운수창고업,"1,342.17","1,233.91","108.26","-8.07","40,256,174","922,511,167,305"
,통신업,"289.08","296.88","7.80","2.70","6,250,919","147,739,795,285"
,금융업,"301.46","298.74","2.72","-0.90","62,254,605","632,139,977,710"
,은행,"164.99","161.39","3.60","-2.18","3,982,672","30,603,910,820"
,증권,"1,347.30","1,326.77","20.53","-1.52","18,022,663","83,371,382,612"
,보험,"10,179.74","10,130.54","49.20","-0.48","16,202,908","106,697,220,845"
,서비스업,"1,134.66","1,135.66","1.00","0.09","31,592,148","722,586,465,899"
,코스피 200 건설,"176.84","179.43","2.59","1.46","14,766,261","221,475,076,440"
,코스피 200 중공업,"182.95","187.83","4.88","2.67","40,094,082","517,183,161,275"
,코스피 200 철강/소재,"530.90","531.25","0.35","0.07","2,409,108","121,826,858,410"
,코스피 200 에너지/화학,"988.93","984.27","4.66","-0.47","8,479,463","573,736,831,090"
,코스피 200 정보기술,"2,136.06","2,096.46","39.60","-1.85","35,194,929","2,132,536,234,817"
,코스피 200 금융,"472.97","467.20","5.77","-1.22","38,146,667","428,825,009,655"
,코스피 200 생활소비재,"969.73","970.75","1.02","0.11","8,207,337","412,747,252,275"
,코스피 200 경기소비재,"1,072.41","1,063.69","8.72","-0.81","19,153,150","714,279,898,582"
,코스피 200 산업재,"369.26","368.61","0.65","-0.18","11,875,794","237,952,402,280"
,코스피 200 헬스케어,"2,059.07","2,070.84","11.77","0.57","24,552,581","1,173,131,510,330"
,코스피 대형주,"1,895.39","1,871.72","23.67","-1.25","124,874,470","6,241,800,537,489"
,코스피 중형주,"1,934.00","1,961.26","27.26","1.41","251,080,479","3,925,814,627,218"
,코스피 소형주,"1,582.33","1,597.56","15.23","0.96","701,039,719","1,529,775,077,803"

 

Builder pattern 참고 : https://stackoverflow.com/questions/10961673/php-builder-pattern-without-inner-classes