Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- * Пришло время жрать оперативку
- */
- class AnimeAchievements{
- var $data;
- function __construct($data_in=null){
- $this->data =($data_in==null) ? (object)array() : json_decode(json_encode($data_in));
- }
- /*
- * Делаем запрос к found
- */
- function find($query){
- $result=new AnimeAchievements();
- if ($this->length()==0){
- // Если текущий список пустой, то даже разбирать не надо
- return $result;
- }
- // @todo not in через много пробелов
- if (!preg_match('_^ *(type|id|status|score) *(\\<\\>|\\=|in|not in|\\>|\\<|\\>\\=|\\<\\=) *([0-9a-z, ]+)_i', $query, $a)){
- return null;
- }
- // Заполняем переменные
- $q_variable =strtolower($a[1]);
- $q_comparision=strtolower($a[2]);
- if ($q_comparision=='not in'){$q_comparision='<>';}
- if ($q_comparision=='in'){$q_comparision='=';}
- $q_values=$a[3];
- //echo '<br />variable '.$q_variable.'; comparision '.$q_comparision.'; q_values '.$q_values;
- // Находим текущий value
- switch ($q_variable){
- case 'id': $key='anime_id'; break;
- default: $key=$q_variable;
- }
- //echo '<br/>key = '.$key;
- // Запоняем result
- foreach($this->data as $datum){
- $current_value=$datum->{$key};
- $a =preg_split('_[ ,]_', $q_values);
- $u =false;
- foreach ($a as $value){
- $value=$this->const_to_value($value);
- //echo '<br />'.$current_value.' '.$q_comparision.' '.$value;
- if (
- (($q_comparision=='=') and($value==$current_value))
- or
- (($q_comparision=='<>')and($value!=$current_value))
- or
- (($q_comparision=='>') and($value< $current_value))
- or
- (($q_comparision=='<') and($value> $current_value))
- or
- (($q_comparision=='>=')and($value<=$current_value))
- or
- (($q_comparision=='<=')and($value>=$current_value))
- ){ $u=true; break; }
- }
- if ($u){
- $result->data->{$datum->anime_id}=(object)$datum;
- }
- }
- return $result;
- }
- function const_to_value($value){
- if (preg_match('|^([0-9]+)$|', $value, $a)){
- return (int)$a[1];
- }
- $value=strtolower($value);
- // 1 — TV; 2 — OVA; 3 — Movie; 4 — Special; 5 — ONA; 6 — Music
- switch($value){
- case 'tv': return 1;
- case 'ova': return 2;
- case 'movie': return 3;
- case 'special': return 4;
- case 'ona': return 5;
- case 'music': return 6;
- case 'watching': return 1;
- case 'completed':return 2;
- case 'onhold': return 3;
- case 'dropped': return 4;
- case 'ptw': return 6;
- case 'drop': return 4;
- case 'plan': return 6;
- case 'complete': return 2;
- default: return null;
- }
- }
- function length(){
- $count=0;
- foreach ($this->data as $datum){$count++;}
- return $count;
- }
- function average_score(){
- if ($this->length()==0){return 0;}
- $sum=0;
- $count=0;
- foreach ($this->data as $datum){
- if ($datum->score>0){$sum+=$datum->score;$count++;}
- }
- return ($count==0) ? 0 : $sum/$count;
- }
- }// class AnimeAchievements
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement