Skip to content

Commit 56ff9ef

Browse files
authored
Merge pull request #10 from karam-mustafa/main
Add Ableities to get features from getters
2 parents 61d45c3 + 8137935 commit 56ff9ef

File tree

4 files changed

+148
-25
lines changed

4 files changed

+148
-25
lines changed

.php-cs-fixer.cache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"php":"8.1.3","version":"3.7.0","indent":" ","lineEnding":"\n","rules":{"blank_line_after_opening_tag":true,"braces":{"allow_single_line_anonymous_class_with_empty_body":true},"class_definition":{"inline_constructor_arguments":false,"space_before_parenthesis":true},"compact_nullable_typehint":true,"declare_equal_normalize":true,"lowercase_cast":true,"lowercase_static_reference":true,"new_with_braces":true,"no_blank_lines_after_class_opening":true,"no_leading_import_slash":true,"no_whitespace_in_blank_line":true,"ordered_class_elements":{"order":["use_trait"]},"ordered_imports":{"sort_algorithm":"alpha"},"return_type_declaration":true,"short_scalar_cast":true,"single_blank_line_before_namespace":true,"single_trait_insert_per_statement":true,"ternary_operator_spaces":true,"visibility_required":true,"blank_line_after_namespace":true,"constant_case":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_keywords":true,"method_argument_space":{"on_multiline":"ensure_fully_multiline","keep_multiple_spaces_after_comma":true},"no_break_comment":true,"no_closing_tag":true,"no_space_around_double_colon":true,"no_spaces_after_function_name":true,"no_spaces_inside_parenthesis":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":{"elements":["property"]},"single_import_per_statement":true,"single_line_after_imports":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"encoding":true,"full_opening_tag":true,"array_syntax":{"syntax":"short"},"no_unused_imports":true,"not_operator_with_successor_space":true,"trailing_comma_in_multiline":true,"phpdoc_scalar":true,"unary_operator_spaces":true,"binary_operator_spaces":true,"blank_line_before_statement":{"statements":["break","continue","declare","return","throw","try"]},"phpdoc_single_line_var_spacing":true,"phpdoc_var_without_name":true,"class_attributes_separation":{"elements":{"method":"one"}}},"hashes":{"src\/Abstracts\/RelationalRelationAbstract.php":3099487083,"src\/Classes\/RelationalMetrics.php":3843364039,"src\/Interfaces\/RelationalInterface.php":1128215520,"tests\/Unit\/ExampleTest.php":3900274864,"tests\/BaseTest.php":3538535820,"tests\/Feature\/ExampleTest.php":3639981892}}
1+
{"php":"8.1.3","version":"3.7.0","indent":" ","lineEnding":"\n","rules":{"blank_line_after_opening_tag":true,"braces":{"allow_single_line_anonymous_class_with_empty_body":true},"class_definition":{"inline_constructor_arguments":false,"space_before_parenthesis":true},"compact_nullable_typehint":true,"declare_equal_normalize":true,"lowercase_cast":true,"lowercase_static_reference":true,"new_with_braces":true,"no_blank_lines_after_class_opening":true,"no_leading_import_slash":true,"no_whitespace_in_blank_line":true,"ordered_class_elements":{"order":["use_trait"]},"ordered_imports":{"sort_algorithm":"alpha"},"return_type_declaration":true,"short_scalar_cast":true,"single_blank_line_before_namespace":true,"single_trait_insert_per_statement":true,"ternary_operator_spaces":true,"visibility_required":true,"blank_line_after_namespace":true,"constant_case":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_keywords":true,"method_argument_space":{"on_multiline":"ensure_fully_multiline","keep_multiple_spaces_after_comma":true},"no_break_comment":true,"no_closing_tag":true,"no_space_around_double_colon":true,"no_spaces_after_function_name":true,"no_spaces_inside_parenthesis":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":{"elements":["property"]},"single_import_per_statement":true,"single_line_after_imports":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"encoding":true,"full_opening_tag":true,"array_syntax":{"syntax":"short"},"no_unused_imports":true,"not_operator_with_successor_space":true,"trailing_comma_in_multiline":true,"phpdoc_scalar":true,"unary_operator_spaces":true,"binary_operator_spaces":true,"blank_line_before_statement":{"statements":["break","continue","declare","return","throw","try"]},"phpdoc_single_line_var_spacing":true,"phpdoc_var_without_name":true,"class_attributes_separation":{"elements":{"method":"one"}}},"hashes":{"src\/Abstracts\/RelationalRelationAbstract.php":3299928560,"src\/Classes\/RelationalMetrics.php":842680276,"src\/Interfaces\/RelationalInterface.php":1128215520,"tests\/Unit\/ExampleTest.php":3900274864,"tests\/BaseTest.php":3538535820,"tests\/Feature\/ExampleTest.php":3639981892}}

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@ The Basic Usage of this package is the same of getting count of some model insta
2525
```php
2626
$instance = (new \SOS\RelationalMetrics\Classes\RelationalMetrics("ModelName"));
2727
// example: (new \SOS\RelationalMetrics\Classes\RelationalMetrics("Store"));
28+
// or you can specify thr model name by this way: new RelationalMetrics(Store::class);
2829
$metrics = $instance->getBasicMetrics();
30+
// or you can get the count or the message directly.
31+
$directCount = $instance->getCount();
32+
$directName = $instance->getName();
33+
2934
/*
3035
* Response will be like:
3136
* [
@@ -42,6 +47,7 @@ Let's assume we want to get the number of stores that has products with price mo
4247
```php
4348
$instance = (new \SOS\RelationalMetrics\Classes\RelationalMetrics("ModelName"));
4449
// example: (new \SOS\RelationalMetrics\Classes\RelationalMetrics("Store"));
50+
// or you can specify thr model name by this way: new RelationalMetrics(Store::class);
4551
$metrics = $instance->getRelationalMetrics($relationName, $relationColumn, $value);
4652
// example: $instance->getRelationalMetrics('products, 'price', 500);
4753
/*
@@ -62,7 +68,7 @@ And last but not least, You could get the metrics about a model depending on any
6268
```php
6369
$instance = (new \SOS\RelationalMetrics\Classes\RelationalMetrics("ModelName"));
6470
// example: (new \SOS\RelationalMetrics\Classes\RelationalMetrics("Store"));
65-
71+
// or you can specify thr model name by this way: new RelationalMetrics(Store::class);
6672
// example for the conditions
6773
$conditions = [
6874
['method' => 'where', 'column' => 'address', 'operator' => 'like', 'value' => '%UAE%'],

src/Abstracts/RelationalRelationAbstract.php

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,87 @@
22

33
namespace SOS\RelationalMetrics\Abstracts;
44

5+
/**
6+
* Class RelationalRelationAbstract
7+
*
8+
* @author karam mustafa
9+
* @package SOS\RelationalMetrics\Abstracts
10+
*/
511
abstract class RelationalRelationAbstract
612
{
13+
/**
14+
* model to handle
15+
* @author karam mustafa
16+
* @var string
17+
*/
718
protected $model = '';
819

20+
/**
21+
* count of rows
22+
* @author karam mustafa
23+
* @var string
24+
*/
25+
private $count = null;
26+
27+
/**
28+
* message for a count
29+
* @author karam mustafa
30+
* @var string
31+
*/
32+
private $name = '';
33+
34+
/**
35+
* @return string
36+
* @author karam mustafa
37+
*/
38+
public function getCount()
39+
{
40+
if (is_null($this->count)) {
41+
$this->getCountDirectly();
42+
}
43+
44+
return $this->count;
45+
}
46+
47+
/**
48+
* @param string $count
49+
*
50+
* @return RelationalRelationAbstract
51+
* @author karam mustafa
52+
*/
53+
public function setCount($count)
54+
{
55+
$this->count = $count;
56+
57+
return $this;
58+
}
59+
60+
/**
61+
* @return string
62+
* @author karam mustafa
63+
*/
64+
public function getName()
65+
{
66+
if (is_null($this->name)) {
67+
$this->name = $this->getResponseName();
68+
}
69+
70+
return $this->name;
71+
}
72+
73+
/**
74+
* @param string $name
75+
*
76+
* @return RelationalRelationAbstract
77+
* @author karam mustafa
78+
*/
79+
public function setName($name)
80+
{
81+
$this->name = $name;
82+
83+
return $this;
84+
}
85+
986
/**
1087
* return count for the model's relation depending on the relation type (where, when ... etc), column and value
1188
*
@@ -56,9 +133,19 @@ protected function getCountWithConditions(array $conditions): int
56133
*/
57134
protected function getCountDirectly()
58135
{
59-
return $this->model::query()->get()->count();
136+
$this->count = $this->model::query()->get()->count();
137+
138+
return $this->count;
60139
}
61140

141+
/**
142+
* append final values to their properties
143+
*
144+
* @param $model_count
145+
*
146+
* @return array
147+
* @author karam mustafa
148+
*/
62149
protected function returnFinalResponse($model_count): array
63150
{
64151
return [
@@ -67,6 +154,12 @@ protected function returnFinalResponse($model_count): array
67154
];
68155
}
69156

157+
/**
158+
* description
159+
*
160+
* @return string
161+
* @author karam mustafa
162+
*/
70163
protected function getResponseName()
71164
{
72165
$path = explode('\\', $this->model);

src/Classes/RelationalMetrics.php

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace SOS\RelationalMetrics\Classes;
44

5+
use Illuminate\Database\Eloquent\Model;
56
use SOS\RelationalMetrics\Abstracts\RelationalRelationAbstract;
67
use SOS\RelationalMetrics\Interfaces\RelationalInterface;
78

@@ -14,60 +15,83 @@
1415
class RelationalMetrics extends RelationalRelationAbstract implements RelationalInterface
1516
{
1617
/**
17-
* description
18+
* class constructor
1819
*
20+
* @throws \Exception
1921
* @author zainaldeen
2022
* @var string
2123
*/
22-
public function __construct(string $target)
24+
public function __construct(string $class)
2325
{
24-
if (class_exists('App\\Models\\'.$target)) {
25-
$this->model = "App\\Models\\" . $target;
26-
} else {
27-
return "Target Model ". $target. " Is Not Found!";
28-
}
26+
$className = $this->validateClass($class);
27+
28+
$this->model = $className;
2929
}
3030

3131
/**
3232
* return simple count for the model
3333
*
34-
* @author zainaldeen
3534
* @return array
35+
* @author zainaldeen
3636
*/
3737
public function getBasicMetrics()
3838
{
39-
$model_count = $this->getCountDirectly();
40-
41-
return $this->returnFinalResponse($model_count);
39+
return $this->returnFinalResponse(
40+
$this->getCountDirectly()
41+
);
4242
}
4343

4444
/**
4545
* return count for the model's relation depending on the relation type (where, when ... etc), column and value
4646
*
47-
* @author zainaldeen
48-
* @param string $relation
49-
* @param string $column
50-
* @param mixed $value
47+
* @param string $relation
48+
* @param string $column
49+
* @param mixed $value
50+
*
5151
* @return array
52+
* @author zainaldeen
5253
*/
5354
public function getRelationalMetrics($relation, $column, $value)
5455
{
55-
$model_count = $this->returnRelationalCount($relation, $column, $value);
56-
57-
return $this->returnFinalResponse($model_count);
56+
return $this->returnFinalResponse(
57+
$this->returnRelationalCount($relation, $column, $value)
58+
);
5859
}
5960

6061
/**
6162
* return count for the model depending on the passing conditions
6263
*
63-
* @author zainaldeen
64-
* @param array $conditions
64+
* @param array $conditions
65+
*
6566
* @return array
67+
* @author zainaldeen
6668
*/
6769
public function getConditionalMetrics($conditions)
6870
{
69-
$model_count = $this->getCountWithConditions($conditions);
71+
return $this->returnFinalResponse(
72+
$this->getCountWithConditions($conditions)
73+
);
74+
}
75+
76+
/**
77+
* check if user insert a correct class name, otherwise we will ty to resolve the model class name
78+
*
79+
* @param string $class
80+
*
81+
* @return \SOS\RelationalMetrics\Classes\string|string
82+
* @throws \Exception
83+
* @author karam mustafa
84+
*/
85+
private function validateClass(string $class)
86+
{
87+
$fixedClassName = str_contains("App\\Models", $class)
88+
? $class
89+
: "\\App\\Models\\$class";
90+
91+
if (! class_exists($fixedClassName) && $fixedClassName instanceof Model) {
92+
throw new \Exception("model $class not found !!");
93+
}
7094

71-
return $this->returnFinalResponse($model_count);
95+
return $fixedClassName;
7296
}
7397
}

0 commit comments

Comments
 (0)