Skip to content

Commit

Permalink
- updated README and the factory class with error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
szymach committed Jul 30, 2014
1 parent fd810ad commit bb14357
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ to functions.
- Moved all constants to a single file 'src/Resources/data/constants.php'. This file is *required*
for the library to function. If you use the factory class, the file is loaded automatically.

How to install it?
Installation:
================

[GitHub](https://github.com/szymach/c-pchart)
Expand All @@ -39,11 +39,11 @@ to your composer.json file and update your dependencies. After that, all
classes are available under "CpChart\Classes" namespace or "CpChart\Services"
for the factory.

How to use it?
Usage:
==============

The main difference is that you can either load the class via the 'use' statement
or use the provided factory. An example below.
or use the provided factory. An example below.


require __DIR__.'/../vendor/autoload.php';
Expand All @@ -68,7 +68,9 @@ or use the provided factory. An example below.
)
);
// creating a pie chart - notice that you specify the type of chart, not class name
// creating a pie chart - notice that you specify the type of chart, not class name.
// not all charts need to be created through this method (ex. the bar chart),
// some are created via the pImage class (check the documentation before drawing).
$pieChart = $factory->newChart("pie", $myPicture, $myData);

// do the drawing
Expand Down Expand Up @@ -97,6 +99,8 @@ Changelog

1.1.1 Changed chart loading via factory a bit (see class annotations)

1.1.2 Updated service class with Exception handling regarding missing / wrong class name

References
==========
[The original pChart website](http://www.pchart.net/)
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
"require": {
"php": ">=5.3.3"
},
"version": "1.1.1"
"version": "1.1.2"
}
36 changes: 35 additions & 1 deletion src/Services/pChartFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,15 @@ class pChartFactory
private $namespace = 'CpChart\Classes\\';

/**
* Load a new chart class (bar, pie etc.). Some classes require instances of
* Loads a new chart class (scatter, pie etc.). Some classes require instances of
* pImage and pData classes passed into their constructor. These classes are:
* pBubble, pPie, pScatter, pStock, pSurface and pIndicator. Otherwise the
* pChartObject and pDataObject parameters are redundant.
*
* ATTENTION! SOME OF THE CHARTS NEED TO BE DRAWN VIA A METHOD FROM THE
* pIMAGE CLASS (ex. 'drawBarChart'), NOT THROUGH THIS METHOD! READ THE
* DOCUMENTATION FOR MORE DETAILS.
*
* @param string $chartType - type of the chart to be loaded (for example 'pie', not 'pPie')
* @param \CpChart\Classes\pImage $pChartObject
* @param \CpChart\Classes\pData $pDataObject
Expand All @@ -38,10 +42,40 @@ public function newChart(
pImage $pChartObject = null,
pData $pDataObject = null
) {
$this->checkChartType($chartType);
$className = $this->namespace.'p'.ucfirst($chartType);
if (!class_exists($className)) {
throw new \Exception('The requested chart class does not exist!');
}
return new $className($pChartObject, $pDataObject);
}

/**
* Checks if the requested chart type is created via one of the methods in
* the pDraw class, instead through a seperate class. If a method in pDraw
* exists, an exception with proper information is thrown.
*
* @param string $chartType
* @throws \Exception
*/
private function checkChartType($chartType)
{
$methods = array(
'draw'.ucfirst($chartType).'Chart',
'draw'.ucfirst($chartType)
);
foreach ($methods as $method) {
if (method_exists($this->namespace.'pImage', $method)) {
throw new \Exception(
'The requested chart is not a seperate class, to draw it you'
. ' need to call the "'.$method.'" method on the pImage object'
. ' after populating it with data!'
. ' Check the documentation on library\'s website for details.'
);
}
}
}

/**
* Creates a new pData class with an option to pass the data to form a serie.
*
Expand Down

0 comments on commit bb14357

Please sign in to comment.